Gustav Brock
Gustav at cactus.dk
Thu May 25 10:03:21 CDT 2006
Hi Chris
This is the function we use. Note it allows for browsing and is very fast.
For large recordsets you should run a reset afterwards to kill the collection and free memory.
Public Function RowCounter( _
ByVal strKey As String, _
ByVal booReset As Boolean) _
As Long
' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
'
' Usage (typical select query):
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
' Call RowCounter(vbNullString, False)
' 2. Run query:
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
' INSERT INTO tblTemp ( RowID )
' SELECT RowCounter(CStr([ID]),False) AS RowID, *
' FROM tblSomeTable
' WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
Static col As New Collection
On Error GoTo Err_RowCounter
If booReset = True Then
Set col = Nothing
Else
col.Add col.Count + 1, strKey
End If
RowCounter = col(strKey)
Exit_RowCounter:
Exit Function
Err_RowCounter:
Select Case Err
Case 457
' Key is present.
Resume Next
Case Else
' Some other error.
Resume Exit_RowCounter
End Select
End Function
/gustav
>>> spikee at oatlandspark.org.uk 25-05-2006 09:29 >>>
Good day!
Dumb question here.
I've got a SELECT query and wish to add a calculated field with row number.
The number needs to start at 1, for the first record, and increment to n for
record n. I'm sure I've seen a post here recently about this but I cannot
find it in the archives.
TIA!
Chris Foote