ADO执行 - 编译错误:未定义用户定义的类型

时间:2010-11-19 14:27:54

标签: ms-access vba excel-vba ado jet

我有从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 个答案:

答案 0 :(得分:6)

你参考了adodb图书馆吗? (从VBE中选择工具,参考)

答案 1 :(得分:0)

我相信问题可能出现在这些类型中:

adOpenKeyset, adLockOptimistic, adCmdTable

尝试按如下方式定义它们:

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

编辑后:

很抱歉这是针对JScrip方言的,但我相信你可以将它改为VBScript。 :)