473,756 Members | 3,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort Table; Then Assign Sequential Numbers

Hi All:

I know this is simple, but I just can't seem to get there:

I need to sort a table by a text field (txtDescription ), then assign
sequential numbers to the field SEQUENCE in table.

Every time a new record is added, the all SEQUENCE #'s must be reset.

The re-numbering will be triggered by an EXPORT button.

What's the best way to accomplish this? An update query, or a Loop in
VBA?

Thanks!

amy
===
Nov 13 '05 #1
14 12046
Amy, you could simply design a query based on this table and sort it by
txtDescription. Now design a report based on this query. Ad an unbound
text box control to the detail section of the report. Select the text box
and view/properties from the main menu. Type in =1 as the control source and
set the Running Sum to Over All. Now it will always count them for you.
All you have to do is export the report.

--
Reggie

----------
"amywolfie" <am*******@veri zon.net> wrote in message
news:33******** *************** ***@posting.goo gle.com...
Hi All:

I know this is simple, but I just can't seem to get there:

I need to sort a table by a text field (txtDescription ), then assign
sequential numbers to the field SEQUENCE in table.

Every time a new record is added, the all SEQUENCE #'s must be reset.

The re-numbering will be triggered by an EXPORT button.

What's the best way to accomplish this? An update query, or a Loop in
VBA?

Thanks!

amy
===

Nov 13 '05 #2
Obviously you are aware that relational tables are, by definition, unordered
so they don't have a "record number" as non-relational database tables
sometimes do. I'd guess the requirement for a sequential number is a
requirement of the software that will use the data you export.

You can create a user defined function to return the next number in sequence
so you can use a query to do what you want, but it's not nearly so simple as
it seems... close examination will show that the UDF may be called multiple
times for each record, so there may be skips in the numbers unless you code
very carefully to ensure you don't increment more than once for the same
record.

I think it'll be a good deal simpler (and perhaps safer, too) if you open a
recordset using a Query with the sort, and update the sequence numbers in
each record one by one. That will, almost certainly, be a slower operation
than using a Query, but creating the required UDF for the query can be very
frustrating.

Larry Linson
Microsoft Access MVP

"amywolfie" <am*******@veri zon.net> wrote in message
news:33******** *************** ***@posting.goo gle.com...
Hi All:

I know this is simple, but I just can't seem to get there:

I need to sort a table by a text field (txtDescription ), then assign
sequential numbers to the field SEQUENCE in table.

Every time a new record is added, the all SEQUENCE #'s must be reset.

The re-numbering will be triggered by an EXPORT button.

What's the best way to accomplish this? An update query, or a Loop in
VBA?

Thanks!

amy
===

Nov 13 '05 #3
Larry:

You are absolutely right -- the Sequence order is a requirement for an
export into DB2.

Is there a "simple" alternative to manual input, perhaps using VBA?

Thanks!

amy
===

"Larry Linson" <bo*****@localh ost.not> wrote in message news:<gV******* *********@nwrdd c01.gnilink.net >...
Obviously you are aware that relational tables are, by definition, unordered
so they don't have a "record number" as non-relational database tables
sometimes do. I'd guess the requirement for a sequential number is a
requirement of the software that will use the data you export.

You can create a user defined function to return the next number in sequence
so you can use a query to do what you want, but it's not nearly so simple as
it seems... close examination will show that the UDF may be called multiple
times for each record, so there may be skips in the numbers unless you code
very carefully to ensure you don't increment more than once for the same
record.

I think it'll be a good deal simpler (and perhaps safer, too) if you open a
recordset using a Query with the sort, and update the sequence numbers in
each record one by one. That will, almost certainly, be a slower operation
than using a Query, but creating the required UDF for the query can be very
frustrating.

Larry Linson
Microsoft Access MVP

"amywolfie" <am*******@veri zon.net> wrote in message
news:33******** *************** ***@posting.goo gle.com...
Hi All:

I know this is simple, but I just can't seem to get there:

I need to sort a table by a text field (txtDescription ), then assign
sequential numbers to the field SEQUENCE in table.

Every time a new record is added, the all SEQUENCE #'s must be reset.

The re-numbering will be triggered by an EXPORT button.

What's the best way to accomplish this? An update query, or a Loop in
VBA?

Thanks!

amy
===

Nov 13 '05 #4
this should get you started - tweak to suit your needs:

Option Compare Database
Option Explicit

Public Sub UpdateCounterIn Table()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT UserName, IDNo FROM tblNames
ORDER BY UserName", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("IDNo ") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

End Sub
Nov 13 '05 #5
Hi Peter:

I'm getting strange results by using the following code:

Function AssignSequenceN umber()

'Sorts tblExportParent by txtDescription, then RENUMBERS SEQUENCE on
each export -- AWW 7/21/04

On Error GoTo Error_Handler:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT DESCRIPTION, SEQUENCE FROM
tblExportParent ORDER BY DESCRIPTION", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("SEQU ENCE") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

Exit Function
Error_Handler:
MsgBox Err.Number & " " & Err.Description

End Function

