B
Brittany :)
Ok, let's try this one more time.
The database that I'm working with is for a law firm and it has records of
all of the cases (filename) that they are working on and all of the notes
that are related to that case. What I need is a query/table/report ANYTHING
that will list the last three notes made on each file. I have tried using
(thanks to the help of this discussion borad) subqueries to complete this.
The problem is that it freezes my computer. Well, to be accurate, when I
switch to Task Manager my CPU is running at 100% capacity, and the program
fluctuates between responding and not responding. I'll let the query sit for
hours, and it still never returns the results. Here is the SQL statment for
the query that i've been trying to run:
SELECT *
FROM File Notes Query
WHERE noteid in (select top 3 noteid from [file notes query] S where
S.namefile = [file notes query].[namefile] order by time DESC);
THIS DOESNT WORK!!
Ok, the next step that I took is to adapt the following code statement found
on
http://support.microsoft.com/default.aspx?scid=kb;en-us;210039
Function NthInGroup(GroupID, N)
' Returns the Nth Item in GroupID for use as a Top N per group
' query criteria.
Static LastGroupId, LastNthInGroup
Dim ItemName, GroupIDName, GDC, SearchTable
Dim SQL As String, rs As Recordset, db As DATABASE
If (LastGroupId = GroupID) Then
' Returned saved result if function is called with the
' same GroupID more than once in a row.
NthInGroup = LastNthInGroup
Else
' Set the SQL statement parameters. These are the only items
' that need to be customized in this function.
' Set to Item field name.
ItemName = "OrderDate"
' Set to Group ID field name.
GroupIDName = "CustomerID"
' GroupID Delimiter Character:
' For Text use "'", Date "#", Numeric ""
GDC = "'"
' Set to search table.
SearchTable = "Orders"
' 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 rs = db.OpenRecordset(SQL)
If (rs.BOF And rs.EOF) Then
' No matches found, return a null.
LastNthInGroup = Null
LastGroupId = GroupID
NthInGroup = LastNthInGroup
Else
' Return the smallest Top N item in the group.
rs.MoveLast
LastNthInGroup = rs(ItemName)
LastGroupId = GroupID
NthInGroup = LastNthInGroup
End If
End If
End Function
The problem with this is that I haven't written code in about 5 years and I
was never really that good at it. I keep getting error messages when I try
to run this. With the line: Set rs = db.OpenRecordset(SQL) I get an error
message saying that I have open parameters. I have no idea how to fix it.
I know that my skill level isn't up there when it comes to code. So I would
really like to have some solution that would enable me to not have to use
code (wishful thinking I know)
Another option that I thought might work would be to rank that notes. I
could give the oldest note the ranking of 1 and so on and so forth, and then
run a query that pulls the last 3 notes. But I think that I'll have the same
problem as before. If I can't pull notes based on a decending date, how will
I be able to pull it with a number?
Any suggestions on this problem would be very helpful.
The database that I'm working with is for a law firm and it has records of
all of the cases (filename) that they are working on and all of the notes
that are related to that case. What I need is a query/table/report ANYTHING
that will list the last three notes made on each file. I have tried using
(thanks to the help of this discussion borad) subqueries to complete this.
The problem is that it freezes my computer. Well, to be accurate, when I
switch to Task Manager my CPU is running at 100% capacity, and the program
fluctuates between responding and not responding. I'll let the query sit for
hours, and it still never returns the results. Here is the SQL statment for
the query that i've been trying to run:
SELECT *
FROM File Notes Query
WHERE noteid in (select top 3 noteid from [file notes query] S where
S.namefile = [file notes query].[namefile] order by time DESC);
THIS DOESNT WORK!!
Ok, the next step that I took is to adapt the following code statement found
on
http://support.microsoft.com/default.aspx?scid=kb;en-us;210039
Function NthInGroup(GroupID, N)
' Returns the Nth Item in GroupID for use as a Top N per group
' query criteria.
Static LastGroupId, LastNthInGroup
Dim ItemName, GroupIDName, GDC, SearchTable
Dim SQL As String, rs As Recordset, db As DATABASE
If (LastGroupId = GroupID) Then
' Returned saved result if function is called with the
' same GroupID more than once in a row.
NthInGroup = LastNthInGroup
Else
' Set the SQL statement parameters. These are the only items
' that need to be customized in this function.
' Set to Item field name.
ItemName = "OrderDate"
' Set to Group ID field name.
GroupIDName = "CustomerID"
' GroupID Delimiter Character:
' For Text use "'", Date "#", Numeric ""
GDC = "'"
' Set to search table.
SearchTable = "Orders"
' 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 rs = db.OpenRecordset(SQL)
If (rs.BOF And rs.EOF) Then
' No matches found, return a null.
LastNthInGroup = Null
LastGroupId = GroupID
NthInGroup = LastNthInGroup
Else
' Return the smallest Top N item in the group.
rs.MoveLast
LastNthInGroup = rs(ItemName)
LastGroupId = GroupID
NthInGroup = LastNthInGroup
End If
End If
End Function
The problem with this is that I haven't written code in about 5 years and I
was never really that good at it. I keep getting error messages when I try
to run this. With the line: Set rs = db.OpenRecordset(SQL) I get an error
message saying that I have open parameters. I have no idea how to fix it.
I know that my skill level isn't up there when it comes to code. So I would
really like to have some solution that would enable me to not have to use
code (wishful thinking I know)
Another option that I thought might work would be to rank that notes. I
could give the oldest note the ranking of 1 and so on and so forth, and then
run a query that pulls the last 3 notes. But I think that I'll have the same
problem as before. If I can't pull notes based on a decending date, how will
I be able to pull it with a number?
Any suggestions on this problem would be very helpful.