3

执行以下代码时出现“未定义用户定义的类型”错误,并且

“objCon 作为 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
4

1 回答 1

6

您可以执行以下操作之一

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

或者在 VBA Editor Tools-->Reference-->AddMicrosoft ActiveX Data Object X.Y Library

于 2012-12-17T08:36:42.433 回答