
Passing a variable as the parameter for a query
Yes, in your code behind forms, do something like this
Dim qdf as QueryDef
'if you have created the query in code then
Set qdf = db.CreateQueryDef("", strSQL)
' or
'if the query is stored, then
Set qdf=CurrentDb.QueryDefs("qryDoStuff")
qdf.Parameters![dtmBegReportPeriod] = Format(Me.txtBegReportDate,
"m/d/yyyy")
qdf.Parameters![dtmEndReportPeriod] = Format(Me.txtEndReportDate,
"m/d/yyyy")
qdf.Execute
And you can substitute a variable for Me.txtBegReportDate or even something
like Date().
Hope this helps,
Lilla
Quote:
>How do I pass a variable to be used as a parameter in a query?
>The only way I can get this to work is to first assign the variable to a
>control (e.g., a textbox). And then pass the value of the control to the
>query.
>Is there a way to pass the variable to the query directly, avoiding the
>extra step of having to first assign it to a control and then referencing
>the control value as the parameter?
>Thanks for any ideas?
>Dave
>--------
>David R. Frick & Co., CPA
>http://www.frick-cpa.com