Solved

Accesing the Autonumber in an Access Table

Posted on 1998-09-10
2
140 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

18 Experts available now in Live!

Get 1:1 Help Now