2017-03-08 56 views
1

不同VBA警報我有這樣的代碼:當結果是由0

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 

它計算在文檔中的2點數量的列之間的差異。 現在,我想要提醒,如果有任何區別(Cantv.Value-Cantc.Value <> 0)。代碼應該檢查文檔的每一行是否有差異,當它找到第一個時,停止並顯示msgbox。

我這樣做了,但我不確定沒關係。當最後一行有差異時,它顯示出彈出。

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 

你能幫助我嗎?謝謝。

+3

考慮編輯代碼塊的清晰度。我懷疑if和while語句的順序有錯誤。 – putonspectacles

回答

2

你只是放錯了地方的If說法,應該是內環路:

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 
+0

太棒了,就是這個問題:D –

+0

我可以再問一個問題嗎? 它彈出一個msgbox的每一行是差異。即使我有1行差異或「n」行,我怎樣才能彈出一個msgbox? –

+1

@IonutP。在循環內部添加一個計數器('i = i + 1'或者smthg就是這樣),並且把循環後面的'MsgBox'放在一個測試中,比如'If i> 0 Then MsgBox';) – R3uK