Error trapping a query. How?

Woof

Registered User.
Local time
Today, 12:29
Joined
Feb 13, 2012
Messages
12
Hi all.

Simple question, but If I run a query that looks for a serial or reference number in a table, where and how do I trap the resulting error when the number isn't found in the table?

Form 1 is bound to a table. A button on form 1 launches form 2, a small dialog form with a few text boxes. (I am using just one text box at the moment.) The user enters a reference number in this text box and presses the search button. The code for this button is below. The query is designed to recieve the content of the textbox as it's criteria.

When a non existant number is entered the error catching on the button doesn't kick in, so where DO I catch it please? Thanks.



Code:
Private Sub btnSearchIncidents_Click()
On Error GoTo Err_btnSearchIncidents_Click
 
DoCmd.OpenForm "frmAnalysis", acNormal
Forms!frmAnalysis.lblAnalysisFormTitle.Caption = "Showing single selected record by number"
Forms!frmAnalysis.RecordSource = "qrySearchByNumber"
DoCmd.GoToRecord , , acLast
DoCmd.Close acForm, "frmChooseIncidentsToView", acSaveNo
Exit_btnSearchIncidents_Click:
    Exit Sub
Err_btnSearchIncidents_Click:
    MsgBox Err.Description
    Resume Exit_btnSearchIncidents_Click
    
End Sub
 
I typically base the form on the query (without the criteria) and then filter the form as it opens. I would check to make sure there are records before opening the form using an IF..THEN END IF in conjunction with the DCount() function.

The basic code would go something like this:

IF DCount("*","qrySearchByNumber","searchfield='" & textboxcontrolname & "'")=0 THEN
msgbox "No records meet your criteria"
exit sub
ELSE
DoCmd.OpenForm "frmAnalysis", acNormal,,"searchfield='" & textboxcontrolname & "'"
END IF

Of course, if there are multiple criteria you would have to include all of them in both the DCount() as well as the criteria section of the Docmd.Openform
 

Users who are viewing this thread

Back
Top Bottom