Catching Errors | 3021, 2766, ETC | Need Help!

J

JK

In most of my forms I have a before update event that make sure certain
information has been entered before allowing the user the ability to save the
form. For example, in my Contacts form when adding a new contact, the user
must provide a last name. If a last name is not entered and the form is saved
or closed the user gets a pop-up message telling them they cannot continue.

This works great except for the fact that I'm getting an error after the
MsgBox. The errors seem to be usually 3021 or 2766 but I've seen others.

I'm using the SaveIt function in these forms. Below I've pasted the Before
Update Event and the SaveIt function. I've tried catching the errors
everywhere I would think to catch them but it doesn't seem to work.

I'd like to be able to surpress the error message and prevent a record from
being added to the ErrorLog Table via the SaveIt function.

Hope this all makes sense; I'm using Access 2003.
Any help would be greatly appreciated.


Private Function SaveIt() As Integer
Dim lngErr As Long, strError As String

' Common Save routine called from cmdSave and a couple of other places

' Default: We expect this to work
SaveIt = True
' No need to do anything if the form isn't "dirty"
If (Me.Dirty = True) Then
' OK, gonna try to save - set error trap
On Error GoTo Save_Error
Me.Dirty = False ' Force a save by resetting Dirty
End If

Save_Exit:
Exit Function

Save_Error:
' Got here if the save failed. Handle most common errors
' (Some may be handled by Form_Error instead)
SaveIt = False ' Indicate save failed
' Try to analyze the error
Select Case Err
Case errCancel, errCancel2, errPropNotFound ' Cancel - ignore
Resume Save_Exit
Case errDuplicate ' Duplicate row - custom error message
MsgBox "You're trying to add a record that already exists. " & _
"Enter a new Issue or click Cancel.", vbCritical, "Attention!"
Case errInvalid, errInputMask
' Invalid data - custom error and log
MsgBox "You have entered an invalid value. ", vbCritical,
"Attention!"
ErrorLog Me.Name & "_Save", Err, Error
' Field validation, Table validation, Custom Validation, End of
Search, Spelling Check
Case errValidation, errTableValidate, errCustomValidate,
errSearchEnd, errSpellCheck
' Display the error
' All validation rules in the tables have custom error messages.
MsgBox Error, vbCritical, "Attention!"
Case Else
' Dunno - log and let error display
' Save the error code values because ErrorLog may get additional
errors
lngErr = Err
strError = Error
ErrorLog Me.Name & "_Save", lngErr, strError
MsgBox "Error attempting to save: " & lngErr & " " & strError &
Chr$(13) & Chr$(10) & "Try again or click Cancel to close without saving.",
48, "Attention!"
End Select
Resume Save_Exit
End Function
 

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

Top