Excel 微软.ACE.OLEDB.12.0;可能的连接字符串格式错误导致“错误”;找不到可安装的ISAM文件;错误?

Excel 微软.ACE.OLEDB.12.0;可能的连接字符串格式错误导致“错误”;找不到可安装的ISAM文件;错误?,excel,vba,ms-word,connection-string,Excel,Vba,Ms Word,Connection String,我遇到问题的宏是用Word VBA编辑器编写的,它是由Word提供的。 它的目标是通过Microsoft.ACE.OLEDB.12.0链接到Excel.xlsm文件(作为客户数据库处理);连接,甚至在不打开文件的情况下,将整个工作表从该文件(sheet name=“data”)读取到一个动态数组中,该数组将在以后进行搜索 我写了一些代码来实现这一点,但是它在连接字符串上崩溃了,可能是因为我不知道如何格式化它-我的意思是,我只是不知道它们是如何放置的以及为什么 最重要的是,代码是在Word VBA

我遇到问题的宏是用Word VBA编辑器编写的,它是由Word提供的。

它的目标是通过Microsoft.ACE.OLEDB.12.0链接到Excel.xlsm文件(作为客户数据库处理);连接,甚至在不打开文件的情况下,将整个工作表从该文件(sheet name=“data”)读取到一个动态数组中,该数组将在以后进行搜索

我写了一些代码来实现这一点,但是它在连接字符串上崩溃了,可能是因为我不知道如何格式化它-我的意思是,我只是不知道它们是如何放置的以及为什么

最重要的是,代码是在Word VBA编辑器中编写的,并且是从Word.docm文件中获取的

Option Explicit

Private Sub UseADOSelect()

Dim connection As New ADODB.connection
Dim recSet As New ADODB.Recordset    'All the results of the query are placed in a record set;

Dim exclApp As Excel.Application
Dim exclWorkbk As Excel.Workbook
Dim mySheet As Excel.Worksheet
Dim wordDoc As Word.Document
Dim cntCntrl As ContentControl
Dim strPESELfromWord As String
Dim strQuery As String
Dim intWiersz As Integer
Dim intRemainder As Integer
Dim intRow As Integer
Dim arraySize As Integer
Dim strSexDigit As String
Dim strArray() As String
Dim i As Integer


'Set exclApp = GetObject(, "Excel.Application")   'When no Excel Workbook is open an error 429 will be returned.
'Debug.Print exclApp

Set wordDoc = Word.ActiveDocument
Debug.Print wordDoc

'Set mySheet = exclApp.ActiveWorkbook.ActiveSheet
'Debug.Print mySheet.Name

'Set mySheet = exclApp.ActiveWorkbook.Sheets("sample")
'Debug.Print mySheet.Name

strPESELfromWord = Trim(Selection.Text)
Debug.Print strPESELfromWord
Word.Application.Visible = True

strSexDigit = Mid(strPESELfromWord, 10, 1)      'Extract 10th digit from PESEL number
Debug.Print strSexDigit
intRemainder = strSexDigit Mod 2
Debug.Print intRemainder

connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm"; & _
                Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"            'something is wrong in the syntax
                                


'strQuery = "SELECT * From [Simple$]"   '[Simple$] is the table name; in this case it's the sheet name;
strQuery = "SELECT * From [data$]"     '[data$]   is the table name; in this case it's the sheet name;
recSet.Open strQuery, connection
Debug.Print " RecordCount = " & recSet.RecordCount
arraySize = recSet.RecordCount

ReDim strArray(1 To arraySize)
For i = 1 To arraySize
   strArray(i) = recSet(i)
Next i


intRow = 2
为什么它是一个
私有子
??我能把它改成潜艇吗??这不是我写的。我从下载了这段代码

我从中获得了连接字符串

我也读过,但他们讨论了不同的代码结构,我无法将答案转换到我自己的示例中

Set Conn = New ADODB.Connection
With Conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & strWorkbook & "; Extended Properties=""Excel 12.0 Macro; HDR=YES"""
.Open
End With

您认为此处的语法有错误,这是正确的:

connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm"; & _
            
                Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"        
因为字符串的格式不正确。至少,您需要将其更改为:

connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;" & _
            
                "Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"        
(若要对此进行分解,请希望连接字符串如下所示:

Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;Extended Properties="Excel 12.0 Macro;HDR=YES;IMEX=1;";
connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;" & "Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"
然后在VBA中,您需要它看起来像这样

Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";
Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;
然后,如果你像这样把它分成两部分

Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";
Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;
扩展属性=“Excel 12.0宏;HDR=YES;IMEX=1;”

你可以想象你的作业应该是这样的:

Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;Extended Properties="Excel 12.0 Macro;HDR=YES;IMEX=1;";
connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;" & "Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"
在VBA中,如果要将一条语句拆分为两行,请在第一行末尾加一个空格,后跟“u”,这意味着您将得到以下结果:

connection.Open "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = X:\Roesler\Excel\FW 1\customer's_dummy_data.xlsm;" & _
"Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1;"";"
(在这种情况下,我不确定您是否需要这些扩展属性)

除此之外,你的

strQuery = "SELECT * From [data$]"
适用于名为“数据”的工作表

很好

但您不能假设该查询将实际返回工作表中的记录数。您在recSet.RecordCount中看到的内容部分取决于“游标”的类型您可以指定打开记录集的时间。例如,当我在此处测试时,recSet.RecordCount为-1。为了查看任何数据,我必须考虑测试
recSet.EOF
,并使用
recSet.MoveNext
移动到数据集中的第一条记录

我希望这里的其他人有一个通过ADO访问Excel访问的可靠模式,你可以直接插入你的代码。但是我不需要。但是,当你使用ADO时,你几乎肯定需要处理的事情是,当你试图取回字段值时,你必须考虑到它们可能是“null”的可能性。,尝试使用空值可能会引发错误


关于“private sub”的问题,不一定非要是private sub.

才能最终找到导致我的系统崩溃的原因的答案..当问题是在字符串中的字符串周围加引号时。这在其他任何地方都没有清楚显示

我花了几个小时在这上面,我得到的只是Msgbox 以防图像无法通过,这是错误状态。 “错误 Microsoft Access数据库引擎-->找不到可安装的ISAM。“

对于一个简单的语法错误来说,这是一个非常烦人和令人费解的不合逻辑的消息

在结尾处缺少“;”;“主要是“;”的

即使是在Excel 2013上,以下内容也显示了我 引用上述同一网站 Xlsm文件 连接到具有Xlsm文件扩展名的Excel 2007(及更高版本)文件。这是启用宏的Office Open XML格式

Provider=Microsoft.ACE.OLEDB.12.0;数据源=c:\myFolder\myExcel2007file.xlsm;扩展属性=“Excel 12.0宏;HDR=YES”


“HDR=Yes;”表示第一行包含列名,而不是数据。“HDR=No;”表示相反。

顺便提一下,我忘记了你问题中的“Private Sub”部分。它不必是Private Sub。