Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 10th, 2005, 02:54 PM
Registered User
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create Sequential Numbering That Does not Skip

I am trying to create a field that will automatically generate a sequential number (one that will not skip integers!). I would like to use little code if at all possible. Autonumber in Access does not work for what I am trying to do. For example... I have a record #1 then I want the next record to be #2, however if I hit escape within the new record (#2), the next number generated is #3. I need it to always be in sync. Any suggestions?

Reply With Quote
  #2 (permalink)  
Old May 10th, 2005, 03:07 PM
Friend of Wrox
Join Date: Oct 2004
Location: Clinton, UT, USA.
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts

I used to shy away from autonumber for this exact reason, but if you can figure out a way to use it, it probably be best for you in the long run.

However if it definitely won't work for you...

I used this code to figure out the next number in the sequence
where strPrimary key is the field Name of the sequential number
and strTable is the table in question.

strSQL = "SELECT * FROM " & strTable & " ORDER BY " & strPrimaryKey
    rs.Open strSQL, cn

    If rs.EOF And rs.BOF Then
        theKey = 1
        theKey = rs.Fields(strPrimaryKey) + 1
    End If

If you don't want the number to be added until you save the record, use an unbound form, and have a save button. The on Click event then pulls each of the values, gets the sequential number and then inserts into your table. This way if you exit without saving the record, nothing is added.

Hope that helps.


Reply With Quote
  #3 (permalink)  
Old May 10th, 2005, 08:40 PM
Friend of Wrox
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post

First of all, I'm going to assume that you need sequential numbering for something other than a foreign key. If it is a foreign key I have to ask what difference it makes?

Otherwise, an even more simple solution to sequential numbering (assuming you haven't gone back and deleted a number in the middle of a sequence) is:

NewNumber = DMax("number_field","table") + 1

Is that "as little code as possible"?

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
Reply With Quote
  #4 (permalink)  
Old May 11th, 2005, 10:34 AM
Registered User
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks for your suggestions! I ended up using the following after looking at rjweers suggestion, =nz(Max([SequentialNumber]),0)+1.

Again, thanks for your help!!!:D

Reply With Quote
  #5 (permalink)  
Old May 11th, 2005, 11:45 AM
Registered User
Join Date: May 2005
Location: Portland, Maine, .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Good solution. I have a co-worker who's very likely to use this very solution.

If the exceptions outnumber the rules, why are they called rules?
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Access sequential numbering in a query ChadKempel Access 3 June 4th, 2007 12:49 PM
Sequential numbering help gear1 Access 1 April 9th, 2007 06:31 AM
Sequential numbering a non auto number field dbartelt Access 3 May 5th, 2005 02:56 PM
Sequential Row numbering in queriws civa Access 5 March 7th, 2005 02:13 AM
How to create a sequential order number? evilandy Access ASP 2 September 24th, 2003 12:06 AM

All times are GMT -4. The time now is 06:06 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.