VBA不断尝试引用SQL数据库时出错

我的程序基本上打开任何.MDB文件,然后显示在电子表格中的文件,但是我不断收到此错误(尽pipe代码工作,并在Excel中打开文件)。

出现的错误是“运行时错误429,Active X组件无法创build对象”

这里是我有的代码,我会写在上面,当我点击debugging时突出显示的行。

Option Explicit Private Sub CommandButton1_Click() Dim strFileToOpen As String ChDir ThisWorkbook.Path strFileToOpen = Application.GetOpenFilename _ (Title:="Please choose a file to open", _ FileFilter:="All Files *.* (*.*),") Workbooks.Open fileName:=strFileToOpen frmMain.TextBox1.Text = strFileToOpen Exit Sub End Sub Private Sub CommandButton2_Click() Dim fileName As String Dim copyDestination As String Dim copyFile As String Dim fso If frmMain.TextBox1.Value = "" Then MsgBox "No File Selected" Else fileName = frmMain.TextBox1.Value copyDestination = ThisWorkbook.Path & "\Sales_Orders.mdb" Set fso = CreateObject("Scripting.FileSystemObject") fso.copyFile fileName, copyDestination, True Set fso = Nothing MsgBox "File copied to current workbook directory!" End If 'SQL Statements Dim db As Database Dim qdef As QueryDef Dim td As TableDef Dim dbname As String ' Open the database. replace "c:\DBfile.mdb" with your ' database file name 

'当我debugging时,这行设置DB = OPENDATABASE被突出显示

 Set db = OpenDatabase(copyDestination) ' List the table names. For Each td In db.TableDefs ' if you want to display also the system tables, replace the line ' below with: List1.AddItem td.Name If td.Attributes = 0 Then ListBox1.AddItem td.Name Next td db.Close End Sub Private Sub CommandButton3_Click() Unload Me End Sub 

如代码中所述,Set db = OpenDatabase(copyDestination)是当我点击debugging时突出显示的行。 任何想法为什么这是怎么回事?

OpenDatabase方法是Workbooks对象的成员。 如果没有对Workbooks对象的引用,就不能特别调用它。

 Dim db as Object Set db = Workbooks.OpenDatabase(copyDestination) 

db声明为对象,而不是数据库。