Results 1 to 4 of 4
  1. #1
    SamL is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2021
    Location
    NJ
    Posts
    20

    .OpenRecordset problem

    Hi, I have the following code:



    Code:
    Private Sub GroupFooter1_Print(Cancel As Integer, PrintCount As Integer)
    
        Dim rstClientGroup As DAO.Recordset, qryCount As QueryDef, strSQL As String
        
        Set qryCount = CurrentDB.CreateQueryDef("")
        With qryCount
        strSQL = "SELECT Program, Count(ClientNum) AS CountOfClientNum FROM qryClientGroupSub " _
            & "WHERE (((Program) = [rptAnnualTotalContactsByProg].[Program])) " _
            & "GROUP BY Program"
            .SQL = strSQL
            Set rstClientGroup = .OpenRecordset(dbOpenDynaset)
            With rstClientGroup
                .MoveFirst
                Me.CountOfClientNum = !CountOfClientNum
                .Close
            End With
            .Close
        End With
    
    
    End Sub
    It breaks with a run-time error on this line:
    Code:
            Set rstClientGroup = .OpenRecordset(dbOpenDynaset)
    The code is supposed to run during the report, where the code is in a group footer (not the report footer).

    The Access error message is "Too few parameters. Expected 2." What's wrong with the code?
    Thanks for the help,
    Sam

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Probably the qryClientGroupSub query has parameters and also you are not isolating the [rptAnnualTotalContactsByProg].[Program] variable in your string:
    Code:
    'WHERE (((Program) = [rptAnnualTotalContactsByProg].[Program])) "
    WHERE (((Program) = '" & Me.[Program] & "')) " 'assume Program is a text type not numeric
    But probably you don't need all that when a simple dCount would do.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Pretty sure you can RunSql written like that (same as you can from an actual query), but you cannot do so from vba using CurrentDb.Execute or .Openrecordset. Not suggesting you use RunSql because that is only for action queries. To stay more or less with your current approach, pretty sure you would have to concatenate as shown if using vba, or don't use a temporary querydef, or use a stored query but modify the querydef sql. That's all for info purposes only because I agree that a domain aggregate function is probably all you need - possibly just in the control itself.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    SamL is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2021
    Location
    NJ
    Posts
    20
    Thanks to both of you for answering. Unfortunately, I never learned how to use the domain functions, so I have to limp around them, I'm afraid.

    Vlad, you are correct in both of your observations. I glossed over isolating the [Program] value, and I didn't even think that the error message was referring to the missing parameters. I added a line
    Code:
    .Parameters("Enter Year of Service") = Me.ServiceYear
    and isolated the [Program] variable with judicious use of double-quotes. It works now. Thanks again.

    The SQL is now
    Code:
        strSQL = "SELECT Program, Count(ClientNum) AS CountOfClientNum FROM qryClientGroupSub " _
            & "WHERE (((Program) = """ & Me.Program & """)) " _
            & "GROUP BY Program"
            .SQL = strSQL
            .Parameters("Enter Year of Service") = Me.ServiceYear
            Set rstClientGroup = .OpenRecordset(dbOpenDynaset)

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error with OpenRecordset
    By snsmith in forum Modules
    Replies: 15
    Last Post: 06-04-2019, 01:43 PM
  2. OpenRecordset method
    By Kundan in forum Programming
    Replies: 4
    Last Post: 03-13-2018, 09:33 PM
  3. Set rst = qdf.OpenRecordset(dbOpenSnapshot)
    By malin7232 in forum Programming
    Replies: 2
    Last Post: 06-25-2017, 10:04 PM
  4. db.OpenRecordset error
    By jscriptor09 in forum Programming
    Replies: 4
    Last Post: 01-27-2012, 12:17 AM
  5. OpenRecordset vs. Query
    By crispy-bacon in forum Programming
    Replies: 7
    Last Post: 07-04-2011, 09:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums