Solved

Last ID generated with INSERT for AutoNumber Key

Posted on 2000-03-31
7
157 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
  • 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 142

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 142

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 20 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now