Syntax error (missing operator) in query expression - VBA - sql

This is my code:
M_BKID = DMax("BK_ID", "BookingMain")
FSQL = " UPDATE Q_HrsToBeRefund_Writeable " & _
" SET BD_ToBeRefund = False, BD_Refunded = True, BD_RefundedRef = " & M_BKID & " " & _
" ORDER BY BD_Dt DESC LIMIT " & M_Refunded & " "
Debug.Print FSQL ' ********************************
DoCmd.RunSQL FSQL
I get an error
Syntax error (missing operator) in query expression '184 ORDER BY BD_Dt DESC Limit 3
Can anyone help me solve this?

MS Access definitely doesn't support LIMIT in UPDATE. I don't think it supports TOP either. But you can do:
UPDATE Q_HrsToBeRefund_Writeable as
SET BD_ToBeRefund = False,
BD_Refunded = True,
BD_RefundedRef = ?
WHERE <primary key> IN (SELECT TOP (M_Refunded) <primary key>
FROM Q_HrsToBeRefund_Writeable
ORDER BY BD_Dt DESC
);
Note:
This assumes your table has a primary key.
Note the use of ?. That is an indication that you should use a parameter for this value, rather than munging the query string.
I'm not sure if the number of rows can be passed in as a parameter.

Related

Executing a SQL query from vba results with nothing

Cheers,
I am trying to send a simple SQL query, but I get no results in my recordset as if it simply was not executed.
Just after, I am executing a different query, which results correctly.
When running the same query from SMSS, do brings up the desired results.
Following is the code:
If CheckLogsFromRestartDay = True Then
'This line does NOT seem to work. Nothing is changed in the Recordset
Set objMyRecordset = objMyConn.Execute( _
" SELECT top 1 cast(tValue AS date) AS RestartDate " & _
" FROM settings (nolock) WHERE tkey = 'EngineStartTime' ")
'This line DOES provide an answer correctly
Set objMyRecordset = objMyConn.Execute( _
" SELECT * FROM " & LogTable & " (nolock) where Webpage like 'Engines%'" & _
" ORDER BY id desc")
sTodayMMDD = objMyRecordset!RestartDate
Else
sTodayMMDD = Format(Date, "yyyy-mm-dd")
End If

Access VBA SQL syntax, errors 3061 and 3075

I am new to Access VBA, I am using SQL statement to get data for creating excel file. The query uses a combo box [FBrpt_FbReason_Cbo] value as a criteria for the data. This is the first code I modified from the Access Query SQL mode:
SQL = "SELECT FB_Register.FB_Date_Received AS [Date received], FB_Register.FB_Resp_date AS [Response date], FB_Register.FB_Title AS [Feedback title], FB_Register.FB_Reason AS Reason " & _
"FROM FB_Register " & _
"WHERE (((FB_Register.FB_Reason)=[Forms]![Feedback_Reports]![FBrpt_FbReason_Cbo])) " & _
"ORDER BY FB_Register.FB_Date_Received DESC; "
It returns the error - Error number: 3061 = Too few parameters. Expected 1.
I then modified the Where line to:
"WHERE FB_Register.FB_Reason = " & [Forms]![Feedback_Reports]![FBrpt_FbReason_Cbo] & _
" ORDER BY FB_Register.FB_Date_Received DESC"
This returns the error – Error Number: 3075= Syntax error (missing operator) in query expression ‘FB_Register.FB_Reason = Positive feedback’.
In this case I have "Positive feedback" selected in the combo box. This code ‘FB_Register.FB_Reason = Positive feedback’ looks to me how it should be. What am I missing?
I figured it out, I needed the extra quotes - I believe - to turn the value into a string, even though its already a string
"WHERE FB_Register.FB_Reason = '" & [Forms]![Feedback_Reports]![FBrpt_FbReason_Cbo] & _ "' ORDER BY FB_Register.FB_Date_Received DESC"

Access 2003: count(*) produces syntax error

I am using ms-access and for some reason, the code here produces a Syntax error which I dont understand.
UPDATE Korrekturentlastung
SET Schueler = SELECT COUNT(*)
FROM Korrekturentlastung
WHERE
Korrekturentlastung_Kurs.Kuerzel = Korrekturentlastung.Kuerzel
AND Korrekturentlastung_Kurs.Klasse = Korrekturentlastung.Klasse
AND Korrekturentlastung_Kurs.Fach = Korrekturentlastung.Fach
AND Korrekturentlastung_Kurs.Kursart = Korrekturentlastung.Kursart
The error is:
Syntax error. in query expression 'SELECT COUNT(*)
FROM Korrekturentlastung'
Whilst #Gordon Linoff has correctly identified the syntax error in your query, if you were to run the query in MS Access, you'll likely receive the familiar response:
Operation must use an updateable query.
This arises as a result of a restriction of the JET database engined used by MS Access whereby no part of an update query can use aggregation. In your case, this arises as a result of the use of count(*).
One possible alternative is to use the DCount domain aggregate function, which is evaluated separately from the evaluation of the main query and therefore retains the "updateability" of the query.
update korrekturentlastung
set schueler =
dcount
(
"*",
"Korrekturentlastung",
"Kuerzel = " & Korrekturentlastung.Kuerzel & " and "
"Klasse = " & Korrekturentlastung.Klasse & " and "
"Fach = " & Korrekturentlastung.Fach & " and "
"Kursart = " & Korrekturentlastung.Kursart
)
Note that if your fields are text fields, you'll also need to surround the above values with single or double quotes, e.g.:
update korrekturentlastung
set schueler =
dcount
(
"*",
"Korrekturentlastung",
"Kuerzel = '" & Korrekturentlastung.Kuerzel & "' and "
"Klasse = '" & Korrekturentlastung.Klasse & "' and "
"Fach = '" & Korrekturentlastung.Fach & "' and "
"Kursart = '" & Korrekturentlastung.Kursart & "'"
)
Subqueries need their own parentheses:
UPDATE Korrekturentlastung
SET Schueler = (SELECT COUNT(*)
FROM Korrekturentlastung
WHERE Korrekturentlastung_Kurs.Kuerzel = Korrekturentlastung.Kuerzel
AND Korrekturentlastung_Kurs.Klasse = Korrekturentlastung.Klasse
AND Korrekturentlastung_Kurs.Fach = Korrekturentlastung.Fach
AND Korrekturentlastung_Kurs.Kursart = Korrekturentlastung.Kursart
);

