Sam
Sam

Reputation: 3167

Connect Excel to Access - VBA

I get a "User-defined type not defined" error when I execute the below code, and the

"objCon As ADODB.Connection"

is highlighted on the first line. I am trying to set a connection from Excel to Access via VBA code. Thank you for any advice!

Private objCon As ADODB.Connection
Private rstRec As ADODB.Recordset
Private strQry

Sub Connect()
   Dim strConn As String


   Set objCon = New ADODB.Connection
   objCon.Mode = adModeReadWrite

      If objCon.State = adStateClosed Then
         strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\DB\Db.accdb;Persist Security Info=False;"
         objCon.ConnectionString = strConn
         objCon.Open
      End If
End Sub

Upvotes: 3

Views: 10589

Answers (1)

Larry
Larry

Reputation: 2794

You can do one of the following

set objCon  = CreateObject("ADODB.Connection")
set rstRec = CreateObject("ADODB.Recordset")

Or in VBA Editor Tools-->Reference--> Add Microsoft ActiveX Data Object X.Y Library

Upvotes: 6

Related Questions