• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

Last ID generated with INSERT for AutoNumber Key

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
sbelean
Asked:
sbelean
  • 2
  • 2
  • 2
  • +1
1 Solution
 
AzraSoundCommented:
doesnt it increment in number?  you could query the field
select max(autonumberfield) from tablename
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
sbeleanAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
wsh2Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
wsh2Commented:
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
 
sbeleanAuthor Commented:
Sorry for late, thank you all,sbelean
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now