Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Queries and parameters (Access 2002)

    I have a query setup that is 3 levels deep and I use it in VBA code.

    My queries are Q1 (which has 2 parameters, and I have defined the parameters section of the query), Q2 which uses Q1 as it's source, and Q3 which uses Q2 as it's source.

    It keeps coming up with Too few Parameters Expected 2 (error 3061).

    The code is:
    <pre> Dim qryDef As DAO.QueryDef
    Set qryDef = dbs.QueryDefs("qry R2R 01 Rank Order")
    qryDef.parameters("[Forms]![frm Analysis Race 2 Race Doubles]![TxtMeeting]") = _
    Forms![frm Analysis Race 2 Race Doubles]![TxtMeeting]
    qryDef.parameters("[Forms]![frm Analysis Race 2 Race Doubles]![TxtRace]") = _
    Val(Forms![frm Analysis Race 2 Race Doubles]![TxtRace])
    sSql = "qry R2R 03 Top N"
    Set rsA = dbs.OpenRecordset(sSql) <--------- it bombs on this -------
    </pre>


    Do I need to change the SQL of the first query Q1 or can it still be done the way I have it?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Queries and parameters (Access 2002)

    You must declare the same parameters explicitly in each of the queries (through Query | Parameters...). Supply the values for the parameters of the highest level query qry R2R 03 Rank Order, and open the recordset of the querydef:
    <pre> Dim qryDef As DAO.QueryDef
    Set qryDef = dbs.QueryDefs("qry R2R 03 Rank Order")
    qryDef.parameters("[Forms]![frm Analysis Race 2 Race Doubles]![TxtMeeting]") = _
    Forms![frm Analysis Race 2 Race Doubles]![TxtMeeting]
    qryDef.parameters("[Forms]![frm Analysis Race 2 Race Doubles]![TxtRace]") = _
    Val(Forms![frm Analysis Race 2 Race Doubles]![TxtRace])
    Set rsA = qryDef.OpenRecordset
    </pre>

    Note that qry R2R 01 Rank Order is not mentioned in this code, and that we use qryDef.OpenRecordset, not dbs.OpenRecordset(...)

  3. #3
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Queries and parameters (Access 2002)

    This frequently happens when you are referencing forms from queries, especially when the queries are layered. To fix this, instead of just this reference in your query:
    [Forms]![frm Analysis Race 2 Race Doubles]![TxtMeeting]

    Use this instead:
    Eval("[Forms]![frm Analysis Race 2 Race Doubles]![TxtMeeting]")

    You can also then skip setting the qdef.parameters via code.
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Queries and parameters (Access 2002)

    Thanks Hans, that works a treat.

    Mark, I tried yours and it kept coming up with an error, the original error I think. Thanks anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •