Compile Error (1 Viewer)

KerryDee

Registered User.
Local time
Today, 04:12
Joined
Nov 24, 2016
Messages
36
Hi I am getting a Compile Error with the following code with the highlight strsearch = The code is below:

Option Compare Database
Option Explicit



Code:
Private Sub btnSearchCarlsbad_Click()
Dim strString As String
Dim strText As String
strText = Me.txtSearch.Value
strsearch = "Select* from Carlsbad where ((LineItemCode like ""*" & strText & "*"") or(FirstName like ""*" & strText & "*"")or(ServiceAddress like ""*" & strText & "*"") or(Location like ""*" & strText & "*"") or (LastName like ""*" & strText & "*""))"
Me.RecordSource = strsearch
End Sub

Any help to get rid of the error would be gratefully appreciated. Thank you very much
 

MarkK

bit cruncher
Local time
Today, 04:12
Joined
Mar 17, 2004
Messages
8,178
Is there an error number or message?
 

KerryDee

Registered User.
Local time
Today, 04:12
Joined
Nov 24, 2016
Messages
36
Hi the error does not give me a number, it says the following:

Compile error:

Variable not defined


Thank you.
 

MarkK

bit cruncher
Local time
Today, 04:12
Joined
Mar 17, 2004
Messages
8,178
The variable strsearch is not defined. You need to "Dim" it as you've done with strString and strText.
 

KerryDee

Registered User.
Local time
Today, 04:12
Joined
Nov 24, 2016
Messages
36
Thank you very much. I am a novice at VBA what but exactly should the line code say? I got this code off the internet.


Thanks again.
 

MarkK

bit cruncher
Local time
Today, 04:12
Joined
Mar 17, 2004
Messages
8,178
See the stuff in blue...
Code:
Private Sub btnSearchCarlsbad_Click()
   Dim [COLOR="Blue"]strString[/COLOR] As String
   Dim strText As String

   strText = Me.txtSearch.Value
   [COLOR="Blue"]strString[/COLOR] = "Select* from Carlsbad where ((LineItemCode like ""*" & strText & "*"") or(FirstName like ""*" & strText & "*"")or(ServiceAddress like ""*" & strText & "*"") or(Location like ""*" & strText & "*"") or (LastName like ""*" & strText & "*""))"
   Me.RecordSource = [COLOR="Blue"]strString[/COLOR]
End Sub
 

Beetle

Duly Registered Boozer
Local time
Today, 05:12
Joined
Apr 30, 2011
Messages
1,808
See the example below. The line in red is what you need to add to your code.

Code:
Private Sub btnSearchCarlsbad_Click()
Dim strString As String
Dim strText As String
[COLOR="Red"]Dim strSearch As String[/COLOR]
strText = Me.txtSearch.Value
strsearch = "Select* from Carlsbad where ((LineItemCode like ""*" & strText & "*"") or(FirstName like ""*" & strText & "*"")or(ServiceAddress like ""*" & strText & "*"") or(Location like ""*" & strText & "*"") or (LastName like ""*" & strText & "*""))"
Me.RecordSource = strsearch
End Sub
 

Beetle

Duly Registered Boozer
Local time
Today, 05:12
Joined
Apr 30, 2011
Messages
1,808
Sorry, the slightly different answers above might just confuse the issue for you, do what MarkK suggested.
 

KerryDee

Registered User.
Local time
Today, 04:12
Joined
Nov 24, 2016
Messages
36
Thanks MarkK and Beetle for your help. It works perfectly. I am also going to start a new thread on figuring out how to attach VBA to a Ribbon. Thanks again for your tremendous help.
 

Users who are viewing this thread

Top Bottom