I have a table in Microsoft Access/JET that has an AutoNumber field that's set incrementally which serves as the table's primary key. I need to know what the value of the primary key will be for the next inserted record, but I need to know the value before the record is inserted. Using SELECT MAX([ID]) + 1 FROM [TableName];
will not work because records are routinely deleted from the end of the table. (Inserting a new record just to figure out the value is not an option either.)
I know that this is easily done in MySQL by using the SHOW TABLE STATUS command. Is there anything that will let me do this exact same thing for Access/JET using ADO, DAO, VB6 or any other available tools?
In the Navigation Pane, right-click the table to which you want to add the primary key, and click Design View. Tip: If you don't see the Navigation Pane, press F11 to display it. Locate the first available empty row in the table design grid. In the Data Type field, click the drop-down arrow and click AutoNumber.
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
For example, if you want the AutoNumber field to start at 100, enter 99 in the Number field. Create and run an append query to append the temporary table to the table whose AutoNumber value you want to change.
Access does not allow you to have two AutoNumber fields in a table. If you explain what you're trying to accomplish, someone should be able to suggest an alternative.
You can use ADOX (Microsoft ADO Extensions for DDL and Security) to determine your autonumber field's current "Seed" value.
Public Function NextAutonumber(ByVal pTable As String, _
ByVal pAutonumField As String) As Long
Dim cat As Object
Set cat = CreateObject("ADOX.Catalog")
Set cat.ActiveConnection = CurrentProject.Connection
NextAutonumber = cat.Tables(pTable).Columns(pAutonumField).Properties("Seed")
Set cat = Nothing
End Function
Note this approach could give the wrong result in a multi-user situation ... if another user can sneak an INSERT
in between the time you retrieve the next autonumber and you actually do your INSERT
. If it's critical, you could verify whether you got the value you expected by checking SELECT @@Identity
after the INSERT
.
I initially agreed with HK1's solution to do your own, but Hans has found the better solution.
I was going to suggest finding the ID by inserting a record in a trasaction with rollback. The problem is, you will get the next number (Ex: 62), but when you actually add the new record for real, you get the ID after that (63). Repairing will reset the values to the next available number.
Just curious, why do you need to know this number? If you have a record (paper or otherwise) that needs this value, you should enter it in that system/paperwork after you actually enter the record.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With