Need to produce an error message

  • Thread starter Thread starter Ment@lBl0ck
  • Start date Start date
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
 
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)
 
Back
Top