View Full Version : Pass parmeters to a query


blueboy2001
04-06-2004, 03:27 AM
I am bulding a form, and I would like to populate a data grid on it with the results of a query, which is simple enough. However, I want to pass parameters to the query, these parameters being the value of a combobox on the form at the date selected on the form.

I have come up with the following code

Private Sub cmdcheck_Click()

Dim strsql As String
Dim jdate As Date
Dim eid As Integer
txtdate.SetFocus
jdate = txtdate
cboEquipment.SetFocus
eid = cboEquipment.Value


strsql = "SELECT EQUIPMENTID, DESCRIPTION, DATE, job.JOBID FROM EQUIPMENT, JOBEQUIPMENT, JOB WHERE " & _
"DATE = CDATE(jdate) AND EquipmentID = (eid)"
CurrentDb.QueryDefs("qryTest").SQL = strsql
DoCmd.OpenQuery "qrytest", acViewNormal

Problem is this isn't passing the parameters, but opening an inputbox for me to type them in.

Where am I going wrong? Thanks for any advice.

dcx693
04-06-2004, 04:45 AM
If you'll be using this query to populate the form grid often, you might want to use a stored query instead of a SQL string that Access needs to compile each time. You can still refer to controls on your form from the query. Here's an article that discusses the technique (and some other stuff): Customizing Access Parameter Queries (http://www.fontstuff.com/access/acctut08.htm). If you'd rather use a query on the fly, as you are now, realize that you need to pass the parameters to the query in a certain way before you open it. This should help: Open Parameter queries from code (http://mvps.org/access/queries/qry0003.htm)

blueboy2001
04-06-2004, 05:48 AM
Thanks for the advice.

The grid is going to be the most used feature of the database, so a permanent query would indeed be more appropriate. The first link looks promising, I'll try a few things in my database and see if I get anywhere.