Solved

Last ID generated with INSERT for AutoNumber Key

Posted on 2000-03-31
7
201 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

726 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