Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / February 2005

Tip: Looking for answers? Try searching our database.

Obtain autonumber for new records in DAO

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Cleave - 14 Feb 2005 14:27 GMT
Hi all

If you use DAO to create a new record the code looks something like this:

Dim DatabaseName As DAO.Database
Set DatabaseName As CurrentDb
Dim TableName As DAO.Recordset
Set TableName = DatabaseName.OpenRecordset(“Name Of Table”)
With TableName
.AddNew
![Field 1] = Value1
![Field 2] = Value2
.Update
.Close
End With

If the table has an autonumber primary key, is there a quick way to get this
new number? (I.E. to find out what it is, and store it in a variable or
something?)

Thanks

David
Nikos Yannacopoulos - 14 Feb 2005 14:41 GMT
David,

Right after the .AddNew, the autonumber value is available; so, if you
use a variable, say, vKey, to store it into, just modify your code like:

With TableName
.AddNew
vKey = .[AutonumberFieldName]
![Field 1] = Value1
![Field 2] = Value2
.Update
.Close
End With

HTH,
Nikos

> Hi all
>
[quoted text clipped - 19 lines]
>
> David
David Cleave - 14 Feb 2005 14:51 GMT
Genius! This will save me so much daft coding!

Cheers

David

> David,
>
[quoted text clipped - 36 lines]
> >
> > David
Ken Snell [MVP] - 14 Feb 2005 14:48 GMT
Two ways to do it if you're using an autonumber primary key, because an
autonumber field gets its value as soon as a record is started.

The first example shows how to move the recordset back to the record that
was just modified, and then read the PKValue.

Dim DatabaseName As DAO.Database
Set DatabaseName As CurrentDb
Dim TableName As DAO.Recordset
Dim lngPKValue As Long
Set TableName = DatabaseName.OpenRecordset("Name Of Table")
With TableName
       .AddNew
           ![Field 1] = Value1
           ![Field 2] = Value2
       .Update
   .Bookmark = .LastModified
   lngPKValue = !AutonumberFieldName
   .Close
End With

The second example shows how to read the value while the record is being
added:

Dim DatabaseName As DAO.Database
Set DatabaseName As CurrentDb
Dim TableName As DAO.Recordset
Dim lngPKValue As Long
Set TableName = DatabaseName.OpenRecordset("Name Of Table")
With TableName
   .AddNew
       ![Field 1] = Value1
       ![Field 2] = Value2
       lngPKValue = !AutonumberFieldName
   .Update
   .Close
End With

Signature

       Ken Snell
<MS ACCESS MVP>

> Hi all
>
[quoted text clipped - 20 lines]
>
> David
David C. Holley - 14 Feb 2005 16:46 GMT
You should be able to by checking the field value containing the
Autonumber. Syntax should be
    TableName.Fields("fieldName")

. You may have to place it immediately after the .Update statement. Let
me know if it works, I'll be visiting tis soon myself.

David H

> Hi all
>
[quoted text clipped - 19 lines]
>
> David
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.