[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Last ID generated with INSERT for AutoNumber Key

Posted on 2000-03-31
7
Medium Priority
?
217 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

650 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