I'm using this code to create multiple SQL statements that select the top ten invoice numbers by the user_id. It appears to produce the correct SQL but disregards the Top 10 in the query results and lists each user even if it is more than ten times.
I would expect to see each user listed <= 10 times with the appropriate invoices. Can anyone see what I'm missing? Thanks
I'm using this SQL in the query that calls the function:
SELECT DUR_March.invoice_nbr, DUR_March.user_id
FROM DUR_March
WHERE (((DUR_March.user_id)>=NthInGroup([DUR_March].[user_id],10))
Function NthInGroup(GroupID, N)
Static LastGroupId, LastNthInGroup
Dim ItemName, GDC, GroupIDName, SearchTable
Dim SQL As String, rs As DAO.Recordset, db As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
If (LastGroupId = GroupID) Then
NthInGroup = LastNthInGroup
Else
ItemName = "invoice_nbr"
GroupIDName = "user_id"
SQL = "Select Top " & N & " [" & ItemName & "] "
SQL = SQL & "From [" & SearchTable & "] "
SQL = SQL & "Where [" & GroupIDName & "]=" & GDC & GroupID & GDC & ""
SQL = SQL & "Order By [" & ItemName & "] Desc"
Set db = CurrentDb()
Set rs = db.OpenRecordset(SQL)
If (rs.BOF And rs.EOF) Then
LastNthInGroup = Null
LastGroupId = GroupID
NthInGroup = LastNthInGroup
Else
rs.MoveLast
LastNthInGroup = rs(ItemName)
LastGroupId = GroupID
NthInGroup = LastNthInGroup
End If
End If
End Function
This is the SQL produced in VBA: (Which is correct)
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABCS6E'Order By [invoice_nbr] Desc
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABCPA9'Order By [invoice_nbr] Desc
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABC6SL'Order By [invoice_nbr] Desc
I would expect to see each user listed <= 10 times with the appropriate invoices. Can anyone see what I'm missing? Thanks
I'm using this SQL in the query that calls the function:
SELECT DUR_March.invoice_nbr, DUR_March.user_id
FROM DUR_March
WHERE (((DUR_March.user_id)>=NthInGroup([DUR_March].[user_id],10))
Function NthInGroup(GroupID, N)
Static LastGroupId, LastNthInGroup
Dim ItemName, GDC, GroupIDName, SearchTable
Dim SQL As String, rs As DAO.Recordset, db As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
If (LastGroupId = GroupID) Then
NthInGroup = LastNthInGroup
Else
ItemName = "invoice_nbr"
GroupIDName = "user_id"
SQL = "Select Top " & N & " [" & ItemName & "] "
SQL = SQL & "From [" & SearchTable & "] "
SQL = SQL & "Where [" & GroupIDName & "]=" & GDC & GroupID & GDC & ""
SQL = SQL & "Order By [" & ItemName & "] Desc"
Set db = CurrentDb()
Set rs = db.OpenRecordset(SQL)
If (rs.BOF And rs.EOF) Then
LastNthInGroup = Null
LastGroupId = GroupID
NthInGroup = LastNthInGroup
Else
rs.MoveLast
LastNthInGroup = rs(ItemName)
LastGroupId = GroupID
NthInGroup = LastNthInGroup
End If
End If
End Function
This is the SQL produced in VBA: (Which is correct)
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABCS6E'Order By [invoice_nbr] Desc
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABCPA9'Order By [invoice_nbr] Desc
Select Top 10 [invoice_nbr] From [DUR_March] Where [user_id]='ABC6SL'Order By [invoice_nbr] Desc