troubleshooting Question

vba code filtering top n values by group

Avatar of sxxgupta
sxxguptaFlag for United States of America asked on
Microsoft Access
8 Comments1 Solution853 ViewsLast Modified:
So the coding below works if I use one make table query to make a table and then use a second select query that calls on the make table to find the top n values via the function defined below.  However, I am now trying to skip the making table part and use the criteria defined in the first select query (the query that made the table before) and use the second query that now queries on the first query to find the top N records by group.  The coding is working all the way til it faults out on the line:

 Set rs = rs2.OpenRecordset

where I get a query syntax error.  I did not have this problem before when querying the temp make table.  So I am assuming it has to do with the way I am filtering the recordset...........The coding below is not mine and was developed by an expert in prior postings.  Could use some help
Function Nth4000(GroupID, N)
    ' Returns the Nth Item in GroupID for use as a Top N per group
    ' query criteria.
    Static LastGroupId, LastNth4000InGroup
    Dim ItemName, GroupIDName, GDC, SearchTable
    Dim SQL As String, rs As DAO.Recordset, rs1 As DAO.Recordset, rs2 As DAO.Recordset, db As DAO.Database, qdf1 As DAO.QueryDef, qdf2 As DAO.QueryDef, prm As DAO.Parameter
    Dim qry1 As String
    Dim qry2 As String

    qry1 = "z nmdf servicedata_4000_a"
    qry2 = "z nmdf servicedata_4000_b"
    Set db = CurrentDb
    Set qdf1 = db.QueryDefs(qry1)
    Set qdf2 = db.QueryDefs(qry2)
    For Each prm In qdf1.Parameters
        prm.Value = Eval(prm.Name)
    Next prm
    Set rs1 = qdf1.OpenRecordset(dbOpenDynaset)

    If (LastGroupId = GroupID) Then
        ' Returned saved result if function is called with the
        ' same GroupID more than once in a row.
        Nth4000 = LastNth4000InGroup
        ' Set the SQL statement parameters. These are the only items
        ' that need to be customized in this function.
        ' Set to Item field name.
        ItemName = "Qty"
        ' Set to Group ID field name.
        GroupIDName = "PartNo1"
        ' GroupID Delimiter Character:
        ' For Text use "'" (Note that this is a quotation mark, a space,
        ' an apostrophe, a space, and then a quotation mark. The spaces
        ' are necessary for SQL statements), Date "#", Numeric ""
        GDC = "'"
        ' Set to search table.
        SearchTable = qry2
        ' Build a Top N SQL statement dynamically given N and
        ' GroupID as parameters. Note that the sort is by the
        ' item in descending order, in order to get the Top N
        ' largest items.
        SQL = "Select Top " & N & " [" & ItemName & "] "
        SQL = SQL & "From [" & SearchTable & "] "
        SQL = SQL & "Where [" & GroupIDName & "]=" & GDC & GroupID & GDC _
        & " "
        SQL = SQL & "Order By [" & ItemName & "] Desc"
        ' Open up recordset on Top N SQL statement and read the
        ' last record to get the smallest item in the Top N.
        Set db = CurrentDb()
        Set rs2 = qdf2.OpenRecordset
        rs2.filter = SQL
        Set rs = rs2.OpenRecordset
        If (rs.BOF And rs.EOF) Then
            ' No matches found, return a null.
            LastNth4000InGroup = Null
            LastGroupId = GroupID
            Nth4000 = LastNth4000InGroup
            ' Return the smallest Top N item in the group.
            LastNth4000InGroup = rs(ItemName)
            LastGroupId = GroupID
            Nth4000 = LastNth4000InGroup
        End If
    End If

End Function

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros