Syntax error in VBA query expression in Access - Stumped - sql

I'm getting a message that there is a syntax error in a VBA query expression I have on a button in Access. Unfortunately, I just cannot figure out what the syntax error is. I'm pretty sure I'm using everything correctly. I'm sending this expression as the WHERE clause to a report being generated. The screenshot is below. Can anyone help?
The two strings that read "tk" are put into the expression from a couple input boxes.
My expression in text:
cond = "(((StrComp(""" & lower & """,Left([Location],2)))<=0) And ((StrComp(""" & upper & """,Left([Location],2)))>=0));"
stDocName = "rptMstrEquipListRange"
DoCmd.OpenReport stDocName, acPreview, , cond
According to www.balancebraces.com, the parentheses aren't the problem:

It should work fine if you remove the ; at the end. (Tested)

Related

Trap Syntax Error During Compile

I am setting a list box row source conditionally based on a value:
If MyValue = "" then
   Me.MyList.RowSource = "SELECT...
   FROM...
   RIGHT JOIN...
   GROUP BY...
   ORDER BY...
Else
   Me.MyList.RowSource = "SELECT...
   FROM...
   RIGHT JOIN...
   GROUP BY...
   HAVING MyValue
   ORDER BY...
End if
Where MyValue is some criteria for the HAVING part.
This works fine as long as MyValue is not "" (blank). However during compile, I get a Microsoft Access error: "Syntax error on HAVING clause." During runtime, the "else" with HAVING does not occur so no error there. How can I avoid or trap the "Syntax error on HAVING clause." during compile? The usual "On error..." does not trap during compile.
Thanks.
The compiler can't go so far as to insert your code into the listbox field and check the outcome. But, some good practices will help you trouble shoot this.
Dim strRowSourceSQL as String
strRowSourceSQL = "SELECT ... FROM ... RIGHT JOIN ..."
If MyValue = "" then
strRowSourceSQL = strRowSourceSQL & "MORE SQL HERE"
else
strRowSourceSQL = strRowSourceSQL & "DIFFERENT SQL HERE"
end if
Me.MyList.RowSource = strRowSourceSQL
This arrangement will let you inspect strRowSourceSQL. Set a break point and...
Debug.Print strRowSourceSQL
Once you have that, drop into the Query editor and fix what's wrong. Also, follow these principles.
Thanks for the suggestions, but I found the problem.
It turned out that I had a left-over query definition in the Property Sheet for the list box that had the offending HAVING clause. When I removed the definition from the Property Sheet, the form opened without the annoying syntax error. The vba code I included in my original post was just fine.

MS Access 2010 SQL Query Syntax Error due to comma in field

I've been having issues with this code I've written to update a field (txtUOM) in MS Access 2010.
Private Sub cboSelector_Change()
Dim myItemCode As String
Dim QfindUOM As String
myItemCode = Me.cboSelector.Column(0)
QfindUOM = ("SELECT item_details.uom from item_details
where item_details.itemCode=" & myItemCode & " end")
DoCmd.RunSQL (QfindUOM)
End Sub
At this point, I haven't gotten around to actually updating txtUOM since the SQL won't even run. The syntax error hits me,because myItemCode has commas inside (i.e. 822,60 or 6,01) and I can't get rid of this because the database I've been asked to work with has this as the 'item numbers' of the inventory. Any suggestions on how I might be able to manage this? I've tried adding parenthesis and apostrophes but they don't seem to help. For example, in the case of item 6,09:
Run-time error '3075':
Syntax error (comma) in query expression 'item_details.itemCode=6,09 end'.
There are also some items with itemCodes that don't have commas inside (i.e. 6909), and the error I get is as follows:
Run-time error '3075':
Syntax error (missing operator) in query expression 'item_details.itemCode=6909 end'.
I'm guessing there's something going terribly wrong in my code and would really appreciate any suggestions you can give!
Thanks for taking the time to read this question!
Add single quotes:
QfindUOM = ("SELECT item_details.uom from item_details
where item_details.itemCode='" & myItemCode & "' end")
or Double, I forget with access:
QfindUOM = ("SELECT item_details.uom from item_details
where item_details.itemCode=""" & myItemCode & """ end")

MS Access VBA Dlookup on Yes/No field

I can't for the life of me work out what is wrong with this, but I'm not an Access/VBA developer normally..
I have a database table with about 20 fields, one of which is a Yes/No field. I want to extract the Yes/No value using DLookup, however am receiving the following error:
Run-time error '3075':
Syntax error (missing operator) in query expression 'Enabled'.
The code I am using it:
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
Enabled is a Yes/No field
ID is a String field.
The above DLookup works absolutely fine for returning String values for other fields, so the last parameter with the search query, and the table field, should be fine. It is simply complaining about the return field ('Enabled') thinking it is a query.
The MsgBox element is also irrelevant, as I have tried assigning the result to an Integer and to a Boolean, and it's not complaining of a type mismatch which I would expect if this were the problematic part.
Any thoughts?
You stated that ID is a string field. If that is the case, try changing the DLookup to...
DLookup("[Enabled]", "Numbers", "ID = " & Chr(34) & Me.cbxNumber.Value & Chr(34))
If ID is a Long, then use this string...
DLookup("[Enabled]", "Numbers", "ID = " & Me.cbxNumber.Value)
Your code works fine for me:
Table:
Form:
Code:
Private Sub Command30_Click()
MsgBox (DLookup("Enabled", "Numbers", "ID = " & Me.cbxNumber.Value & ""))
End Sub
The messagebox displays 0 or -1 as required. Things to check:
Is your code in the forms module? Otherwise Me.cbxNumber.Value won't return anything.
What do you get if you run
debug.print Me.cbxNumber
from the OnClick of a button on the form?

Syntax error (missing operator) in query expression - SQL to pull 1 ID back in resultset

please find below my SQL statement, on line 20 the error is apparently;
https://gist.github.com/815c4dd5655e79581a1a
current error im getting is
Microsoft Access Database Engine error '80040e14'
Syntax error (missing operator) in query expression 'sea_Reference=A01AND sea_TheID=5'.
/STUDENT/s0191958/PART2/bookingprocess1.asp, line 22
all the variables being requested and stuff, are digits.
Ive been removing and adding quotes all night and im sick of it!
Put a space before the AND and put the data for sea_Reference in quotes.
Change: 'sea_Reference=A01AND sea_TheID=5'
to 'sea_Reference='A01' AND sea_TheID=5'
here's the altered code
SeatSetRs="SELECT sea_ID FROM seat WHERE sea_Reference='" & request("firstSeat") & "' AND sea_TheID=" & request("the_ID") & ""

How to display value from cell in vba code?

Row_No = 5
MsgBox Range.("A & Row_No").value
i have above code but it gives me error 1004..please help me with this.
Just try this
MsgBox Range.("A" & Row_No).Value
or this
MsgBox Range.("A" & Row_No).Text
or this
MsgBox Cells(1,"C")
Problem with the code you used is nothing but placing of & and " in wrong place.
Hope this helps.
When doing concatenation, keep in mind that strings will be in quotes and variables will not -- think of the quotes as telling the compiler to interpret what is between them as literal text. A good IDE will usually indicate this via syntax highlighting.
So, in your code, the Range() method is being passed the string A & Row_No instead of A5 -- so it errors out.