2013-10-12 82 views
1

我將後端MS Access 2003升級到數據庫的MySQL 5.1。 我將後端MYSQL 5.1數據庫thr'ODBC(MySQL ODBC 5.1驅動程序)鏈接到MS Access前端.mdb。運行時錯誤'-2147352567(80020009)'沒有當前記錄MySQL後端DAO記錄集

我正在使用DAO記錄集。我使用.AddNew方法添加新記錄並使用更新

.Update方法;在更新語句之後,我將自動編號字段提取到變量中,該變量給出

「運行時錯誤」-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

幾年前,在將後端數據庫從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