Before Update validation

B

BruceM

There are two parts to this. First, I use code in a standard module and
custom navigation buttons for record navigation. This lets me use the same
functions and the same buttons in all of my forms. For instance, to go to
the next record, I have the following function in the module:

Public Function TheNextRecord()

On Error GoTo NextRecord_Error

DoCmd.GoToRecord , , acNext

ProcExit:
Exit Function

NextRecord_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
"in NextRecord, mdlRecordNav"
Resume ProcExit

End Function

The command button is cmdNext. In its Click property:
=TheNextRecord()

Second, I am using a form's Before Update event to perform some data
validation (mostly making sure that required fields are filled in). For
instance:

If IsNull(Me.Summary) Then
If MsgBox("Summary is needed", vbOKCancel) = vbOK Then
Me.txtSummary.SetFocus
Cancel = True
Else
Me.Undo
End If
End If

When I use cmdNext to go to a next record while Summary is empty (and
changes have been made elsewhere in the record), I see the message box as
intended. However, if I click OK (return to txtSummary), I receive the
error message 2105 (You can't go to the specified record). The error is in
the function TheNextRecord. I can trap the error:

If Err.Number <> 2105 Then ' 2105: "You can't go to the specified
record"
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
"in NextRecord, mdlRecordNav"
End If

but I wonder if there is a better way. I have other code that disables
cmdNext when I am at the last record (thus avoiding error 2105, which would
be generated if I was at the last record and attempted to go to the next
one). Is there another reason I would want not to trap 2105? Can I do
something different in the Before Update event that would let me avoid
generating the error?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top