[AccessD] Autonumber when?

Rusty Hammond rusty.hammond at cpiqpc.com
Wed Apr 6 09:46:37 CDT 2011


Okay, now I remember what's going on.  I had to go to Access 97
Developer's Handbook (page 269)to refresh my memory (page 1540 of the
Access 2000 Developer's Handbook volume 1).

You mentioned it in your original post that SQL doesn't create the
autonumber value until after the record is created.  When using the
AddNew method on a Dynaset-type recordset, as soon as you do .Update,
that new record gets added to the end of the recordset and is NOT the
current record.  To get to that new record, you use the .LastModified
property of the recordset.  You can use either of the following methods
to get to the new record:

.BookMark = .LastModified
OR
.Move 0, .LastModified

Then get the value of the ID field.  The nice thing is it works with
either a linked SQL table or Access table.

You new code should look like the following (I've rem'ed out your
existing lines so you can see the changes):

'
'Adds a record to the table saying that a specific user logged in at a
specific time '
'*+ Private class functions
Private Function mPLSLogin(blnLogIn As Boolean) Dim rs As DAO.Recordset
Dim db As DAO.Database

On Error GoTo Err_mPLSLogin

     Set db = dbDAOCurr
     Set rs = db.OpenRecordset("usystblPLSLog", dbOpenDynaset,
dbSeeChanges)
     If mlngIDUser = 0 Then Exit Function
     With rs
         .AddNew
         !PLSL_IDPLSUSR = mlngIDUser
         !PLSL_FE = CurrentProject.Name
         !PLSL_Login = blnLogIn
         !PLSL_WorkstationID = CurrentMachineName()
         'Take out the following two lines - get the ID value after the
Update - RustyH
         'On Error Resume Next
         'mlngLogID = !PLSL_ID
         .Update
         'Added the following line - RustyH
         .BookMark = .LastModified
         'If Err Then
             mlngLogID = !PLSL_ID
         'End If

         .Close
     End With

Exit_mPLSLogin:
     On Error Resume Next
     Set rs = Nothing
     If Not (rs Is Nothing) Then rs.Close: Set rs = Nothing
     Exit Function
Err_mPLSLogin:
     Select Case Err
     Case 0      '.insert Errors you wish to ignore here
         Resume Next
     Case Else   '.All other errors will trap
         Beep
         PLSLogErr Err.Number, Err.Description, Erl, cstrModule,
"mPLSLogin"
         Resume Exit_mPLSLogin
     End Select
     Resume 0    '.FOR TROUBLESHOOTING
End Function
**********************************************************************
WARNING: All e-mail sent to and from this address will be received,
scanned or otherwise recorded by the CPI Qualified Plan Consultants, Inc.
corporate e-mail system and is subject to archival, monitoring or review 
by, and/or disclosure to, someone other than the recipient.
**********************************************************************




More information about the AccessD mailing list