Melissa said:
I'm trying to create a function that I can put in a query field that will
consecutively number the records returned by the query starting at 1 and will
start at 1 each time the query is run. So far I have the function shown below
which doesn't work. If Reset is True then all I get is 1 in every field and if
Reset is False, the numbering does not start at 1 each time the query is run.
Can someone show me a function that works.
Function NextNumber(SomeArgument, Optional Reset As Boolean = False) As Integer
Static NN As Integer
If Reset = True Then NN = 0
NN = NN + 1
NextNumber = NN
End Function
That approach will not work Melissa. Access retrieves
records from a query on an as needed basis. Even after you
sort out the immediate problems, when you open the query in
data sheet mode you'll see the numbers looking great until
you scroll forward a ways then scroll backwards. The
records scrolling in on the top of the sheet will continue
to count up from the highest number displayed when you were
scrolling forward.
To make a query display sequential numbers, you need to do
it entirely in the query by using a subquery. One of the
keys to this kind of operation is that you have to have a
well defined sort order with no duplicate values in the
order by field.
Here's an example (air code)
SELECT table.f1, table.f2, table.f3,
(SELECT Count(*)
FROM table As X
WHERE X.sortfield <= table.sortfield
) As RecNum
FROM table
ORDER BY sortfield