Chances are, these queries contain a reference such as:
[Forms].[Form1].[Text0]
Strictly, this is a parameter, but OpenQuery or RunSQL calls the Expression
Service (ES) to resolve the parameter, so you do not get the Parameter
dialog unless the reference does not resolve (e.g. if Form1 is not open.)
The ES is not available to resolve these references for the Execute method.
Your options are:
a) Explicitly assign the value to the parameter
Dim qdf As QueryDef
Set qdf = dbEngine(0)(0).QueryDefs("Query1")
qdf.Parameters("[Forms].[Form1].[Text0]") = [Forms].[Form1].[Text0]
qdf.Execute
b) Concatenate the value from the text box into the SQL string, instead of
using a saved query, e.g.:
Dim strSql As String
strSql = "DELETE FROM Table1 WHERE ID = " & [Forms].[Form1].[Text0] &
";"
dbEngine(0)(0).Execute strSql, dbFailOnError
Personally, I almost always use (b), because this method is so flexible. You
can examine the text boxes at runtime, ignore any that do not have a value,
and so the string you build is the most efficient execution possible.
Additionally, the code is independent of a stored query, which means:
- reduced dependencies (able to reuse the code elsewhere without worrying
about what queries it depends on);
- easier maintenance (not having to worry about somebody altering/removing
the query);
- far fewer saved queries.
But the *main* reason to use Execute rather than RunSQL is that you know if
it succeeded. With the dbFailOnError switch, you get a trappable error if
anything goes wrong. In 99% of cases, you really need to know if your action
query succeeded before you go on to the next thing you need to achieve.
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
rmcompute said:
Thank you. The method seems to be working except on some Make Table
queries.
Some work but others say:
3061 Too Few Parameters Expected 1.
These queries do not take parameters and work with DoCmd. They are Action
queries so should work with dbEngine(0)(0).Execute "Query1",
dbFailOnError.
Do you know why the system thinks that it needs parameters supplied ?