我如何确定下一个记录号(PK)是什么?
我试图以编程方式获取主键的自动编号序列中的下一个数字。例如,如果表中的最后一个数字是 10,我需要它返回 11。以前,我会使用类似的内容:
docmd.RunCommand acCmdRecordsGoToNew
为了告诉数据库转到下一条记录,然后我将它分配给表单上的控件向用户显示他们当前正在输入的记录。问题是,当我通过在属性窗口中将其属性设置为“否”来禁用导航按钮时,此功能停止工作。如何在不启用导航栏的情况下获取 vba 中的下一条记录?
im trying to get the next number in the autonumber sequence for the primary key programatically. For instance, if the last number in the table was 10, i need it to return 11. Before, I would use something like:
docmd.RunCommand acCmdRecordsGoToNew
in order to tell the database to go to the next record, and then i'd assign it to a control on the form to show the user what record they are currently entering. The problem is, this function ceased to work when I disabled the navigation buttons by setting it's property to "No" in the properties window. How do I get the next record in vba without the nav bar being enabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要知道真正的下一个值是什么,您必须查找自动编号列的 SeedValue。此代码的作用是:
如果您要经常调用它,您可能希望缓存 ADOX Catalog 对象变量,而不是每次调用此函数时都重新初始化它。
请注意,在多用户环境中,这可能准确,也可能不准确,因为当您使用它时,它可能已被其他用户更新。但是,它不存在跳过 Max()+1 可以具有的自动编号值的问题。
但请记住,如果您关心下一个自动编号值,则意味着您使用错误。自动编号值是代理键,您永远不应该关心这些值是什么。
To know what the real next value is, you have to look up the SeedValue for your Autonumber column. This code does that:
If you're going to call it a lot, you'd likely want to cache the ADOX Catalog object variable, rather than re-initialize it each time you call this function.
Note that in a multiuser environment, this may or may not be accurate, since by the time you use it, it may have been updated by another user. However, it doesn't have the problem with skipping Autonumber values that Max()+1 can have.
Keep in mind, though, that if you care about the next Autonumber value, it means YOU'RE USING IT WRONG. Autonumber values are surrogate keys and you should never, ever care what the values are.
结果发现有一个 VBA 函数可以与数据库交互并实际返回一个值。这就是我最终为获得下一个记录而所做的事情:
草率,但对我的单一客户情况有效。还有一个可以应用的 where 子句:
Turns out that there is a VBA function that will interact with the database and actually return a value. This is what I ended up doing to get the next record number:
Sloppy, but effective for my single client situation. There is also a where clause that can be applied: