gpt4 book ai didi

excel - 如何从 Access 表中找到下一个 ID 自动编号并在 Excel 用户表单中显示该编号?

转载 作者:行者123 更新时间:2023-12-04 22:21:44 24 4
gpt4 key购买 nike

我想从 Access 表中获取下一个 ID 自动编号,并将其显示在 Excel 用户表单的文本框中。我已经找到了这段代码并且我已经玩过它,但我没有得到任何地方并且完全迷失了。如果有人能指出我正确的方向来解决它,我将不胜感激。

    Dim con As Object: Set con = CreateObject("ADODB.Connection")
Dim rs As Object: Set rs = CreateObject("ADODB.Recordset")
Dim sql As String

connectionstring = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
connectionstring = connectionstring & "DATA Source=C:\Users\MyPc\Desktop\Database1.accdb;"
con.Open connectionstring

sql = "select max(ID) as maxvalue from TableOne"

con.Execute sql

NextValue = (maxvalue) + 1

MsgBox NextValue
userform1.textbox2.value = NextValue
rs.Close
Set rs = Nothing

最佳答案

执行是针对操作 SQL(INSERT、UPDATE、DELETE)而不是 SELECT。使用 SELECT 打开记录集对象,然后从记录集中读取数据。

    Dim con As Object: Set con = CreateObject("ADODB.Connection")
Dim rs As Object: Set rs = CreateObject("ADODB.Recordset")
Dim sql As String

connectionstring = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
connectionstring = connectionstring & "DATA Source=C:\Users\MyPc\Desktop\Database1.accdb;"
con.Open connectionstring

sql = "select max(ID) as maxvalue from TableOne"

rs.Open sql, con, adOpenStatic, adLockReadOnly

NextValue = (rs!maxvalue) + 1

MsgBox NextValue
userform1.textbox2.value = NextValue
rs.Close
Set rs = Nothing

由于只需要一个值,请考虑替代方案:
Dim appAccess As Object
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase ("C:\Users\MyPc\Desktop\Database1Umpires.accdb")
userform1.textbox2.value = appAccess.DMax("ID", "TableOne")

关于excel - 如何从 Access 表中找到下一个 ID 自动编号并在 Excel 用户表单中显示该编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62327185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com