473,769 Members | 5,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Some Argument, 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 9605
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*****@earthl ink.net> wrote in message
news:%9******** ***********@new sread1.news.atl .earthlink.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(Some Argument, 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*****@earthl ink.net> wrote in message
news:%9******** ***********@new sread1.news.atl .earthlink.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(Some Argument, 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
consecutivel y 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(Some Argument, 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*****@earthl ink.net> wrote in message
news:UL******** **********@news read1.news.atl. earthlink.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*****@earthl ink.net> wrote in message
news:%9******** ***********@new sread1.news.atl .earthlink.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(Some Argument, 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*****@earthl ink.net> wrote in message
news:%9******** ***********@new sread1.news.atl .earthlink.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(Some Argument, 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*****@earthl ink.net> wrote in message
news:%9******** ***********@new sread1.news.atl .earthlink.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(Some Argument, 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*****@earthl ink.net> wrote in message news:<%9******* ************@ne wsread1.news.at l.earthlink.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(Some Argument, 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
4964
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 webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
3
5098
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 orders quarterly, and at the same time how many different item numbers used in each quarter.
0
2107
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 reconiliation of debits and credits. Say you have a table / list that is 1000s of lines long. These could be either debits or credits. Then imagine you know that a certain
11
21932
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 C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
7
1309
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
3135
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 how global variables and function returns work... For example, I have a global array "char datestring;" which is defined in the function speakdate. speakdate just converts a set of integers (date variables) to a string.
6
2263
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)' works fine , but... when I was trying: I got "TypeError: 'list' object is not callable".
53
8416
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 language="javascript" type="text/javascript">
1
3141
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 numbering doesn't start always, running more times the update query, from the same starting parameter assigned (1000000000). It seems to me that it takes memory last number calculated and running prox update it starts from last table row updated. I need...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.