Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

Access VBA: Find max number in column and add 1

In my Access database, I have a table called "Demande". I want to read all of the records in the column "Numero de Commande" and read the largest number in the column so that I can use that number for the next new record.

So here is my table:

Table Picture

The Key Index is for the Numero de Commande.

and here is my code currently:

 Dim highestInt as Integer
 Dim newNumeroCommande as Integer

 Set currentDatabase = CurrentDb
 Set rstDemande = currentDatabase.OpenRecordset("Demande")

 ' Find the highest integer in the column "Numero de Commande"

 newNumeroCommande = highestInt + 1
 rstDemande.AddNew
 rstDemande("Numero de Commande").Value = newNumeroCommande 
 rstDemande.Update

Thank you for all help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As long as the column only contains numeric values with no alpha's then you can use:

NewNumeroCommande = Dmax("[Numero de Commande]", "Demande") + 1

Note: This is not my recommended method but just an in-built option of Access, if possible then you can save yourself the hassle by using Autonumbering, or alternatively you could have a seperate "counter" table which records the highest record number, when you wish to create another, you could lock this table, increment the value by one (and then use this) and then release the lock, this would be more effective in a multi-user environment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...