473,698 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Auto Incrementing a number field (not autonumber field)

118 New Member
I have a database with 4 tables: tblMembers, tblRides, Session table and Reservation table. One of the fields in the Members table is set to number, but now I have all relationships set up and the data inserted, but one problem is now that when I add a new user it doesn't increment.. Is there any way with VBA, that I can grab the last CustomerID from the table and add one to it, and insert that as the new ID?

I was thinking to grab the last CustomerID of using

Expand|Select|Wrap|Line Numbers
  1. Dim SQL As String
  2.  
  3. SQL = "SELECT CustomerID FROM tblMembers ORDER BY CustomerID DESC;"
  4.  
  5. DoCmd.RunSQL SQL

That would simply get the value... the part of using it as a new ID may be doable with an INSERT INTO query.

how would I then increment the value from the query by one?

Sam
Jan 15 '08 #1
14 8969
Rabbit
12,516 Recognized Expert Moderator MVP
That would not return a value. DoCmd.RunSQL does not return any values. Plus, your current code will throw an error because your SQL string is not an action query.

Use the DMax function to return the highest number and then add 1 to it for the new number.
Jan 15 '08 #2
missinglinq
3,532 Recognized Expert Specialist
Where CustID is the textbox holding the incremented number, something like:
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.    Me!CustID = Nz(DMax("CustomerID", "tblMembers"),0) + 1
  3. End If
Linq ;0)>
Jan 15 '08 #3
PotatoChip
26 New Member
Where CustID is the textbox holding the incremented number, something like:
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.    Me!CustID = Nz(DMax("CustomerID", "tblMembers"),0) + 1
  3. End If
Linq ;0)>
I am using the exact same function but, I need my number to read 010001. I added the code with a few records already in the database with that number format but all my new records drop the first zero e.g 10002. I know it must be super simple but how do I get Access to add the zero at beginning?

Thanks!
Feb 5 '08 #4
missinglinq
3,532 Recognized Expert Specialist
You need to wrap it in the Format() function. For a six digit number it'd be something like this

Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.    Me!CustID = Format(Nz(DMax("CustomerID", "tblMembers"),0) + 1,"000000")
  3. End If
I think I got the parens right, but can't check right now. Post back if you have problems.

Welcome to TheScripts!

Linq ;0)>

P.S. Should we call you "Lays" or "Ruffles?"
Feb 5 '08 #5
PotatoChip
26 New Member
You need to wrap it in the Format() function. For a six digit number it'd be something like this

Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.    Me!CustID = Format(Nz(DMax("CustomerID", "tblMembers"),0) + 1,"000000")
  3. End If
I think I got the parens right, but can't check right now. Post back if you have problems.

Welcome to TheScripts!

Linq ;0)>

P.S. Should we call you "Lays" or "Ruffles?"

It doesn't work. :(
If I enter it the way you suggest, all I get is 0. I tried typing "010000" and all I get is 1000 and when I try going to a new record, I still get 1000. Thanks for the attempt, though.

Ruffles
Feb 5 '08 #6
jaxjagfan
254 Recognized Expert Contributor
It doesn't work. :(
If I enter it the way you suggest, all I get is 0. I tried typing "010000" and all I get is 1000 and when I try going to a new record, I still get 1000. Thanks for the attempt, though.

Ruffles
Make sure the data type of the CustomerID field is Text. And if you are entering the number in a control such as a text box - make sure it isn't using a number format.
Feb 5 '08 #7
PotatoChip
26 New Member
Make sure the data type of the CustomerID field is Text. And if you are entering the number in a control such as a text box - make sure it isn't using a number format.
It's set to Text, not Number. I'm starting to give up on the whole damn thing!
Feb 5 '08 #8
blyxx86
256 Contributor
It's set to Text, not Number. I'm starting to give up on the whole damn thing!
The function I use here works like this...
Expand|Select|Wrap|Line Numbers
  1. Function get_callnumber()
  2.     dim LP 
  3.     LP = 1
  4.     while(LP = 1)
  5.  
  6.         ' Instantiate a Recordset object, get the next avai call #
  7.         Set rsCALL = Server.CreateObject("ADODB.Recordset")
  8.         strSQL = "select CALLNUMBER from CONFIG"
  9.         Session("lastSQL")= strSQL
  10.         rsCALL.Open  strSQL, objConn, 3, 3
  11.  
  12.         callnumber = Cstr(rsCall("CALLNUMBER"))
  13.         new_callnumber = Int(rsCall("CALLNUMBER")) + 1
  14.         rsCALL.close
  15.  
  16.         strSQL = "update CONFIG set CALLNUMBER = '" & Cstr(new_callnumber) & "' "
  17.         session("lastSQL")= strSQL
  18.         rsCALL.Open  strSQL, objConn, 3, 3
  19.  
  20.         ' Instantiate a Recordset object, check if call # is already exists
  21.         strSQL = "select * from SERVICECALLS where CALLNUMBER = '" & callnumber & "' "
  22.         rsCALL.Open  strSQL, objConn, 3, 3
  23.  
  24.         if (rsCall.EOF) then
  25.             LP = 0
  26.             get_callnumber = callnumber
  27.         end if
  28.  
  29.         set rsCALL= nothing
  30.     wend
  31. End Function
  32.  
