我如何确定下一个记录号(PK)是什么?

发布于 2024-11-27 04:46:32 字数 263 浏览 0 评论 0原文

我试图以编程方式获取主键的自动编号序列中的下一个数字。例如,如果表中的最后一个数字是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-12-04 04:46:32

要知道真正的下一个值是什么,您必须查找自动编号列的 SeedValue。此代码的作用是:

  Public Function GetSeedValue(strTable As String, strColumn As String) As Long
    Dim cnn As Object 'ADODB.Connection
    Dim cat As Object ' New ADOX.Catalog
    Dim col As Object ' ADOX.Column

    Set cnn = CurrentProject.Connection
    Set cat = CreateObject("ADOX.Catalog")
    cat.ActiveConnection = cnn

    Set col = cat.Tables(strTable).Columns(strColumn)
    GetSeedValue = col.Properties("Seed")

    Set col = Nothing
    Set cat = Nothing
    Set cnn = Nothing
  End Function

如果您要经常调用它,您可能希望缓存 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:

  Public Function GetSeedValue(strTable As String, strColumn As String) As Long
    Dim cnn As Object 'ADODB.Connection
    Dim cat As Object ' New ADOX.Catalog
    Dim col As Object ' ADOX.Column

    Set cnn = CurrentProject.Connection
    Set cat = CreateObject("ADOX.Catalog")
    cat.ActiveConnection = cnn

    Set col = cat.Tables(strTable).Columns(strColumn)
    GetSeedValue = col.Properties("Seed")

    Set col = Nothing
    Set cat = Nothing
    Set cnn = Nothing
  End Function

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.

纸短情长 2024-12-04 04:46:32

结果发现有一个 VBA 函数可以与数据库交互并实际返回一个值。这就是我最终为获得下一个记录而所做的事情:

Dim nextRecord As Integer
nextRecord = DMax("ID", "my_table") + 1

草率,但对我的单一客户情况​​有效。还有一个可以应用的 where 子句:

Dim nextRecord As Integer
nextRecord = DMax("ID", "my_table", "field =  value")

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:

Dim nextRecord As Integer
nextRecord = DMax("ID", "my_table") + 1

Sloppy, but effective for my single client situation. There is also a where clause that can be applied:

Dim nextRecord As Integer
nextRecord = DMax("ID", "my_table", "field =  value")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文