在我的 Access 数据库中,我有一个名为“Demande”的表。我想读取“Numero de Commande”列中的所有记录并读取该列中的最大数字,以便我可以将该数字用于下一条新记录。
这是我的 table :
关键索引是为 Numero de Commande 设计的。
这是我目前的代码:
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
感谢大家的帮助。
最佳答案
只要该列仅包含没有字母的数值,那么您就可以使用:
NewNumeroCommande = Dmax("[Numero de Commande]", "Demande") + 1
注意:这不是我推荐的方法,而只是 Access 的一个内置选项,如果可能的话,您可以使用自动编号来省去麻烦,或者您可以有一个单独的“计数器”表来记录最高记录number,当你想创建另一个时,你可以锁定这个表,将值加一(然后使用它)然后释放锁,这在多用户环境中会更有效。
关于ms-access - Access VBA : Find max number in column and add 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11949603/