Joseph Bupe
Joseph Bupe

Reputation: 77

Prevent The Details Form From Openning If No Details, Access 2007

I am trying to prevent the details form from opening if their no details to display. It seems to work, but there is an extra annoying error 2501 that pops ups "The openform action was cancelled" when I close the custom error message.

I have this code on the details button on the parent form:

DoCmd.OpenForm "frmEvent_Participants", , , "[Event_ID]=" & Me!txtEvent_ID

And in the open event of the details form I have this code:

Private Sub Form_Open(Cancel As Integer)

With Me.RecordsetClone
If .RecordCount = 0 Then
Beep
MsgBox "There are no records to view", vbInformation + vbOKOnly
Cancel = True
End If
End With

End Sub

Upvotes: 0

Views: 35

Answers (1)

ashleedawg
ashleedawg

Reputation: 21639

It's not the form you're opening that's generating the error, it's the code or macro that's opening the form, and the error can be avoided with basic error handling.

For example, if the form is called Form1 and you're opening with code behind a command button (or whatever), with code like this:

DoCmd.OpenForm "Form1"

then wrap error handling around it like:

On Error Resume Next
DoCmd.OpenForm "Form1"
On Error GoTo 0

Alternatively, depending on what the form's doing, you might want to be more careful, to make sure that the only error you're "ignoring" with On Error Resume Next is Error 2501:The OpenForm action was canceled then check for it:

On Error Resume Next
DoCmd.OpenForm "Form1"
If Err <> 0 And Err <> 2501 Then Err.Raise Err, , Err.Description
On Error GoTo 0

Upvotes: 2

Related Questions