Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to retrieve the last insert id in MsAccess Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
retrieve the last insert id in MsAccess 19-Jun-03
Category
ADO/OLE-DB
Language
Delphi 5.x
Views
192
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ronald Buster 


Ever wondered how to retrieve the last insert id in MsAccess, of the autoincrement 
field from a table.

Answer:

We have a table in MsAccess like : 

Test, Fields (id=autoinc, name=text); 

First we have to have a function like the one below : 

1   function GetLastInsertID: integer;
2   begin
3   
4     // datResult = TADODataSet
5   
6     datResult.Active := False;
7     datResult.CommandText := 'select @@IDENTITY as [ID]';
8     datResult.Active := True;
9   
10    Result := datResult.FieldByName('id').AsInteger;
11  
12    datResult.Active := False;
13  
14  end;


Now before getting the last inserted record record id = autoincrement field, in 
other words calling the above function. You have to do a SQL insert like the 
following 

15  procedure InsertRec;
16  begin
17  
18    // datCommand = TADOCommand
19  
20    datCommand.CommandText := 'insert into [test] ( [name] ) values ( "Test" )';
21    datCommand.Execute;
22  
23  end;


Now if we like to know which is the last autoinc value ( notice that the 
getlastinsertid proc. only works after the insertrec proc) 

24  procedure Test;
25  begin
26    InsertRec;
27    Showmessage(format('lastinsertid : %d', [GetLastInsertID]));
28  end;


Hope you can make this work, it works for me, any questions feel free to ask 

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC