MS-Access – Retrieve the next autonumber of Access table

I have a table in Microsoft Access/JET, which has an automatically set field, which is set incrementally and used as the primary key of the table. I need to know the value of the primary key What is the next record inserted, but I need to know the value before inserting the record. Use SELECT MAX([ID])1 FROM [TableName]; because records are usually deleted from the end of the table, it doesn’t work. (Insert new Recording is just to find out the value and not an option.)

I know this can be easily done in MySQL by using the SHOW TABLE STATUS command. Is there anything that allows me to use ADO, DAO , VB6 or any other tools available to do the same for Access/JET?

You can use ADOX (Microsoft ADO Extensions for DDL and Security) to determine the 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 that this method may give wrong results in multi-user situations… if another user can You secretly INSERT between the retrieval of the next automatic number and the actual execution of the INSERT. If it is important, you can verify whether you got the expected value by checking SELECT @@ Identity after the INSERT.

I have a table in Microsoft Access/JET in which there is an automatically set field, which is set incrementally and used as the primary key of the table. I need to know the value of the primary key for the next insert What is the record, but I need to know the value before inserting the record. Use SELECT MAX([ID])1 FROM [TableName]; because records are usually deleted from the end of the table, so it doesn’t work. (Insert new records just to find Out-of-value is not an option either.)

I know this can be done easily in MySQL by using the SHOW TABLE STATUS command. Is there anything that allows me to use ADO, DAO, VB6 or whatever Other available tools do the same for Access/JET?

You can use ADOX (Microsoft ADO Extensions for DDL and Security) to determine the current “seed” value of the autonumber field.

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 that this method may give wrong results in multi-user situations… if another user can be between your retrieval of the next automatic number and the actual execution of the INSERT INSERT secretly. If it is important, you can verify that you got the expected value by checking SELECT @@ Identity after INSERT.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2700 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.