VB6 - reset ID AutoNumber field in ms access table
Hello all
I would like to reset the ID AutoNumber field from my MS Access table when i delete a record.
How can i do that?
Thanks again for your help
Full code
Private Sub delete_Click() Select Case MsgBox("Do you realy want to delete that record " & ID_NUM.Text & " ?", vbYesNo Or vbQuestion Or vbSystemModal Or vbDefaultButton1, "Delete...") Case vbYes Set con = New ADODB.Connection con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & link_for_base.Caption con.Open con.Execute ("DELETE * From [TDM_Rate_form_no1] WHERE ID= " & ID_NUM.Text) con.Close con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & link_for_base.Caption con.Open rs.Open "select * from TDM_Rate_form_no1", con, adOpenDynamic, adLockOptimistic Set MSHFlexGrid1.Recordset = rs rs.Close Case vbNo End SelectEnd Sub
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
From VBA code, you can do this - I assume you mean the AutoNumber:
** VBA Code method - Example**
Place this code in a standard module. Do not name the module the same as the Function below.
Public Function mResetAutoNumber(lStartVal As Long, lIncrement As Long) As String
'Example: Set the Starting Value and Increment for a table named Table1ANTest and an AutoNumber field called AutoNum
Dim sSQL As String
sSQL = "ALTER TABLE [Table1ANTest] ALTER COLUMN [AutoNum] COUNTER (" & lStartVal & ", " & lIncrement & ");"
CurrentDb.Execute sSQL
mResetAutoNumber = "Auto Number has been re-numbered"
End Function
The first number (lStartVal) is the starting value, and the second (lIncrement) is the increment.
The example above will start the Auto Number at 1000 and increment by 50.
To run the code one time, open the VBA Immediate Window, and type
this line (including the question mark, followed by Enter:
You can run this against an empty existing table, or to modify the *next higher* Auto Number value and increment.
It will *not* change any existing auto number values.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
"An autonumber field with a data type of Long can go up to over 2 billion. "
Actually ... that's not quite accurate - since we're now on that subject:
An Auto Number (Long Integer) will increment from 1 to 2,147,483,647, then jump to -2,147,483,648, and then
count up to 0. Adding one record a second, 24/7 with no holidays, you can add
records for a bit over 136 years before hitting the limit.
** VBA Code method - Example**
Place this code in a standard module. Do not name the module the same as the Function below.
Public Function mResetAutoNumber(lStartVal
'Example: Set the Starting Value and Increment for a table named Table1ANTest and an AutoNumber field called AutoNum
Dim sSQL As String
sSQL = "ALTER TABLE [Table1ANTest] ALTER COLUMN [AutoNum] COUNTER (" & lStartVal & ", " & lIncrement & ");"
CurrentDb.Execute sSQL
mResetAutoNumber = "Auto Number has been re-numbered"
End Function
The first number (lStartVal) is the starting value, and the second (lIncrement) is the increment.
The example above will start the Auto Number at 1000 and increment by 50.
To run the code one time, open the VBA Immediate Window, and type
this line (including the question mark, followed by Enter:
?mResetAutoNumber(<YourSta
Example:
?mResetAutoNumber(500,3)
You can run this against an empty existing table, or to modify the *next higher* Auto Number value and increment.
It will *not* change any existing auto number values.
This works for A2K and later.