What should I remove from this code?

punter

Registered User.
Local time
Today, 13:38
Joined
Nov 24, 2006
Messages
108
Hello everyone. I thank you in advance for your time.

I think I have a little bit too much stuff in my code. I want an error message to pop up if the Carton Count or CBM is out of balance. Here is the code:

Code:
Private Sub AddRecord_Click()
On Error GoTo Err_AddRecord_Click

If (Me.Cartons - Me.Text31) = 0 Then
    DoCmd.GoToRecord , , acNewRec
Else
    MsgBox "Carton count out of balance"
End If

Err_AddRecord_Click:
    MsgBox Err.Description
    Resume Exit_AddRecord_Click

    
    If (Me.Text69 - Me.Text78) = 0 Then
    DoCmd.GoToRecord , , acNewRec
Else
    MsgBox "CBM out of balance"
End If

Exit_AddRecord_Click:
    Exit Sub

Err_AddRecord_Click:
    MsgBox Err.Description
    Resume Exit_AddRecord_Click
    
End Sub

The first half and second half of the code work if I have them seperately. The problem comes when I am trying to combine them. I have tried pulling out various commands when I get error messages but I'm not sure exactly what I am doing wrong.

As always your thoughts are most welcome.

Thanks

Eddie.
 
Try:
Code:
Private Sub AddRecord_Click()
   On Error GoTo Err_AddRecord_Click

   If (Me.Cartons - Me.Text31) <> 0 Then
      MsgBox "Carton count out of balance"
   ElseIf (Me.Text69 - Me.Text78) <> 0 Then
      MsgBox "CBM out of balance"
   Else
      DoCmd.GoToRecord , , acNewRec
   End If

Exit_AddRecord_Click:
   Exit Sub

Err_AddRecord_Click:
   MsgBox Err.Description
   Resume Exit_AddRecord_Click

End Sub
 
That worked awesome. Thank you so very much. I'm getting better with VBA but not great at it yet. I knew it was something simple but it would have taken me forever to figure it out.
 
Is there some kind of bump points (or whatever it used to be) that I could click for you?
 
Try clicking the scales at the top right of RuralGuru's post ;)
 

Users who are viewing this thread

Back
Top Bottom