开发者

ADO Execution - compile error: User-defined type not defined

I have the following ado connection from excel to access but it does not work, am getting the error above. Any ideas?

Sub ADO_to_access()

Dim 开发者_如何学Pythondatabase 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


Did you reference the adodb library ? (From VBE, select Tools, References)


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