Type Mismatch error with "Like"

LaurieW

Registered User.
Local time
Today, 03:51
Joined
May 9, 2002
Messages
99
I have code on a button in the On Click Event that fills a report title and gets data from a table. I get a type mismatch error on this line and believe it is having a problem with the "Like" operator. I took this code directly from the SQL statement from a query...why does it not work? How do I use something similar to "Like" in this statement"

DoCmd.OpenReport stDocName, acPreview, , "((tblPayee.FirstName) Like " * " & [Forms]![frmReportMenu]![txtSearchFor] & " * "))"

Here is all my code, in case you want a look. I have done this same thing before but only using things like = and > and <...never trying "Like". Hope someone can help. Thanks, Laurie

Private Sub cmdTenantSearch_Click()
On Error GoTo Err_cmdTenantSearch_Click

Dim stDocName As String

Me.Controls("txtSearchFilter") = Me.Controls("cboSearchFilter")
stDocName = "rptTenantSearch"
If cboSearchFilter = "Address" Then
Me.Controls("txtReportTitle") = "Tenant Search For " & [Forms]![frmReportMenu]![txtSearchFor] & " In Address"
' DoCmd.OpenReport stDocName, acPreview, , "(((tblPayee.Address1) Like " * " & [Forms]![frmReportMenu]![txtSearchFor] & " * ")) OR (((tblPayee.Address2) Like " * " & [Forms]![frmReportMenu]![txtSearchFor] & " * "))"
ElseIf cboSearchFilter = "First Name" Then
Me.Controls("txtReportTitle") = "Tenant Search For " & [Forms]![frmReportMenu]![txtSearchFor] & " In First Name"
DoCmd.OpenReport stDocName, acPreview, , "((tblPayee.FirstName) = [Forms]![frmReportMenu]![txtSearchFor])"
' DoCmd.OpenReport stDocName, acPreview, , "((tblPayee.FirstName) Like " * " & [Forms]![frmReportMenu]![txtSearchFor] & " * "))"
ElseIf cboSearchFilter = "Last Name" Then
Me.Controls("txtReportTitle") = "Tenant Search For " & [Forms]![frmReportMenu]![txtSearchFor] & " In Last Name"
' DoCmd.OpenReport stDocName, acPreview, , "((tblPayee.LastName) Like " * " & [Forms]![frmReportMenu]![txtSearchFor] & " * "))"
End If

Exit_cmdTenantSearch_Click:
Exit Sub

Err_cmdTenantSearch_Click:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox "Error No: " & Err.Description & "; Description: " & Err.Description
Resume Exit_cmdTenantSearch_Click
End If
End Sub
 
Never mind, got it!

I looked in SQL Books Online and found I needed to use the % character instead of *, so this works:

DoCmd.OpenReport stDocName, acPreview, , "((tblPayee.FirstName) Like '%' & [Forms]![frmReportMenu]![txtSearchFor]& '%')"

Thanks anyway!
Laurie
 
Glad we could help! :D
 

Users who are viewing this thread

Back
Top Bottom