访问VBA:查找最大数列,并添加1 [英] Access VBA: Find max number in column and add 1
问题描述
在我的Access数据库中,我有一个名为DEMANDE表。我想读的所有列NUMERO德COMMANDE的记录和读取列数量最多的,这样我可以用这个数字在未来新的记录。
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:
密钥索引是对NUMERO德COMMANDE。
The Key Index is for the Numero de Commande.
和这里是我的code目前:
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
感谢您的所有帮助。
推荐答案
只要列只包含数字值,不带alpha的,那么你可以使用:
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.
这篇关于访问VBA:查找最大数列,并添加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!