Solved

Accesing the Autonumber in an Access Table

Posted on 1998-09-10
2
142 Views
Last Modified: 2010-04-30
I need to access the next number available in an autonumber field of an access table.  I could look for the last number used and incremente it by one, but if a record has been added and then deleted, my solution will not be valid.  Does anyone knows how to do this?
0
Comment
Question by:gawilliam
  • 2
2 Comments
 
LVL 14

Accepted Solution

by:
waty earned 200 total points
ID: 1434072
One solution is the following :

With Data1.Recordset
  .AddNew
  MsgBox !ID
  .CancelUpdate
End With
0
 
LVL 14

Expert Comment

by:waty
ID: 1434073
Another solution is the following :

Public Function GetMaxSequence() As Long
   ' *** Get the las sequence
   
   Dim record        As Recordset
   Dim sSQL          As String
   
   On Error GoTo ERROR_GetMaxSequence
   
   sSQL = "Select Max(Counter) "
   sSQL = sSQL & "From Spool "
   
   Set record = DB.OpenRecordset(sSQL, DAO.dbOpenDynaset, DAO.dbSQLPassThrough + DAO.dbReadOnly)
   
   If (record.RecordCount <> 0) Then
      ' *** Infos found
      GetMaxSequence = CLng(ReadRecordSetField(record, 0))
   
   Else
      GetMaxSequence = 0
     
   End If
   
   record.Close
   Set record = Nothing
   
   Exit Function

ERROR_GetMaxSequence:
   LogString "Error in GetMaxSequence " & Error, LOG_TYPE_ERROR
   GetMaxSequence = 0
   
   Exit Function

End Function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now