VBA如何找到最后插入的id?

发布于 2024-08-26 04:50:43 字数 444 浏览 5 评论 0原文

我有这样的代码:

With shtControleblad
    Dim strsql_basis As String
        strsql_basis = "INSERT INTO is_calculatie (offerte_id) VALUES ('" & Sheets("controleblad").Range("D1").Value & "')"

        rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic

        Dim last_id As String
        last_id = "select last_insert_id()"
End With

字符串last_id 未填充。怎么了?我需要找到last_insert_id,以便我可以在其他查​​询中使用它。

I have this code:

With shtControleblad
    Dim strsql_basis As String
        strsql_basis = "INSERT INTO is_calculatie (offerte_id) VALUES ('" & Sheets("controleblad").Range("D1").Value & "')"

        rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic

        Dim last_id As String
        last_id = "select last_insert_id()"
End With

The string last_id is not filled. What is wrong? I need to find te last_insert_id so I can use it in an other query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

第七度阳光i 2024-09-02 04:50:43

打开记录集后,您必须添加 rs.movelast ,这应该会有所帮助

you have to add rs.movelast after you open the recordset, that should help

你怎么敢 2024-09-02 04:50:43
last_id = "select last_insert_id()"

您已设置要执行的sql语句,但未执行
使用上述语句调用 rs.Open 来获取“last_insert_id”。

如果mysql支持单行多个sql语句,你可以这样做

strsql_basis = "INSERT INTO is_calculatie (offerte_id)  
VALUES ('" & Sheets("controleblad").Range("D1").Value & "')
; select last_insert_id()"

rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic
last_id = "select last_insert_id()"

You have set the sql statement to be executed, but have not executed it.
Call rs.Open with the above statement to get the 'last_insert_id` instead.

If mysql supports multiple sql statements on single line, you could do

strsql_basis = "INSERT INTO is_calculatie (offerte_id)  
VALUES ('" & Sheets("controleblad").Range("D1").Value & "')
; select last_insert_id()"

rs.Open strsql_basis, oConn, adOpenDynamic, adLockOptimistic
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文