运行时错误'-2147352567(80020009)'无当前记录MySQL后端DAO记录集

时间:2013-10-12 14:01:37

标签: mysql ms-access dao autonumber mysql-odbc-connector

我将后端MS Access 2003升级到数据库的MySQL 5.1。 我将后端MYSQL 5.1数据库thr'ODBC(MySQL ODBC 5.1驱动程序)链接到MS Access前端.mdb。

我正在使用DAO记录集。我使用.AddNew方法添加新记录并使用

进行更新

。更新方法;在更新语句之后,我将自动编号字段提取到变量中,该变量给出了

“运行时错误”-2147352567(80020009)“无当前记录”错误。

但是同样的代码适用于以前的MS-Access 2003后端版本。

'new 
if bNew  = true then
    lngInvoiceID = 0
else 'edit , 
    lngInvoiceID = Forms("frmInvoice").[tbInvoiceID].value
end if


Set rstAux = dbsLocal.OpenRecordset("Select * from tblElectronicInvoices where
eipID = " & lngInvoiceID, dbOpenDynaset, dbSeeChanges)

rstAux.AddNew

rstAux.Update
lngInvoiceID = Nz(rstAux.Fields("eipID"), 0)

'当我尝试恢复自动编号字段的eipID时,没有当前记录错误。

以前的MS Access代码具有访问后端链接表到前端的权限。未指定dbSeeChanges的选项,在更新语句之前,我可以获取自动编号字段的新ID。

1 个答案:

答案 0 :(得分:1)

几年前,我将后端数据库从Access迁移到MySQL(在Access中保留前端)后遇到了同样的情况。我使用以下解决方法结束了:

Dim cdb As DAO.Database, qdf As DAO.QueryDef, rst As DAO.Recordset
Dim lngCustomerID As Long
Set cdb = CurrentDb

' create pass-through query to insert new row and then retrieve the IDENTITY value
Set qdf = cdb.CreateQueryDef("")
qdf.Connect = cdb.TableDefs("customers").Connect  ' get .Connect from existing linked table
qdf.ReturnsRecords = False
qdf.SQL = "INSERT INTO customers (customerName) VALUES ('GordCo')"
qdf.Execute
qdf.ReturnsRecords = True
qdf.SQL = "SELECT LAST_INSERT_ID()"
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
lngCustomerID = rst(0).Value
Debug.Print "LAST_INSERT_ID() returned " & lngCustomerID
rst.Close
Set rst = Nothing
Set qdf = Nothing
相关问题