Trap Syntax Error During Compile - vba

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.

Related

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 in VBA query expression in Access - Stumped

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)

Use toggle buttons to update a record in Access

Hopefully not too difficult question but I cannot figure this out and have been unable to find anything searching the forums.
I want to convert the output of my toggle boxes from 1,2,3,4,5 to the text each button displays.
Couldn't find any setting on the toggle boxes properties themselves so decided I would have to write a macro/vba to do it from the table but as it's quite new to me I am struggling on syntax.
I have tried doing this on the inbuilt data macro mainly, but also tried it via a query and vba and still couldn't figure it out.
[don't have any reputation yet so have not been allowed to post pics of my macro attmept]
please help! Any solution using vba, data macro or a query would be great
EDIT
To be specific rather than a message box I want to update field1 in my table "Major Equipment" based off the option group selection this is my latest attempt but still not sure how to reference the option group. Do I need to set grp equal to the option group and if so how? Is it something like forms!myform!optiongroup ?
Option Compare Database
Function MyFunc(grp As OptionGroup)
Dim rcrdset As Recordset
set grp =
Set rcrdset = Application.CurrentDb.OpenRecordset("Major Equipment", dbOpenDynaset)
Select Case grp.Value
Case 1
With rcrdset
.AddNew
![Field1] = "Not Reviewed"
Case 2
.AddNew
![Field1] = "Reviewed"
Case Else
MsgBox "Error"
End Select
End Function
Also just realised since these toggle buttons will be updated by the user and so I probably need an update rather than addnew?
http://i59.tinypic.com/2ym8wet.jpg
Your buttons are part of an Option Group. That is what you must reference. Below is a snippet from my net search.
From the AfterUpdate() event of your Option Group:
Call MyFunc(Me.MyGroup)
... which will use Select Case to evaluate:
Function MyFunc(grp As OptionGroup)
Select case grp.Value
Case 1
MsgBox "Option value is 1."
Case 2
MsgBox "Option value is 2."
Case Else
MsgBox "Huh?"
End Select
End Function
If you are entirely new to VBA, there are a half-dozen things to learn here, but they will serve you well. VBA provides a bit less-friendly-looking start than a macro, but I can tell you have more adventures ahead and I suggest you skip macros. For most needs, VBA will serve you better; and it's much easier to trouble shoot or provide details when you need advice.
To convert this to a useful function, you will fill a string variable rather than raising a message box. Then you can use the string variable to do something like run an update query. Your latest edit suggests you will go for something like:
strSQL = "UPDATE [Major Equipment] " _
& "SET Field1='" & strUserSelection & "' " _
& "WHERE MyID=" & lngThisRecord
DoCmd.RunSQL strSQL
Your last edit proposes using a DAO recordset. I think you might be fine with the humble DoCmd. Less to learn. You can hammer out a prototype of the query in good ol' Access; then switch to SQL View and paste the query into your VBA module. Insert variables as seen above, taking care with the quote marks. If it doesn't work, use Debug.Print to grab the value for strSQL and take that back to good ol' Access where you can poke at it into shape; use your findings to improve the VBA.

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.