我正在尝试使用VBA将Excel 2010中的4个单元格更新到MS Access 2010以下代码。问题是,有时它会更新表记录中的那些字段,有时则不会。我想知道是否有人可以对我的代码有所了解导致不一致的原因。
'Add a reference to the Microsoft ActiveX Data 2.8 or later Object Library
'via the Tool | References... in the VB-editor
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stDB As String
Dim stCon As String
'Instantiate the ADO COM's objects.
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
'Pathway and name of the database
stDB = "P:\Quote Log.mdb"
'Create the connectionstring.
stCon = "Provider=Microsoft.Ace.OLEDB.12.0; Persist Security Info = False;" & _
"Data Source=" & stDB & ";"
'Open the connection
cnt.Open stCon
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Find and Update the record in Access
With rst
.Index = "PrimaryKey"
.CursorLocation = adUseServer
.Open "tbQuote", cnt, 1, 3, adCmdTableDirect
.Seek Range("dx32").Value
If Not rst.EOF Then
.Fields("QuoteNum") = Sheets("Quote Notice").Range("dx32").Value
.Fields("OtherCost") = Sheets("Cost").Range("b3").Value
.Fields("StocklistCost") = Sheets("Cost").Range("b4").Value
.Fields("DesignHrs") = Sheets("Cost").Range("b5").Value
.Fields("ProductionHrs") = Sheets("Cost").Range("b6").Value
.Update
Else
MsgBox "No such record...not updating."
End If
End With
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Close the recordset and close the connection.
rst.Close
cnt.Close
Set cnt = Nothing
ExitSub:
Set rs = Nothing
'...and set to nothing if no such record
Exit Sub
谢谢,
hughv
答案 0 :(得分:0)
好的,发现了我的问题......这一行需要正确引用工作表和单元格。 。查询范围(“dx32”)。值
于: .seek工作表(“报价通知”)。范围(“dx32”)。值