Search code examples
ms-accessvbaexceladojet

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 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

Solution

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