Need to produce an error message

M

Ment@lBl0ck

From a form we are finding a customer, we use like for a wildcard option;

When we enter a seach string, for example "mich*" - it will seach for
michael, michelle etc... If no match is found it creates a new record....
What we want is to do is display a message if no match is found and NOT to
create a new record.

Have limited code experience, this is what was created by the wizard and
then added to for the wildcard searching.

Code
Private Sub viaPhone_Click()
On Error GoTo Err_viaPhone_Click
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Customer"
stLinkCriteria = "[Phone] LIKE " & "'" & Me![FindCustomer] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_viaPhone_Click:
Exit Sub

Err_viaPhone_Click:
MsgBox Err.Description
Resume Exit_viaPhone_Click
 
G

Graham Mandeno

You can have your code use your filter string to check first whether there
are any matching records and, if not, display the message instead of opening
the form:

stDocName = "Customer"
stLinkCriteria = "[Phone] LIKE " & "'" & Me![FindCustomer] & "'"
If IsNull( DLookup( "CustomerID", "tblCustomer", stLinkCriteria ) ) Then
MsgBox "No Matches were found"
Else
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

(Replace "tblCustomer" and "CustomerID" respectively with the name of your
customer table and the name of its primary key)
 

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