将 Excel 连接到 Access - VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13910450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:50:45  来源:igfitidea点击:

Connect Excel to Access - VBA

vbaado

提问by Sam

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

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

"objCon As ADODB.Connection"

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

在第一行突出显示。我正在尝试通过 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

回答by Larry

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

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