military donut
asked on
VBA Delete sql "Too Few Parameters"
hello,
Not sure what I am missing here: Says "Too Few Parameters. Expected 1"
Not sure what I am missing here: Says "Too Few Parameters. Expected 1"
Dim strSql As String
strSql = "DELETE tblMakeVehicle.Make " & vbCrLf & _
"FROM tblMakeVehicle " & vbCrLf & _
"WHERE (((tblMakeVehicle.Make)=[forms]![frmMakeVehicle_Edit]![frmMakeVehicle_Edit_subform].[Form]![cboMakeEdit]));"
CurrentDb.Execute strSql, dbFailOnError
ASKER
Sorry but just gives me the:
Expected End of Statement at: [forms
Expected End of Statement at: [forms
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
That did it...
Just a question, I just don't understand the statement with the Eval?
Just a question, I just don't understand the statement with the Eval?
So ... when you use CurrentDB. Execute or CurrentDB.OpenRecordset etc ... and the SQL has a Forms Reference like you do, JET does not understand that reference at all. Note this is different that if you that a Forms reference in a Stored Query and you just run the query or use it in say a RecordSource or Row Source.
EVAL() gets around this issue as it turns out ... a bit of an old trick.
You can also use the Parameters property in the code itself ... but that required more lines of code.
EVAL() gets around this issue as it turns out ... a bit of an old trick.
You can also use the Parameters property in the code itself ... but that required more lines of code.
Here is another explanation I found in my notes - pretty much the same thing:
This shows how to user the Parameters gig ...
The reason for the Too Few Parameters error is because ... CurrentDb.Execute sends the query SQL directly to JET, which does not 'see' your Forms. Consequently, it does not know what Forms!SomeFormName!SomeCon trolName is, resulting in the Parameter error message ... just like if you were running a SELECT query, and misspelled a Form or Field name used in a criteria expression.
However, VBA does "see" your Forms, so that you can (and need to) evaluate the parameters in VBA before running the query using the Execute Method.
This can be done as follows:
Function mRunActionQuery(sYourActio nQryName As String) As Long
Dim parm As DAO.Parameter
With CurrentDb.QueryDefs(sYourA ctionQryNa me)
For Each parm In .Parameters
parm.Value = Eval(parm.Name) 'evaluates the parameters here
Next parm
.Execute dbFailOnError ' add error trapping
'Optional ...
mRunActionQuery = .RecordsAffected ' Function also returns the number of records affected as a bonus
End With
End Function
And your action query SQL might look like this:
UPDATE Table1
SET Table1.FIELD0 = "Update Test"
WHERE Table1.FIELD1=[Forms]![For m9].[txtTe st]
This shows how to user the Parameters gig ...
The reason for the Too Few Parameters error is because ... CurrentDb.Execute sends the query SQL directly to JET, which does not 'see' your Forms. Consequently, it does not know what Forms!SomeFormName!SomeCon
However, VBA does "see" your Forms, so that you can (and need to) evaluate the parameters in VBA before running the query using the Execute Method.
This can be done as follows:
Function mRunActionQuery(sYourActio
Dim parm As DAO.Parameter
With CurrentDb.QueryDefs(sYourA
For Each parm In .Parameters
parm.Value = Eval(parm.Name) 'evaluates the parameters here
Next parm
.Execute dbFailOnError ' add error trapping
'Optional ...
mRunActionQuery = .RecordsAffected ' Function also returns the number of records affected as a bonus
End With
End Function
And your action query SQL might look like this:
UPDATE Table1
SET Table1.FIELD0 = "Update Test"
WHERE Table1.FIELD1=[Forms]![For
One more similar explanation
Courtesy of EE's mbizup:
"References to form controls work fine in stored queries. If you take that same query and open it through VBA, the same form reference that worked before will cause an error -- unless you evaluate that reference seperately. This holds true for OpenRecordset statements, and also CurrentDB.Execute and Docmd.RunSQL."
Courtesy of EE's mbizup:
"References to form controls work fine in stored queries. If you take that same query and open it through VBA, the same form reference that worked before will cause an error -- unless you evaluate that reference seperately. This holds true for OpenRecordset statements, and also CurrentDB.Execute and Docmd.RunSQL."
ASKER
thanks so much!
Wrap your Form Reference in the Eval() function and note the Double Quotes