I have the following ado connection from excel to access but it does not work, am getting the error above. Any ideas?
我有从excel到访问的以下ado连接但它不起作用,我得到上面的错误。有任何想法吗?
Sub ADO_to_access()
Dim database As New ADODB.Connection // ERROR HERE
Dim connectionstring As String
Dim NewSet As Recordset
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveSheet
Set objaccess = Nothing
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\Carlos\Desktop\VBA - CW - Database.mdb;"
database.Open connectionstring
' ************* MEN
Set NewSet = New ADODB.Recordset
NewSet.Open "Mens_Dept_Data", database, adOpenKeyset, adLockOptimistic, adCmdTable
x = 6
Do While Len(Range("P" & x).Formula) > 0
With NewSet
.AddNew
.Fields("Irina").Value = CurrentSheet.Range("P" & x).Value
.Fields("Thomas").Value = CurrentSheet.Range("Q" & x).Value
.Fields("Jackie").Value = CurrentSheet.Range("R" & x).Value
.Update
End With
x = x + 1
Loop
NewSet.Close
database.Close
End Sub
2 个解决方案
#1
6
Did you reference the adodb library ? (From VBE, select Tools, References)
你参考了adodb图书馆吗? (从VBE中选择工具,参考)
#2
0
I belive that the problem might be in those types:
我相信问题可能出现在以下类型中:
adOpenKeyset, adLockOptimistic, adCmdTable
Try to define them as follows:
尝试按如下方式定义它们:
var adOpenForwardOnly = 0, adOpenKeyset = 1, adOpenDynamic = 2, adOpenStatic = 3; //CursorType Values
var adLockReadOnly = 1, adLockPessimistic = 2, adLockOptimistic = 3, adLockBatchOptimistic = 4; //LockTypeEnum Values
var adStateClosed = 0, adStateOpen = 1, adStateConnecting = 2, adStateExecuting = 4; //ObjectStateEnum Values
var adUseServer = 2, adUseClient = 3 //CursorLocationEnum Values
var adCmdTable = 2 //CommandTypeEnum Values
After Edit:
编辑后:
Sorry this is for the JScrip dialect, but I am sure that you can chang it to VBScript. :)
对不起,这是针对JScrip方言的,但我相信你可以将它改为VBScript。 :)
#1
6
Did you reference the adodb library ? (From VBE, select Tools, References)
你参考了adodb图书馆吗? (从VBE中选择工具,参考)
#2
0
I belive that the problem might be in those types:
我相信问题可能出现在以下类型中:
adOpenKeyset, adLockOptimistic, adCmdTable
Try to define them as follows:
尝试按如下方式定义它们:
var adOpenForwardOnly = 0, adOpenKeyset = 1, adOpenDynamic = 2, adOpenStatic = 3; //CursorType Values
var adLockReadOnly = 1, adLockPessimistic = 2, adLockOptimistic = 3, adLockBatchOptimistic = 4; //LockTypeEnum Values
var adStateClosed = 0, adStateOpen = 1, adStateConnecting = 2, adStateExecuting = 4; //ObjectStateEnum Values
var adUseServer = 2, adUseClient = 3 //CursorLocationEnum Values
var adCmdTable = 2 //CommandTypeEnum Values
After Edit:
编辑后:
Sorry this is for the JScrip dialect, but I am sure that you can chang it to VBScript. :)
对不起,这是针对JScrip方言的,但我相信你可以将它改为VBScript。 :)