VBA MS Access Loop: Как определить оператор IF, который сравнивает текущий рекорд с предыдущей записью во время цикла - PullRequest
0 голосов
/ 19 мая 2019

Я хотел бы зациклить набор записей и выяснить, произошло ли значительное изменение значения, например, +/- 10% от одной записи к другой.Моя проблема в том, что я не знаю, как ссылаться на предыдущую запись ... или сравнивать ее со следующей .....

Здесь идея набора записей

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

Ответы [ 2 ]

1 голос
/ 19 мая 2019

Это может быть что-то вроде этого:

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
0 голосов
/ 19 мая 2019

Большое спасибо donPablo и Gustav.Я мог бы решить проблему с вашей помощью:

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
...