Link to home
Start Free TrialLog in
Avatar of AbacusInfoTech
AbacusInfoTech

asked on

Placing of code to supress error messages (DoCmd.SetWarnings)

Hello experts.

I have the following code in a popup form that stops the form opening if there are no records to display:

Private Sub form_open(Cancel As Integer)
DoCmd.SetWarnings (False)
    If Me.RecordsetClone.RecordCount = 0 Then
    MsgBox "There are no records to display within your search criteria."
    Cancel = True
  End If
DoCmd.SetWarnings (True)
End Sub

This works fine, but once I 'OK' my own message box as coded above, I get the defaut access error message 'The openform action was cancelled'. It is this error message I want to supress so I must be placing my "DoCmd.SetWarnings" in the wrong place...

Rem'ing out the DoCmd.SetWarnings (True) doesn't even help, so am I even using the right code?

ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AbacusInfoTech
AbacusInfoTech

ASKER

Thanks, right first time :-)

FYI My main form's code is:

Private Sub BtnConsultantPicker_Click()
On Error GoTo Err_BtnConsultantPicker_Click
    Dim stDocName As String
    Dim stLinkCriteria As String
    stDocName = "FrmUserPopJobs"
        stLinkCriteria = "[consultantid]=" & Me![CboConsultantPicker]
    DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_BtnConsultantPicker_Click:
    Exit Sub
Err_BtnConsultantPicker_Click:
    If Err.Number = 2501 Then  
        Resume Exit_BtnConsultantPicker_Click    
    End If
    MsgBox Err.description
    Resume Exit_BtnConsultantPicker_Click
End Sub

And my Form code is now:

Private Sub form_open(Cancel As Integer)
    If Me.RecordsetClone.RecordCount = 0 Then
    MsgBox "There are no records to display within your search criteria."
    Cancel = True
  End If
End Sub
Fine.

Thanks

Pete