I have a form where, before the form data is saved, (a) certain fields must be filled in if other fields are filled in and (b) one specified date must precede another specified date.
I want to ensure that the form can't be saved with those errors present. So I've used the form's Before Update event--Private Sub Form_BeforeUpdate(Cancel As Integer)--I've coded detectors and error messages for those requirements. I've also specified Cancel = True after each of the error detector/message parts of the Sub's code.
Here's a sample of that Sub:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'If Gender or LastName field is blank, display error message.
If LenB([Gender] & "") = 0 Or LenB([LastName] & "") = 0 Then
MsgBox "On the Identity tab, please specify both Gender and Last Name."
'Put the cursor on the appropriate tab and field
If LenB([Gender] & "") = 0 Then
Gender.SetFocus
ElseIf LenB([Gender] & "") > 0 Then
LastName.SetFocus
End If
'Cancel the update
Cancel = True
'If FirstPublicationDate is earlier than DeathDate, display error message.
ElseIf Me.FirstPublicationDate < Me.DeathDate Then
MsgBox "Date of Death (on Death Age/Date tab) must be earlier" & vbCrLf & _
"or the same as First Obituary Date (on Obituary tab)."
'Put the cursor on the appropriate tab and field
DeathDate.SetFocus
'Cancel the update
Cancel = True
'[snip]
End If
End Sub
I've also built several command buttons for navigation (Next Record, Previous Record, First Record, Last Record), because I don't want to assume form users will be familiar with the Access navigation at the bottom of the frame. So for each of those buttons, I want the same error detecting/messages and "save prevention" to occur. Here's my code for one of my nav buttons:
Code:
Private Sub btnGotoFirstRecord_Click()
DoCmd.GoToRecord , , acFirst
End Sub
The problem is that if one of my nav buttons is clicked with errors on the form, my error message is displayed all right, but when OK is clicked, a system error message appears:
Run-time error '2105'
You can't go to the specified record.
...and the program stops. If I instead use the standard Access nav arrows, that message doesn't appear and all is well. Is there a way to prevent the system message from appearing?