1

I have this code:

DM.OnUpdate="CalculDiferenta"
DM.OnInsert="CalculDiferenta"

Sub CalculDiferenta

If Dsrid.Value=50000 Then

stl.first
Do While Not Stl.Eof
Diferenta.Value=Cantv.Value-Cantc.Value


Stl.Next
Loop

end if
End Sub

It calculates the difference between 2 Quantity columns in a document. Now, i want to be alerted if there is any difference (Cantv.Value-Cantc.Value <> 0 ). The code should check every line of the document for any differences and when it finds the first one, stop and show msgbox.

I did this, but i'm not sure it's ok. It show's a pop up just when the last line has a difference.

DM.OnUpdate="VerificareDiferente"
DM.OnInsert="VerificareDiferente"

Public Sub VerificareDiferente

If Dsrid.value=50000 and Cantv.Value-Cantc.Value <> 0  then

stl.first
             Do  While Not Stl.Eof
       MsgBox "Exista diferente intre cantitate comandata si cantitate verificata.", vbInformation, "Atentie !!!"
             Stl.Next
             Loop
       end if
End Sub

Could you help me guys ? Thank you.

R3uK
  • 14,417
  • 7
  • 43
  • 77
Ionut P.
  • 63
  • 10
  • 3
    consider editing your code blocks for clarity. I suspect that there is an error with the order of the if and while statements. – parsethis Mar 08 '17 at 07:57

1 Answers1

2

You just misplaced your If statement, it should be inside the loop :

DM.OnUpdate = "VerificareDiferente"
DM.OnInsert = "VerificareDiferente"

Public Sub VerificareDiferente()
    Stl.first
    Do While Not Stl.EOF
        If Dsrid.Value = 50000 And Cantv.Value - Cantc.Value <> 0 Then
            MsgBox "Exista diferente intre cantitate comandata si cantitate verificata.", _
                vbInformation, "Atentie !!!"
        Else
        End If
        Stl.Next
    Loop
End Sub
R3uK
  • 14,417
  • 7
  • 43
  • 77
  • Great, that was the problem :D – Ionut P. Mar 08 '17 at 09:41
  • May i have another question ? It pop's up a msgbox for every line where are differences. How can i pop up just one msgbox even if i have 1 line with differences or "n" lines ? – Ionut P. Mar 08 '17 at 09:42
  • 1
    @IonutP. Add a counter inside the loop (`i = i +1` or smthg like that) and put the `MsgBox` after the loop with a test like `If i > 0 Then MsgBox` ;) – R3uK Mar 08 '17 at 09:50