Using form field value in query criteria

Andy Mc

New member
Local time
Today, 16:05
Joined
May 6, 2012
Messages
24
Hi.

I have a query named "ClientWrkHrs":

SELECT ClientCal.Abbr, ClientCal.Start_Date, ClientCal.StartTime, ClientCal.EndTime, DateDiff("n",[StartTime],[EndTime])/10+1 AS Slots
FROM ClientCal
WHERE (((ClientCal.Abbr) Like "*" & "jo" & "*"))
ORDER BY ClientCal.StartTime;

This returns the records that I want, limited to those with "jo" in the field Abbr.

However, I want to make the criteria less specific, so I tried inserting a reference to a form field:

SELECT ClientCal.Abbr, ClientCal.Start_Date, ClientCal.StartTime, ClientCal.EndTime, DateDiff("n",[StartTime],[EndTime])/10+1 AS Slots
FROM ClientCal
WHERE (((ClientCal.Abbr) Like "*" & [Forms]![ClientAppt]![Cname] & "*"))
ORDER BY ClientCal.StartTime;

If I run the query in isolation and enter "jo" in the prompt it works fine. However, If I run the query from a command button on the form I get an error: "Too few parameters. Expected 1". The command button calls a module and it fails at line:

Set RS = db.OpenRecordset("ClientWrkHrs")

Does anyone have any ideas what could be wrong?

Thanks
 
Thanks Gasman. I couldn't get that to work for me (my bad probably) but it did get me on to a work around.
 
A DAO method such as Openrecordset passes the query directly to the database engine (Jet/ACE). This understands SQL, but cannot do anything with variables and objects such as form text fields, which is why the query occurs.
In the simplest case, you make a replacement
Code:
Forms.ClientAppt.Cname => Eval(Forms.ClientAppt.Cname)
Eval evaluates the content of the text field so that the content is passed directly to processing.

Other ways to pass parameters:
 
Rich (BB code):
Dim db    As     DAO.Database
Dim qd    As    DAO.Querydef
Dim rs  As  DAO.Recordset

    Set db = Currentdb()
    Set qd = db!querydef!yourqueryname
        qd.Parameters![EnterCustID] = Me.CustID
        qd.Parameters![EnterStartDT] = Me.StartDT
    Set rs = qd.OpenRecordset

When I create parameters I named them [Entersomething] so I know if the prompt is intentional or accidental
 

Users who are viewing this thread

Back
Top Bottom