访问 VBA:在列中查找最大数并加 1 [英] Access VBA: Find max number in column and add 1

查看:21
本文介绍了访问 VBA:在列中查找最大数并加 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Access 数据库中,我有一个名为Demande"的表.我想读取Numero de 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.

这是我的桌子:

关键索引用于 Numero de Commande.

The Key Index is for the 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

感谢大家的帮助.

推荐答案

只要该列只包含没有字母的数字值,您就可以使用:

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

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

注意:这不是我推荐的方法,而只是 Access 的内置选项,如果可能,您可以使用自动编号来省去麻烦,或者您可以拥有一个单独的计数器"表来记录最高记录number,当你想创建另一个时,你可以锁定这个表,将值加一(然后使用这个)然后释放锁定,这在多用户环境中会更有效.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