473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function to number lines in a query

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

Thanks!

Melissa
Nov 12 '05 #1
7 9584
TC
One way would be to discard the Reset parameer, and declare NN as a global
variable (not a local static). Then you could reset NN from code, before you
run the query:

NN = 0
docmd.openquery ...

HTH,
TC
"Melissa" <mt*****@earthlink.net> wrote in message
news:%9*******************@newsread1.news.atl.eart hlink.net...
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

Thanks!

Melissa

Nov 12 '05 #2
TC,

Thank you for your response!

Could you show me how to declare NN as a global variable.

Thanks,

Melissa
"TC" <a@b.c.d> wrote in message news:1069733055.699253@teuthos...
One way would be to discard the Reset parameer, and declare NN as a global
variable (not a local static). Then you could reset NN from code, before you
run the query:

NN = 0
docmd.openquery ...

HTH,
TC
"Melissa" <mt*****@earthlink.net> wrote in message
news:%9*******************@newsread1.news.atl.eart hlink.net...
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

Thanks!

Melissa


Nov 12 '05 #3
Melissa wrote:
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

--
Marsh
MVP [MS Access]
Nov 12 '05 #4
TC
Just go to the top of the module & enter:

Public NN as integer

Voila!

But naturally, it would be better to name it something better.

HTH,
TC
(off for the day)
"Melissa" <mt*****@earthlink.net> wrote in message
news:UL******************@newsread1.news.atl.earth link.net...
TC,

Thank you for your response!

Could you show me how to declare NN as a global variable.

Thanks,

Melissa
"TC" <a@b.c.d> wrote in message news:1069733055.699253@teuthos...
One way would be to discard the Reset parameer, and declare NN as a global variable (not a local static). Then you could reset NN from code, before you run the query:

NN = 0
docmd.openquery ...

HTH,
TC
"Melissa" <mt*****@earthlink.net> wrote in message
news:%9*******************@newsread1.news.atl.eart hlink.net...
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

Thanks!

Melissa



Nov 12 '05 #5
Here's a ready made solution:
http://www.lebans.com/rownumber.htm
Rownumber.zip is a database containing functions for the automatic row
numbering of Forms, SubForms and Queries.

Updated Oct. 13 by Allen Browne. Includes error handling and cleaned
code.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Melissa" <mt*****@earthlink.net> wrote in message
news:%9*******************@newsread1.news.atl.eart hlink.net...
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

Thanks!

Melissa


Nov 12 '05 #6
Have a look here:
http://www.lebans.com/rownumber.htm
Rownumber.zip is a database containing functions for the automatic row
numbering of Forms, SubForms and Queries.

Updated Oct. 13 by Allen Browne. Includes error handling and cleaned
code.
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"Melissa" <mt*****@earthlink.net> wrote in message
news:%9*******************@newsread1.news.atl.eart hlink.net...
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

Thanks!

Melissa


Nov 12 '05 #7
"Melissa" <mt*****@earthlink.net> wrote in message news:<%9*******************@newsread1.news.atl.ear thlink.net>...
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

Thanks!


Can you tell us more about your needs? If you have an AutoNumber field
or Date/Time field you could "rank" the rows based on that - or do you
have some other field that can be used to rank the query results?

You can use a correlated subquery to count the number of records
outside your current position, something like:

SELECT
field1
, field2
, (SELECT COUNT(*) FROM yourTable WHERE field1 > y.field1) + 1 As
Ranking
FROM
yourTable y
Nov 12 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: Anne | last post by:
Hi I have got a table with some sales transactions. It has got 3 fields 1) Ordernumber 2) Item number 3) Shippingdate Now I want to make ONE (not two) query that shows how many different...
0
by: seanseaghan | last post by:
New to this group, so greetings all! I am trying to develop query syntax in Access 2000 to accomplish the following: Imagine you are in an accounting dept. and you are working on a...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
7
by: K. Jansma | last post by:
Hi, given the following example class class Test: def f(self,a, L=): L.append(a) return L and the following statements
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
6
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)'...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.