在 MS Access 中使用 VBA 获取新添加的 MySQL 的 AutoID
Getting the newly added AutoID of MySQL using VBA in MS Access
我有一个带有 MySQL 数据库的 MS Access 应用程序。我从 Stack Overflow 中尝试了很多方法,但无法获取新添加的行的主键。
我已经尝试了我能找到的所有方法,但没有成功。
Set pxRST = db.OpenRecordset("SELECT * from tblPatients WHERE dispenseID = " & oPxID & " AND ChemistID = " & chemID, dbOpenDynaset, dbSeeChanges)
if pxrst.eof then
pxRST.AddNew
pxRST("dispenseid") = oPxID
pxRST("chemistID") = chemID
pxRST("firstname") = firstName
pxRST("lastname") = lastName
pxRST("address") = Address
pxRST("postcode") = postcode
pxRST("phonenumber") = phonenumber
pxRST.Update
pxRST.Bookmark = pxRST.LastModified
gPxID = pxRST!PatientID
Debug.Print gPxID
end if
这给出了一个 "record is deleted" 错误
我也尝试过使用
gPxID = currentdb.openrecordset("SELECT @@identity").value(0)
这只是把我 0 作为一个数字
我确实进入了 MYSQL workbench 并尝试了
INSERT INTO tblpatients
SELECT @@IDENTITY
我确实得到了一条新记录的 ID。
所以我已经很努力地尝试了,但我只是无法从这里弄清楚如何在 VBA.
中实现它
我目前正在使用 QueryDefs,但我对此很陌生,我认为还没有真正做到这一点。
Dim qdf2 As DAO.QueryDef
strSQL = "INSERT INTO tblPatients (dispenseid,chemistID,firstname,lastname,address,postcode,phonenumber) " & _
"VALUES (" & oPxID & "," & chemID & ",'" & firstName & "','" & lastName & "','" & Address & "','" & postcode & "','" & phonenumber & "' )"
Set qdf2 = db.QueryDefs("quGetPxDetails")
With qdf2
.SQL = strSQL
.Connect = oCon
qdf2.Execute
End With
这取决于您的 tblPatients
是否具有自动递增主键。
如果是这样,那么您不应该手动为其分配一个值,即您的
INSERT INTO tblPatients (dispenseid,chemistID,firstname,lastname,address,postcode,phonenumber) " & _
"VALUES (" & oPxID & "," & chemID & ",'" & firstName & "','" & lastName & "','" & Address & "','" & postcode & "','" & phonenumber & "' )"
没有在 PK 中插入任何东西。
让我们假设您在 tblPatients 中有一列名为
PatientID
类型 int(11) NOT NULL AUTO_INCREMENT
、
现在,在您插入一条新记录后,您可以使用函数
取回该插入记录的 ID
LAST_INSERT_ID()
所以
Select LAST_INSERT_ID() as my_last_pk;
将 return 您刚刚插入名为 my_last_pk
的记录的主键。
请注意,这是特定于会话的,因此如果您和其他人或多或少同时插入一条新记录,您将各自取回自己插入的 PK。
所以我的答案来自 ComputerVersteher 和 User2834566 信息。
我不得不 运行 两个查询,既作为直通查询又作为 运行ning 在同一个数据库实例上以确保它返回正确的值:
Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim rst As DAO.Recordset
strSQL = "INSERT INTO medstome_masterdb.tblChemists (chemName) VALUES ('newchem')"
strGETID = "SELECT @@IDENTITY"
Set db = CurrentDb
With db
Set qdf = db.CreateQueryDef("")
With qdf
.ReturnsRecords = False
.Connect = oCon
.SQL = strSQL
.Execute
End With
With qdf
.ReturnsRecords = True
.Connect = oCon
.SQL = strGETID
Set rst = .OpenRecordset(dbOpenSnapshot)
gPxID = rst(0)
Debug.Print gPxID
End With
End With
这非常有效!
感谢大家的回应和帮助
我有一个带有 MySQL 数据库的 MS Access 应用程序。我从 Stack Overflow 中尝试了很多方法,但无法获取新添加的行的主键。
我已经尝试了我能找到的所有方法,但没有成功。
Set pxRST = db.OpenRecordset("SELECT * from tblPatients WHERE dispenseID = " & oPxID & " AND ChemistID = " & chemID, dbOpenDynaset, dbSeeChanges)
if pxrst.eof then
pxRST.AddNew
pxRST("dispenseid") = oPxID
pxRST("chemistID") = chemID
pxRST("firstname") = firstName
pxRST("lastname") = lastName
pxRST("address") = Address
pxRST("postcode") = postcode
pxRST("phonenumber") = phonenumber
pxRST.Update
pxRST.Bookmark = pxRST.LastModified
gPxID = pxRST!PatientID
Debug.Print gPxID
end if
这给出了一个 "record is deleted" 错误
我也尝试过使用
gPxID = currentdb.openrecordset("SELECT @@identity").value(0)
这只是把我 0 作为一个数字
我确实进入了 MYSQL workbench 并尝试了
INSERT INTO tblpatients
SELECT @@IDENTITY
我确实得到了一条新记录的 ID。
所以我已经很努力地尝试了,但我只是无法从这里弄清楚如何在 VBA.
中实现它我目前正在使用 QueryDefs,但我对此很陌生,我认为还没有真正做到这一点。
Dim qdf2 As DAO.QueryDef
strSQL = "INSERT INTO tblPatients (dispenseid,chemistID,firstname,lastname,address,postcode,phonenumber) " & _
"VALUES (" & oPxID & "," & chemID & ",'" & firstName & "','" & lastName & "','" & Address & "','" & postcode & "','" & phonenumber & "' )"
Set qdf2 = db.QueryDefs("quGetPxDetails")
With qdf2
.SQL = strSQL
.Connect = oCon
qdf2.Execute
End With
这取决于您的 tblPatients
是否具有自动递增主键。
如果是这样,那么您不应该手动为其分配一个值,即您的
INSERT INTO tblPatients (dispenseid,chemistID,firstname,lastname,address,postcode,phonenumber) " & _
"VALUES (" & oPxID & "," & chemID & ",'" & firstName & "','" & lastName & "','" & Address & "','" & postcode & "','" & phonenumber & "' )"
没有在 PK 中插入任何东西。
让我们假设您在 tblPatients 中有一列名为
PatientID
类型 int(11) NOT NULL AUTO_INCREMENT
、
现在,在您插入一条新记录后,您可以使用函数
取回该插入记录的 IDLAST_INSERT_ID()
所以
Select LAST_INSERT_ID() as my_last_pk;
将 return 您刚刚插入名为 my_last_pk
的记录的主键。
请注意,这是特定于会话的,因此如果您和其他人或多或少同时插入一条新记录,您将各自取回自己插入的 PK。
所以我的答案来自 ComputerVersteher 和 User2834566 信息。
我不得不 运行 两个查询,既作为直通查询又作为 运行ning 在同一个数据库实例上以确保它返回正确的值:
Dim strSQL As String
Dim qdf As DAO.QueryDef
Dim db As DAO.Database
Dim rst As DAO.Recordset
strSQL = "INSERT INTO medstome_masterdb.tblChemists (chemName) VALUES ('newchem')"
strGETID = "SELECT @@IDENTITY"
Set db = CurrentDb
With db
Set qdf = db.CreateQueryDef("")
With qdf
.ReturnsRecords = False
.Connect = oCon
.SQL = strSQL
.Execute
End With
With qdf
.ReturnsRecords = True
.Connect = oCon
.SQL = strGETID
Set rst = .OpenRecordset(dbOpenSnapshot)
gPxID = rst(0)
Debug.Print gPxID
End With
End With
这非常有效! 感谢大家的回应和帮助