Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Accesing the Autonumber in an Access Table

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 64
Run code from text file in vb 1 71
How to Add / Edit Windows Menu 4 67
backup program with robocopy 6 42
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…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

791 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