Syntax Error Missing Operator VBA - ms-access

I have a Dlookup in Access 2010 that's supposed to pull a value from a query table that counts the number of checkboxes untoggled. When I run it, it gives me a 'Missing operator in query expression' here.
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo =]" & Me.ProjNo & "'")
I can't quite figure out what's wrong.

If ProjNo is numeric, the correct code is:
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo] =" & Me.ProjNo )
If ProjNo is text, the code should be:
Countboxes = DLookup("Expr1", "qryCountUntoggled", "[ProjNo] ='" & Me.ProjNo & "'")

Related

MS access update query issue with date range

I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.
UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.
As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"

ms-Access is removing leading zeros from a string

In the below code, the value of skuSet.Fields(0), which is a text field in the database, is "000000002".
If skuSet.RecordCount = 0 Then
sql = "SELECT Stock_Code FROM Stock WHERE Stock_Code = '" & stkLine.StockCode & "'"
Set skuSet = dbStock.OpenRecordset(sql, dbOpenDynaset)
End If
sql = "SELECT * FROM PickedByPicco WHERE OrderID = " & stkLine.OrderId & " AND StockCode = '" & skuSet.Fields(0) & "'"
Set SOPSLineSet = dbSOPS.OpenRecordset(sql, dbOpenDynaset)
If SOPSLineSet.RecordCount = 0 Then
sql = "INSERT INTO PickedByPicco(OrderID, StockCode, Qty, ScannedBy, ProcessedBy, processed) VALUES(" & stkLine.OrderId & _
", " & skuSet.Fields(0) & ", " & stkLine.Quantity & ", '" & stkLine.UserId & "', '', False)"
dbSOPS.Execute (sql)
When I step through the code, the value in skuSet.Fields(0) is still the same, as expected.
When the query has been executed, I check the PickedByPicco table, and the StockCode field shows just a value of 2 instead, and it's removed all of the leading 0's.
Why would it be doing this and how can I stop it?
That's because your INSERT statement looks like this:
INSERT INTO PickedByPicco (...) VALUES (..., 000000002, ...)
when it should look like this:
INSERT INTO PickedByPicco (...) VALUES (..., '000000002', ...)
(Note the quotes around your value.)
Normally, I'd tell you to use parameterized SQL, because SQL creation by string concatenation is evil, but, unfortunately, DAO's Database.Execute does not support that. So, the next best thing is to properly escape strings. To do that:
put single quotes around your value and
escape any single quotes occurring in your value:
I.e. this line
", " & skuSet.Fields(0) & ", " & ...
becomes
", '" & Replace(skuSet.Fields(0), "'", "''") & "', " & ...
While you are at it, be sure to replace single quotes in your other strings as well! Currently, a UserId of O'Brien would break your SQL, and other values might do worse.

MS Access DLOOKUP with text and nested DLOOKUP for criteria

