ms-access - Access VBA : Find max number in column and add 1

标签 ms-access find vba

在我的 Access 数据库中,我有一个名为“Demande”的表。我想读取“Numero de Commande”列中的所有记录并读取该列中的最大数字,以便我可以将该数字用于下一条新记录。

这是我的 table :

Table Picture

关键索引是为 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/

相关文章:

ms-access - 控制源 If 语句

sql - 在 MS Access 查询中使用多值字段

sql-server - 将 Access 2007 连接到 SQL Server 2008 数据库

c++ - 查找失败时 boost string_algo 返回值

batch-file - 使用批处理脚本查找和替换多个文件中的字符串

sql-server - SQL Server 直通查询作为 Access 中 DAO 记录集的基础

vba - 匹配记录时出错

sql-server - 将 listbox.recordset 设置为 ADODB 记录集会在使用列表框时导致崩溃

regex - 使用正则表达式查找具有确切扩展名的文件(并且后面没有任何内容)

vba - 在 VBA 中调用批处理文件无法正常工作