将Excel连接到Access - VBA

时间:2012-12-17 08:11:56

标签: vba ado

执行以下代码和

时,出现“用户定义的类型未定义”错误
  

“objCon As ADODB.Connection”

在第一行突出显示。 我试图通过VBA代码设置从Excel到Access的连接。谢谢你的任何建议!

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

1 个答案:

答案 0 :(得分:6)

您可以执行以下某项操作

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

或者在VBA编辑器中 工具 - >参考 - > 添加Microsoft ActiveX Data Object X.Y Library