VBA MS Access Loop:如何定义在循环期间将当前记录与先前记录进行比较的 IF 语句
VBA MS Access Loop: How to define IF statement that compares Current Recod to previous Record douring a loop
我想遍历一个记录集并弄清楚值是否有显着变化,例如+/-10% 从一条记录到另一条记录。
我的问题是我不知道如何参考以前的记录...或将它与下一个进行比较.....
这里是Recordset的思路
Month_Year Price
01.2019 112.85
02.2019 145.25 (here the price jumped up more then 10% --> Msg "Check....")
03.2019 147.45
rs1.MoveFirst
Do Until rs1.EOF
******** HERE I NEED HELP:
IF
rs1.currentrecord???? / rs1.previousrecord???? between 0.9 and 1.1 THEN
rs1.Edit
rs1!Comments = "Check if Index is correct"
rs1.Update
End If
rs1.MoveNext
Loop
可能是这样的:
Dim CurrentPrice As Currency
Dim PreviousPrice As Currency
rs1.MoveFirst
Do Until rs1.EOF
CurrentPrice = rs1!Price.Value
If PreviousPrice > 0 Then
If CurrentPrice / PreviousPrice >= 1.1 Or
CurrentPrice / PreviousPrice <= 0.9 Then
rs1.Edit
rs1!Comments.Value = "Check if Index is correct"
rs1.Update
End If
End If
PreviousPrice = CurrentPrice
rs1.MoveNext
Loop
rs1.Close
非常感谢 Pablo 和 Gustavo。我可以在你的帮助下解决问题:
Function Mailings()
Dim rs1 As DAO.Recordset
Dim db As Database
Dim StrSql1 As String
Dim CurrentPrice As Single
Dim PreviousPrice As Single
Set db = CurrentDb
StrSql1 = "SELECT * " & _
"FROM IAZI_Index " & _
"ORDER BY JahrT ASC;"
Set rs1 = db.OpenRecordset(StrSql1)
rs1.MoveFirst
Do Until rs1.EOF
If rs1.BOF = True Then
PreviousPrice = rs1!SI_Condominium_PR.Value
Debug.Print rs1!SI_Condominium_PR
rs1.MoveNext
End If
Debug.Print rs1!SI_Condominium_PR
CurrentPrice = rs1!SI_Condominium_PR.Value
If PreviousPrice > 0 Then
If CurrentPrice / PreviousPrice >= 1.03 Or CurrentPrice / PreviousPrice <= 0.97 Then
rs1.Edit
rs1!Comments = "Check if Index is correct"
rs1.Update
End If
End If
PreviousPrice = CurrentPrice
rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing
Set db = Nothing
End Function
我想遍历一个记录集并弄清楚值是否有显着变化,例如+/-10% 从一条记录到另一条记录。 我的问题是我不知道如何参考以前的记录...或将它与下一个进行比较.....
这里是Recordset的思路
Month_Year Price
01.2019 112.85
02.2019 145.25 (here the price jumped up more then 10% --> Msg "Check....")
03.2019 147.45
rs1.MoveFirst
Do Until rs1.EOF
******** HERE I NEED HELP:
IF
rs1.currentrecord???? / rs1.previousrecord???? between 0.9 and 1.1 THEN
rs1.Edit
rs1!Comments = "Check if Index is correct"
rs1.Update
End If
rs1.MoveNext
Loop
可能是这样的:
Dim CurrentPrice As Currency
Dim PreviousPrice As Currency
rs1.MoveFirst
Do Until rs1.EOF
CurrentPrice = rs1!Price.Value
If PreviousPrice > 0 Then
If CurrentPrice / PreviousPrice >= 1.1 Or
CurrentPrice / PreviousPrice <= 0.9 Then
rs1.Edit
rs1!Comments.Value = "Check if Index is correct"
rs1.Update
End If
End If
PreviousPrice = CurrentPrice
rs1.MoveNext
Loop
rs1.Close
非常感谢 Pablo 和 Gustavo。我可以在你的帮助下解决问题:
Function Mailings()
Dim rs1 As DAO.Recordset
Dim db As Database
Dim StrSql1 As String
Dim CurrentPrice As Single
Dim PreviousPrice As Single
Set db = CurrentDb
StrSql1 = "SELECT * " & _
"FROM IAZI_Index " & _
"ORDER BY JahrT ASC;"
Set rs1 = db.OpenRecordset(StrSql1)
rs1.MoveFirst
Do Until rs1.EOF
If rs1.BOF = True Then
PreviousPrice = rs1!SI_Condominium_PR.Value
Debug.Print rs1!SI_Condominium_PR
rs1.MoveNext
End If
Debug.Print rs1!SI_Condominium_PR
CurrentPrice = rs1!SI_Condominium_PR.Value
If PreviousPrice > 0 Then
If CurrentPrice / PreviousPrice >= 1.03 Or CurrentPrice / PreviousPrice <= 0.97 Then
rs1.Edit
rs1!Comments = "Check if Index is correct"
rs1.Update
End If
End If
PreviousPrice = CurrentPrice
rs1.MoveNext
Loop
rs1.Close
Set rs1 = Nothing
Set db = Nothing
End Function