Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    May 2001
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    too few parameters. expected 2 (Access 97 SR-2)

    I am getting the above error on the statement below:

    Set rsData = Db.OpenRecordset("HistAllRecordsForJobsWithAbend")

    ("HistAllRecordsForJobsWithAbend") is a query with the following SQL code behind it.

    SELECT [wsf-data-history].JobName AS Job, CDate(Left([CDate],2) & "/" & Mid([CDate],3,2) & "/" & Right([CDate],2)) AS RunDate, [wsf-data-history].JobId, [wsf-data-history].C, [wsf-data-history].CDate, [wsf-data-history].CTime, [wsf-data-history].Form, [wsf-data-history].Lines, [wsf-data-history].D, [wsf-data-history].E, [wsf-data-history].nCode, [wsf-data-history].cCode, [wsf-data-history].UserDataCode
    FROM [wsf-data-history] INNER JOIN HistJobAbendCounts ON [wsf-data-history].JobName = HistJobAbendCounts.Job
    WHERE (((CDate(Left([CDate],2) & "/" & Mid([CDate],3,2) & "/" & Right([CDate],2)))>=[Forms]![History]![dStartDate] And (CDate(Left([CDate],2) & "/" & Mid([CDate],3,2) & "/" & Right([CDate],2)))<=[Forms]![History]![dEndDate]))
    ORDER BY [wsf-data-history].JobName, CDate(Left([CDate],2) & "/" & Mid([CDate],3,2) & "/" & Right([CDate],2)), [wsf-data-history].JobId;

    This query works fine when run directly.

    What am I doing wrong.

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

    Re: too few parameters. expected 2 (Access 97 SR-2)

    The problem is in the reference to a form.

    [Forms]![History]![dStartDate] and [Forms]![History]![dEndDate] are valid in an Access query, but not in a DAO recordset. DAO doesn't know about Access forms and therefore sees these two expressions as parameters.

    There is a workaround, but it is elaborate:
    1. <LI>Open the query HistAllRecordsForJobsWithAbend in design view.
      <LI>Select Query/Parameters...
      <LI>In the first row, enter Forms![History]![dStartDate] under Parameter and Date/Time under Data Type.
      <LI>In the second row, enter Forms![History]![dEndDate] under Parameter and Date/Time under Data Type.
      <LI>Click OK.
      <LI>Save and close the query.
      <LI>Modify your code as follows:

      Dim qdf As QueryDef
      Set qdf = Db.QueryDefs("HistAllRecordsForJobsWithAbend")
      ' Set query parameters
      qdf.Parameters("Forms![History]![dStartDate]") = Forms![History]![dStartDate]
      qdf.Parameters("Forms![History]![dEndDate]") = Forms![History]![dEndDate]
      ' Open the recordset
      Set rsData = qdf.OpenRecordset
    This way, you explicitly set the parameters to the values in the text boxes on the form.

    Regards,
    Hans

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: too few parameters. expected 2 (Access 97 SR-2)

    Hi,
    I think your problem is that you have parameters in your original query. Try this:
    <pre>dim db as database, rsData as recordset, qdf as querydef
    set db = currentdb()
    set qdf = db.querydefs("HistAllRecordsForJobsWithAbend")
    with qdf
    ![Forms!History!dStartDate] = Forms!History!dStartDate
    ![Forms!History!dEndDate] = Forms!History!dEndDate
    set rsData = .openrecordset()
    end with
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    New Lounger
    Join Date
    May 2001
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: too few parameters. expected 2 (Access 97 SR-2)

    Thanks for the answer, that got me working.

  5. #5
    3 Star Lounger
    Join Date
    Jun 2001
    Location
    Maidstone, Kent, England
    Posts
    398
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: too few parameters. expected 2 (Access 97 SR-2)

    This should work too.
    <pre>dim db as database, rsData as recordset, qdf as querydef, prm As Parameter
    set db = currentdb()
    set qdf = db.querydefs("HistAllRecordsForJobsWithAbend")
    For Each prm In qdf.Parameters
    prm.Value = Eval(prm.Name)
    Next prm
    Set rsData = qdf.OpenRecordset
    </pre>


    HTH

    Peter

    edited to add missing dim statement

Posting Permissions

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