|
-
Dec 10th, 2009, 10:52 AM
#1
Thread Starter
Lively Member
Syntax run-time error
I get error Syntax error (missing operator) in query expression 'Name LIKE '%V%@'' on this code
Code:
Select Case cmbOperation(0).ListIndex
Case 0
strFilter = strFilter & " LIKE '%" & txtFilter(0).Text & "%'"
Case 1
strFilter = strFilter & " = '" & txtFilter(0).Text & "'"
Case 2
strFilter = strFilter & " <> '" & txtFilter(0).Text & "'"
Case 3
strFilter = strFilter & " > '" & txtFilter(0).Text & "'"
Case 4
strFilter = strFilter & " >= '" & txtFilter(0).Text & "'"
Case 5
strFilter = strFilter & " < '" & txtFilter(0).Text & "'"
Case 6
strFilter = strFilter & " <= '" & txtFilter(0).Text & "'"
End Select
ConnectToDB
Set mobjRst = New ADODB.Recordset
'mobjRst.Open "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
strSQL = "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
mobjCmd.CommandText = strSQL
Set mobjRst = mobjCmd.Execute
Debug line
Code:
Set mobjRst = mobjCmd.Execute
can anyone help?
-
Dec 10th, 2009, 10:57 AM
#2
Re: Syntax run-time error
Instead of this:
Code:
mobjCmd.CommandText = strSQL
Set mobjRst = mobjCmd.Execute
Try this:
Code:
Set mobjRst = mobjCmd.Execute(strSQL)
-
Dec 10th, 2009, 11:06 AM
#3
Thread Starter
Lively Member
Re: Syntax run-time error
Error Command text was not set for the command on that line
-
Dec 10th, 2009, 11:40 AM
#4
Re: Syntax run-time error
before you use the strSQL ... print it to the debug window or to a message box and make sure that the resulting SQL is in fact valid... I have a sneaky suspicion that you aren't getting the SQL you think you are. Also, what kind of database are you running against?
-tg
-
Dec 10th, 2009, 11:48 AM
#5
Re: Syntax run-time error
And you don't Execute a SELECT query with the command object, you use the recordset object to OPEN it.
-
Dec 10th, 2009, 11:53 AM
#6
Thread Starter
Lively Member
Re: Syntax run-time error
Msgbox strFilter results in
LIKE '%V%'
when I remove the command object and use
Code:
mobjRst.Open "SELECT * from Customer where " & Me.cmbFields.Text & strFilter
I get error "The connection cannot be used to perform this operation. It's either closed or invalid in this context."
-
Dec 10th, 2009, 11:56 AM
#7
Thread Starter
Lively Member
Re: Syntax run-time error
sorry forgot to mention i'm running it on an MS Access database
-
Dec 10th, 2009, 12:35 PM
#8
Thread Starter
Lively Member
Re: Syntax run-time error
corrected the open line by adding the open connection to the end.
Now i'm getting the syntax error again.
Syntax error (missing operator) in query expression 'Name LIKE '%V%' on this code
-
Dec 10th, 2009, 12:52 PM
#9
Re: Syntax run-time error
Doesn't Access use * vs % for wild cards? I forget.
Anyway, Name is one of those fields that are kinda reserved, correct? Try using [Name] in your query instead. If that still generates an error, please post the entire sql statement so that we may have a look at it.
-
Dec 10th, 2009, 12:56 PM
#10
Thread Starter
Lively Member
Re: Syntax run-time error
testing on another field looks like your correct LaVolpe it doesn't like Name, i'll do some more tests to make sure it's working ok now
-
Dec 11th, 2009, 06:50 AM
#11
Thread Starter
Lively Member
Re: Syntax run-time error
does anyone know how I can use the word Name in a search without getting the syntax error?
-
Dec 11th, 2009, 08:22 AM
#12
Re: Syntax run-time error
The best thing to do is rename the field to something else - because even tho there is a fix in this situation, there are situations where you cannot fix it at all.
I would recommend changing it to something like CustomerName
For information on other unsafe field/table names, see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|