Question : Access VBA

Hi, I have the below error code to exit the sub after a msgbox warning but when I comment out the msgbox, it works fine but with the msgbox code, I get the msgbox every time the button is clicked regardless of whether an error occured or EOF = true.

I'm sure it is just a syntax issue but I can't see the forrest for the trees, so to speak.

Thanks for any help.


On Error GoTo NextRecErr

If Me.Dirty Then
MsgBox "You have unsaved changes! I will now save the changes.", vbCritical & vbOKOnly
Me.Dirty = False
End If

DoCmd.GoToRecord , , acNext


NextRecErr:
MsgBox "An error has occured or you are at the end of the records.", vbInformation
Exit Sub

End Sub

Answer : Access VBA

Move the Exit Sub above NextRecErr:, not after, so that your code does not continue there every time:

On Error GoTo NextRecErr

If Me.Dirty Then
MsgBox "You have unsaved changes! I will now save the changes.", vbCritical & vbOKOnly
Me.Dirty = False
End If

DoCmd.GoToRecord , , acNext
Exit Sub

NextRecErr:
MsgBox "An error has occured or you are at the end of the records.", vbInformation


End Sub
Random Solutions  
 
programming4us programming4us