It works by storing the last (known) good "CallNumber " in a table called CONFIG. Then when the function is called, it generates the new number based on the number in the record in CONFIG, and compares it to the table to make sure it doesn't exist. If it does exist it loops until it finds one that is valid and it then updates the CONFIG table with this last (known) good "CalLNumber ".

Of course, you can perform some formating on the number as a string, for example using the "Format" function to ensure that the number is in the specified pattern you want. You will want to use the CStr(Format(get _callnumber()," 000000")) For example.. I hope this hels or guides you a ways.
Feb 6 '08 #9
missinglinq
3,532 Recognized Expert Specialist
If your ID field is text, you have to use the Val() function to convert it to a number in order to add to it. This will work for a text ID "number."

Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then        
  2. Me!CustID = Format(Nz(DMax("val([CustomerID])", "tblMembers"),0) + 1,"000000")
  3. End If
Linq ;0)>
Feb 6 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3774
by: Irwinsp | last post by:
Hi All, I have a form with an auto number field displayed. The field looks great except when the user is entering a new record. The field then has the text "auto number" in it. Is there a better way to display this field. I thought the number assigned to this record should be displayed while the user is entering the rest of the form data. Thanks, Paula
5
4547
by: Geoff Cayzer | last post by:
At http://www.blueclaw-db.com/tips_tricks.htm I came across a section which is included below and was hoping for some comment on the article. -------------- Almost never use this auto-number field as the primary key of the table. If you are thinking of hiring an Access programmer or consultant ask
2
1399
by: Vikaspell | last post by:
I want to set a auto number but the first 3 digits should be picked from a value of another table like for example a customer no is 999 i want the Pet no of this customer to be 999/(Autonumber) i can i get this done what i want is no user input in this field user will select the customer from the otrher table which give the first 3 digits and the auto number should come on it own
16
20504
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number can go without the system crashing. An ancillary question is how one resets an auto number so that the sequence starts again at 1. In the case of this file, the auto number field serves no useful purpose except as an
4
10645
by: the hotshot | last post by:
hello, this seems to be a hard question so far and noone has been able to help with this. is it possible to have access start an autonumber with a prefix according to the year when the data is entered. for example, if i entered something in 2004, i would like the number to bigin with 2004003, 2004004, 2004005... and same for 2005001, 2005002...? much and great appreciation for suggestions. thank you,
1
3677
by: John Phelan-Cummings | last post by:
I'm not certain if this made the post. Sorry if it's a repeat: Using a Button to take an autonumber from one form to populate another autonumber field on another form. I have a Mainform "A" with a button and code that opens another Mainform "B" with a subform which has the same table as Mainform "A". The button is supposed to tell the subform with the autonumber field
13
4728
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when i type in the details. I have just moved over to mysql server with access as the front end. I have setup the sql tables with the customer number as autonumber. When i go into the form and add a new customer it does not generate the
1
3509
by: asandiego | last post by:
Hey guys, this is my first post here but have been checking this site a lot for anything I need. I hope someone can lead me to what I should do or just an idea to what can be done. What I'm trying to do: I have a Form that has a subform. My primary form has a field that autonumber and this field has a one to many relationship to a field in my subform. The subform has another field that I want to AutoNumber or increment number BUT has to go...
1
1640
jinalpatel
by: jinalpatel | last post by:
I am dealing with 3 tables. Here is a brief meta data see the attachment for the relationship Conditions: 1)One GrantIndex can have multiple Deliverables 2)one Deliverable is made from combination of more than 85 performance measures. Performance measures have chkbox value, textbox value (number) and goal value(number)
0
9164
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...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
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
7734
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
5860
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
4370
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...
1
3051
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
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.