Problem with error trapping

kbrown

Registered User.
Local time
Today, 03:37
Joined
Dec 4, 2003
Messages
45
I am trying to trap the 2501 error message for the following circumstance:

User presses button to view Nutrition Screen responses. If there are no nutrition screens, user gets a message telling her there aren't any and is returned to main form (frmClientInfo). Of course, this process cancels the opening of the second form, thus the 2501 error message. I just want to trap it and continue to exit the sub, but I am still getting the runtime error 2501.

There are two places that the error message could be coming from, but I don't know which. One, the code behind the button. Two, the code on opening of the form. I have error handling in place for both sections of code. It seems like my error handling isn't even being triggered.

Is there another way to trap this error, or have I made a silly mistake that I can't see? Thank you for taking a look at this!

Code:
Private Sub cmdNutrScrn_Click()
On Error GoTo Err_cmdNutrScrn_Click
    
If IsNull(Me.frmEnrollment!EnrollDate) Then
 MsgBox "Enter Enrollment Information For This Person"
'Set focus on EnrollDate of subform so information can be entered
    Me.frmEnrollment.SetFocus
    Me.frmEnrollment.Form!EnrollDate.SetFocus
Else
   Me.Visible = False
   DoCmd.OpenForm "frmSelectNS", acNormal, , acFormReadOnly
End If

Exit_cmdNutrScrn_Click:
    Exit Sub
    
Err_cmdNutrScrn_Click:
    If Err = 2501 Then 'OpenForm action was cancelled
         Resume Exit_cmdNutrScrn_Click
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_cmdNutrScrn_Click
    End If
End Sub

Code:
Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open

Dim rs As Object

Set rs = Me.RecordsetClone

If rs.RecordCount < 1 Then
    Cancel = True
    MsgBox "There are no nutrition screens entered for this person."
    DoCmd.OpenForm "frmClientInfo"
End If

Exit_Form_Open:
    Exit Sub

Err_Form_Open:
    If Err = 2501 Then 'OpenForm action was cancelled
         Resume Exit_Form_Open
    Else
        MsgBox Err.Number & " - " & Err.Description
        Resume Exit_Form_Open
    End If
End Sub
 
Have you tried to step through the code using a breakpoint? This should isolate the error...

???
kh
 
You only need to trap the error on the command button,
in fact if you used DoCmd.Close instead of Cancel=True you wouldn't get the error at all
 
Thank you both for the ideas! I had left the error breaking option set to break on all errors, so of course it was doing what I told it to do. I reset it to break on unhandled errors and now I am getting the results I wanted!

Thanks!
 

Users who are viewing this thread

Back
Top Bottom