Access Update QueryDef Parameters not found

I've two queries I'm trying to create with QueryDef.
This insert query works perfectly and I can assign the parameters:
Dim qryAddPackage As String: qryAddPackage = "" & _
"INSERT INTO `RMF_tblPackages` " & _
"(`SystemID`, `DoD`, `DateCreated`, `LastModified`, `LastUpdatedBy`) " & _
"VALUES ([SystemID], [DoD], NOW(), NOW(), [UID])"
With CurrentDb.CreateQueryDef(vbNullString, qryAddPackage)
.Parameters!SystemID.Value = cbbSystems.Value
.Parameters!DoD.Value = Me.txtDoD.Value
.Parameters!UID.Value = strGetUserID()
.Execute
End With
However, in this update query, I am only able to access the Parameter UID in the WHERE statement:
Dim qryUpdatePkg As String: qryUpdatePkg = "" & _
"UPDATE `RMF_tblPackages` " & _
"SET " & _
"`SystemID` = [SystemID], `DoD` = [DoD], `LastModified` = NOW(), `LastUpdatedBy` = [UID] " & _
" WHERE `PackageID` = [PackageID]"
With CurrentDb.CreateQueryDef(vbNullString, qryUpdatePkg)
.Parameters("SystemID").Value = cbbSystems.Value
.Parameters!DoD.Value = Me.txtDoD.Value
.Parameters!UID.Value = strGetUserID()
.Parameters!PackageID.Value = Me.lbxPackages.Value
.Execute dbSeeChanges
End With
In every other line trying to assign a parameter, it just says that the parameter's name is not in the collection.
I've tried assigning them like this:
.Parameters("PackageID").Value = Me.lbxPackages.Value
As well, but that doesn't work either.
I tried just running the update query with some data and it worked fine so the query works.
.Params.count returns 1 as you would expect given it's only recognizing one param.
Any other ideas as to why the parameters aren't being recognized by that update query?
Use [ ] for quoting identifiers. In Access the backtick is not valid (unlike MySQL). Also column names and parameter names use the same quoting characters, so you must choose different names for the parameters. Quoting identifiers is only necessary if the names collide with a reserved word or function name (like Date, From, By, Order etc).
Dim qryUpdatePkg As String
qryUpdatePkg = "UPDATE RMF_tblPackages " & _
"SET " & _
"SystemID = prmSystemID, DoD = prmDoD, LastModified = Now(), LastUpdatedBy = prmUID " & _
" WHERE PackageID = prmPackageID"
With CurrentDb.CreateQueryDef(vbNullString, qryUpdatePkg)
.Parameters("prmSystemID").Value = cbbSystems.Value
.Parameters!prmDoD.Value = Me.txtDoD.Value
.Parameters!prmUID.Value = strGetUserID()
.Parameters!prmPackageID.Value = Me.lbxPackages.Value
.Execute dbSeeChanges
End With
Do the same changes in the first query.

Query database and check not equal to

I have a data set in excel that I am querying info in a database using that data set. However, when checking against the database I get the "too few parameters. Expected 1" error.
Query:
Set rex = db.OpenRecordset("SELECT * FROM [CallQuality] WHERE ([Racf] = '" & sRacf & "') AND ([DateChecked] = #" & sDateChecked & "#) AND ([Overall] <> '" & sOverall & "') ;")
Literal:
SELECT * FROM [CallQuality] WHERE ([Racf] = 'SMITHJ') AND ([DateChecked] = #2017/05/17#) AND ([Overall] <> 'Development Required') ;
I have tried without the brackets and using != instead of <>. I am sure it is something simple I am missing.
Edit:
Error in this section:
Set rex = db.OpenRecordset("SELECT * FROM [CallQuality] WHERE ([Overall] <> '" & sOverall & "')")
Edit2:
The field name was wrong. Sorry guys! Not sure why it didnt give me the name error when it didnt find the field.
Thank you for your help.
use a + instead of & in the query.
So use this:
Set rex = db.OpenRecordset("SELECT * FROM [CallQuality] WHERE ([Racf] = '" + sRacf + "') AND ([DateChecked] = #" + sDateChecked + "#) AND ([Overall] <> '" + sOverall + "') ;");
If you look in the Error List you should get:
Error CS0019 Operator '&' cannot be applied to operands of type
'string' and 'string'
I can see why you put the & instead. Simple mistake :-)
It was an issue with the field name in the query.