?
Solved

Last ID generated with INSERT for AutoNumber Key

Posted on 2000-03-31
7
Medium Priority
?
208 Views
Last Modified: 2011-09-20
How can I know the last ID generated in a Jet Table with AutoNumber field type key after INSERT from VB a new record?
Thank you, sbelean.
0
Comment
Question by:sbelean
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2675591
doesnt it increment in number?  you could query the field
select max(autonumberfield) from tablename
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2675687
IN ADODB+Tables:
YourTable.AddNew
YourTable.Update
ID = YourTable.Field(ID).Value

With ADODB+SQL:
Connection.BeginTrans
Connection.Execute (INSERT...)
ID = Connection.Execute (SELECT (MAX(ID) FROM ... ).Fields(0).Value
Connection.CommitTrans

0
 

Author Comment

by:sbelean
ID: 2676010
AzraSound and angelIII, both of you answered me, but if New Value for AutoNumber field is Random how can I know the last ID generated? The first who will answer to this will receive the points if you agree that is fair enough . Thank you, sbelean  
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:wsh2
ID: 2676338
sbelean.. AutoNumber by definition..is a sequentially numbered field.. ie.. 1.. 2.. 3.. 4.. 5.. etc.

The only hope you would have in retrieving the random number would be..

Select LAST(MyRandomNumber) From MyTable

then you will get the last record.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2676360
wsh2: in access, you can have Autonumber which are generated randomly, so Max() won't work.

sbelean:
If you want to know the ID generated in every case, then you need to use the schema i mentioned already:
YourTable.AddNew
yourTable.Fields("ColumnName").value = FirstValue
YourTable.Update
ID = YourTable.Field(ID).Value
because with this method, the field value will be set after .Update()
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 80 total points
ID: 2676394
AngelIII:
Thank YOU for the random tip.. <smile>.

Yes, I agree with your comment as long as the program is still currently connected to the db, your solution clearly will work. However, if the program has lost its place (ie. through program termination).. and the db has NOT been rearranged, compacted, suffered high fragmentation or had others posting to it.. then "Maybe" <lol> the LAST record SQL statement will work.

The other choice the writer has, is to create a separate Table in the db to Log the Numbers as they are assigned. Its more overhead.. but if Number assignation retrieval is what he/she requires.. this would certainly work.
0
 

Author Comment

by:sbelean
ID: 2677983
Sorry for late, thank you all,sbelean
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question