=====
SEQUENCE numbers are being set as:
3,5,8,10,11,12, 13,15,19,26,27, 28,29,31,33,40, 42,47,48,55,59, 64,65,
etc.

??????

Thanks!

amy
====

pi********@hotm ail.com (Pieter Linden) wrote in message news:<bf******* *************** ****@posting.go ogle.com>...
this should get you started - tweak to suit your needs:

Option Compare Database
Option Explicit

Public Sub UpdateCounterIn Table()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT UserName, IDNo FROM tblNames
ORDER BY UserName", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("IDNo ") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

End Sub

Nov 13 '05 #6
am*******@veriz on.net (amywolfie) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
Hi Peter:

I'm getting strange results by using the following code:

Function AssignSequenceN umber()

'Sorts tblExportParent by txtDescription, then RENUMBERS SEQUENCE on
each export -- AWW 7/21/04

On Error GoTo Error_Handler:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT DESCRIPTION, SEQUENCE FROM
tblExportParent ORDER BY DESCRIPTION", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("SEQU ENCE") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

Exit Function
Error_Handler:
MsgBox Err.Number & " " & Err.Description

End Function

=====
SEQUENCE numbers are being set as:
3,5,8,10,11,12, 13,15,19,26,27, 28,29,31,33,40, 42,47,48,55,59, 64,65,
etc.

??????

Thanks!

amy
====


no idea what's going on... do you have the table opened exclusively?
(Can you even do that using dbDenyWrite(?). ..)
Nov 13 '05 #7
No -- backend is on the network, and I am currently the only user of
the front end, which is multi-user.

amy
===

pi********@hotm ail.com (Pieter Linden) wrote in message news:<bf******* *************** ***@posting.goo gle.com>...
am*******@veriz on.net (amywolfie) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
Hi Peter:

I'm getting strange results by using the following code:

Function AssignSequenceN umber()

'Sorts tblExportParent by txtDescription, then RENUMBERS SEQUENCE on
each export -- AWW 7/21/04

On Error GoTo Error_Handler:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT DESCRIPTION, SEQUENCE FROM
tblExportParent ORDER BY DESCRIPTION", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("SEQU ENCE") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

Exit Function
Error_Handler:
MsgBox Err.Number & " " & Err.Description

End Function

=====
SEQUENCE numbers are being set as:
3,5,8,10,11,12, 13,15,19,26,27, 28,29,31,33,40, 42,47,48,55,59, 64,65,
etc.

??????

Thanks!

amy
====


no idea what's going on... do you have the table opened exclusively?
(Can you even do that using dbDenyWrite(?). ..)

Nov 13 '05 #8
Can anyone out there debug this code or suggest alternate code to
accomplish assigning Sequence #'s to records on a re-sort?

Thanks!!

amy
===
am*******@veriz on.net (amywolfie) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
Larry:

You are absolutely right -- the Sequence order is a requirement for an
export into DB2.

Is there a "simple" alternative to manual input, perhaps using VBA?

Thanks!

amy
===

"Larry Linson" <bo*****@localh ost.not> wrote in message news:<gV******* *********@nwrdd c01.gnilink.net >...
Obviously you are aware that relational tables are, by definition, unordered
so they don't have a "record number" as non-relational database tables
sometimes do. I'd guess the requirement for a sequential number is a
requirement of the software that will use the data you export.

You can create a user defined function to return the next number in sequence
so you can use a query to do what you want, but it's not nearly so simple as
it seems... close examination will show that the UDF may be called multiple
times for each record, so there may be skips in the numbers unless you code
very carefully to ensure you don't increment more than once for the same
record.

I think it'll be a good deal simpler (and perhaps safer, too) if you open a
recordset using a Query with the sort, and update the sequence numbers in
each record one by one. That will, almost certainly, be a slower operation
than using a Query, but creating the required UDF for the query can be very
frustrating.

Larry Linson
Microsoft Access MVP

"amywolfie" <am*******@veri zon.net> wrote in message
news:33******** *************** ***@posting.goo gle.com...
Hi All:

I know this is simple, but I just can't seem to get there:

I need to sort a table by a text field (txtDescription ), then assign
sequential numbers to the field SEQUENCE in table.

Every time a new record is added, the all SEQUENCE #'s must be reset.

The re-numbering will be triggered by an EXPORT button.

What's the best way to accomplish this? An update query, or a Loop in
VBA?

Thanks!

amy
===

Nov 13 '05 #9
am*******@veriz on.net (amywolfie) wrote in message news:<33******* *************** ****@posting.go ogle.com>...
Hi Peter:

I'm getting strange results by using the following code:

Function AssignSequenceN umber()

'Sorts tblExportParent by txtDescription, then RENUMBERS SEQUENCE on
each export -- AWW 7/21/04

On Error GoTo Error_Handler:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim lngCounter As Integer