I have a combobox on a form that contains search terms. The user chooses a search term and this looks up to a table containing the number X. The RVU (a number) of X is looked up in another table given the category is equal to the string 'PHYS'. I was using nested DLOOKUP statements to look up the number X and then use that number X and the string criteria to look up the RVU. Here's my code:
FH_array(0) = Val(Nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP]= " & Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))), ""))
I wasn't having luck so I broke it down to debug:
a = Val(Nz(DLookup("[FORES]", "[IP Number Xwalk]", "[Reference Name] = '" & Me.Ref_Name & "'"), 0))
Debug.Print "a:"; a 'returns value 279
aa = Val(nz(DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')))
Debug.Print "aa:"; aa
I'm getting a syntax error on the line for variable aa. if I changed the code from
aa = DLookup("[RVU]", "[FORES IP Picker]", "[IP] = " & a & " and [Cost Category] = 'PHYS')
to
aa = DLookup("[RVU]", "[FORES IP Picker]", "[Cost Category] = 'PHYS'" And "[IP] = " & a)
I get a run-time error 13 type mismatch
All the variables are declared as variant and called properly. The array FH_array is sized correctly. I copied this code from another database that does the same type of nested DLOOKUP but it has only one criteria and therefore works. I can't figure out what syntax I'm missing or where the type mismatch is to get it to work.
You need a single valid string as the DLookup criteria option. Use the Immediate window to examine what you have for the criteria in the final DLookup example.
Debug.Print "[Cost Category] = 'PHYS'" And "[IP] = " & a
That is actually a "logical conjunction" of two strings. (That will make more sense if you review the Access help topic for the And Operator.)
And since you're using And with two string expressions, Access complains about type mismatch, the same as it does with this simpler example:
Debug.Print "a" And "b"
So you need to create a single valid string for the criteria option ...
a = 279
Debug.Print "[Cost Category] = 'PHYS' And [IP] = " & a
[Cost Category] = 'PHYS' And [IP] = 279
Translating that back to the last DLookup in your question ...
Dim strCriteria As String
strCriteria = "[Cost Category] = 'PHYS' And [IP] = " & a
Debug.Print strCriteria ' <- just to make sure you got what you need
aa = DLookup("[RVU]", "[FORES IP Picker]", strCriteria)

Not able to read from access table properly

I have encountering a very silly problem. The requirement is to stop the duplicate entry in access table. I am using Vb6. But each time I try I am encountering syntax error.
My code
My flexgrid is populated and refreshed. I am able to insert and select data in another table in same database. But this one is failing
sql_txt1 = "Select SL_No from SpAIReport where DOM ='" & Format(Now, "mm/dd/yyyy") & _
"' and AC-REG ='" & msgDisFlex.TextMatrix(i, 4) & "' and Flt No = '" & msgDisFlex.TextMatrix(i, 6) & "'"
Set rs1 = db.OpenRecordset(sql_txt1)
I am able to update this table, but multiple time same data are getting populated.
The access table structure with the entries are given below
Date_OF_Fly Flt No GMT Weight Airtime Station DOM Data_hrs Filename
7/3/2000 11 03:45:01 5 01:23:40 XXX 01/25/2014 120:10:15 ABCD
Plus, after saving if I want to access it through recordset then it is showing NULL.
The code is:
sql_txt = "select * from SpAIReport where DOM='" & dateDailyReport & "'"
Set rs = db.OpenRecordset(sql_txt)
The dateDailyReport value is 01/25/2014. This value is present in database. Still this query is not working.
Please help.
You probably want to put 'Flt No' and 'AC-REG' in square brackets otherwise they look like two field names:
"' and [AC-REG] ='" & msgDisFlex.TextMatrix(i, 4) & "' and [Flt No] = '" &

syntax error, I don't see one :S

Apparently i'm getting a syntax error, but I don't see any issues with my code!
if rs.EOF then
'User doesn't exist, create the record
query = currentDate&",'"
query = query + password & "','"
query = query + "user', '"
query = query + email & "', '"
query = query + fname & "', '"
query = query + sname & "'"
handle = add_to_database("users","jdate,password,perms,email,fname,sname",query)
response.write handle
else
response.write "Error: User already exists, please use a different email address"
end if
Here is the add_to_database function
function add_to_database(where,column,values)
'Create query
sql = "INSERT INTO " & where & " (" & column & ") VALUES (" & values & ")"
on error resume next
'Add images to database
conn.Execute sql,recaffected
if err<>0 then
add_to_database = err.description &":"&sql&"<br />"
end if
'Destroy connection
sql = ""
end function
This is the error that is written to the page
Syntax error in INSERT INTO statement.:INSERT INTO users (jdate,password,perms,email,fname,sname) VALUES (#2013/8/2#,'af453d19feb2520c8c0d30fb39ebd211','user', 'martynleeba#gmail.com', 'Martyn', 'Ball')
The users table includes a field named password. But password is a reserved word, so can confuse the db engine when it finds it in a query. Rename that field if possible. If you must keep the name as is, enclosed it in square brackets to signal the db engine it is an object name.
handle = add_to_database("users","jdate,[password],perms,email,fname,sname",query)