Set db = CurrentDb
Set rs = db.OpenRecordse t("SELECT DESCRIPTION, SEQUENCE FROM
tblExportParent ORDER BY DESCRIPTION", dbOpenDynaset)

'--set initial counter value
Do Until rs.EOF
rs.Edit
lngCounter = lngCounter + 1
rs.Fields("SEQU ENCE") = lngCounter
rs.Update
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
Set db = Nothing

Exit Function
Error_Handler:
MsgBox Err.Number & " " & Err.Description

End Function

=====
SEQUENCE numbers are being set as:
3,5,8,10,11,12, 13,15,19,26,27, 28,29,31,33,40, 42,47,48,55,59, 64,65,
etc.

??????

Thanks!

amy
====


Amy,

While you're waiting for us to figure this out, why don't you create a
Make Table query with all the fields in tblExportParent and sorted by
DESCRIPTION. After creating the new table, say tblExportParent 2, go
into it's table design and remove the SEQUENCE field. Then add it
back as an AutoNumber field. After saving the design go back in and
change the SEQUENCE field to a Long. After checking out the results,
delete tblExportParent and rename tblExportParent 2 to tblExportParent .
I can't believe I just suggested those steps :-). I notice a few
things about the code that still don't explain the results you are
getting. First, a Function that doesn't return a value is being used.
I would normally use a Sub for something like that but it's not
unheard of to use a Function without a return value. Second, you
don't give an initial value for lngCounter. I don't like assuming
that Access will give it a meaningful value. That doesn't explain the
results you are getting either. Third, the code doesn't do a
rs.MoveFirst before starting to edit the record. I wouldn't assume
that Access is at any particular record without performing some kind
of positioning command. It's possible that this could do some strange
things, but the MoveNext would have to skip around almost randomly to
miss hitting the EOF and increment the count for that record more than
once. It's a little confusing to me to name a variable lngCounter and
make it an Integer but it's not unheard of. I normally put a ';' at
the end of the SQL string but it's not required. None of these things
seem to account for what you are getting. Did Pieter think you were
just looking for someone to do your homework assignment? It doesn't
look like his normal style of coding.

James A. Fortune
Nov 13 '05 #10

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

Similar topics

6
12709
by: Jenn L | last post by:
I have a database that is pre-populated with sequential part numbers. As people reserve the parts I update a flag to show the # is no longer available. Now they want the ability to take out a block of "x" number of sequential part numbers - say for example 5. If my database had the following numbers available: 101 104 105 110
2
3404
by: Tony Williams | last post by:
I recently posted a message asking for help with sequential numbers. I want to create an autonnumber reference number that reverts back to 1 at the start of each year. GlenAppleton gave me some code and I thought I had it working but I don't. When I create a new record nothing happens in the control CommDocNbrtxt where the number should appear. Here is my complete code, can anyone help? If you read this Glen sorry to post again but I...
2
5119
by: John | last post by:
Hi I need to assign sequential invoice numbers to orders starting from the last highest number + 1. I have tried the following code; UPDATE Orders SET Orders. = DMax("","Orders")+1 WHERE Orders.) Is Null AND ... The problem is that all orders get the same number which is the last highest number + 1. Apparently the query does not recalculate DMax("","Orders")+1 for each record and instead only gets the value once in the
4
2092
by: Bruce | last post by:
Surely someone has done this before, and I am guessing there is a simple solution that is eluding me. I have a simple report based on a recordset. For each record there is a field (RecNum) that is basically a sequential number from 1 to n. Occasionally there are skips between numbers. I would like to print 'record skipped' for each record missing from the sequence. For example, if there were no records numbered 5 and 6 in a sequence...
7
4013
by: Daine | last post by:
Thanks to everyone who gave me pointers last time, I got what I needed done, now I have a new one. We have pulled the invoices from our sales systems into access. The dockets should be sequential, so I want tot find out whic (if any) numbres have been skipped so I can find out why the docket was not invoiced. I have a table that list lost of information but I'm interested in DockNo from the InvHistory table between certain dates, I can...
3
5091
by: Finomosec | last post by:
Hi, i have a table of number-objects with beginning and endnr: 10-15 16-20 25-30 32-32 35-35 36-36 37-40
3
4691
by: Excel 009 | last post by:
Hi, Is there a way to populate sequential numbers in a field using SQL or VBA? If yes, how? Assume the following is my existing table: Fruit ID Apply Banana
18
2422
by: Joel Miller | last post by:
I found an article that was somewhat like what I was trying to do. The article was titled: SQL Query - Find block of sequential numbers Here is the article http://bytes.com/topic/sql-server/answers/81418-sql-query-find-block-sequential-numbers What I have is slightly different. I am trying to create a 'phone sheet' report which lists all the given phone numbers for a customer. Sometimes, the customer will have blocks of hundreds of...
2
2387
by: JonHuff | last post by:
looking for MS Access VBA code to do the following: I am trying to assign incrementing numbers ( ie - 1-10) to records in an existing table by date and pull a 10% random sample from that table based on the numbers assigned. Any help is appreciated. Existing Example Table: number date person 45001 1/19/2011 Ray 21020 1/19/2011 Steve 49009 1/19/2011 Bob 72005 1/19/2011 Ray 62203 1/19/2011 Bob...
0
9287
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
10046
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
9857
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
8723
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
6542
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
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3369
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.