通过VBA将访问查询复制到Excel(错误3343)

时间:2016-07-04 14:22:02

标签: excel vba excel-vba ms-access

我正在尝试复制访问查询的结果并将其粘贴到Excel选项卡中。我已经google了但似乎无法让它工作,我得到错误“错误3343:无法识别的数据库格式”所以我认为它与我检查的引用有关。

有没有人知道我需要正确的参考资料才能让它发挥作用?

  

参考文献:

     

Visual Basic For Application

     

Microsoft Excel 14.0对象库

     

OLE自动化

     

Microsoft Office 14.0对象库

     

Microsoft ActiveX数据对象2.8库

     

Microsft DAO 3.6对象库

ax = gca;                        % gets the current axes
ax.XAxisLocation = 'origin';     % sets them to zero
ax.YAxisLocation = 'origin';     % sets them to zero
ax.Box = 'off';                  % switches off the surrounding box
ax.XTick = [-3 -2 -1 0 1 2 3];   % sets the tick marks
ax.YTick = [-1 -0.5 0 0.5 1];    % sets the tick marks

2 个答案:

答案 0 :(得分:1)

在初始化Database和Recordset对象之前,请考虑调用Access对象。另外,使用OpenCurrentDatabase方法作为OpenDatabase用于DBEngine工作区对象。

Sub Query()
    Dim accObj As Object
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim sql As String
    Dim iCol As Integer

    Sheets("DataDump1").Cells.ClearContents

    Set accObj = CreateObject("Access.Application")
    accObj.OpenCurrentDatabase("C:\Folder\DatabaseName.accdb")

    Set db = accObj.CurrentDb
    Set rst = db.OpenRecordset("Query 1")

    For iCol = 1 To rst.Fields.Count
        Sheets("DataDump1").Cells(1, iCol) = rst.Fields(iCol - 1).Name
    Next iCol

    Sheets("DataDump1").Range("A2").CopyFromRecordset rst
    rst.Close
    db.Close

    Set rst = Nothing
    Set db = Nothing
    Set accObj = Nothing

End Sub

或者,不需要与Access对象接口,因为Access不仅是一个数据库而是一个.exe,因此可以通过ODBC / OLEDB连接,就像任何其他RDMS(Oracle,SQL Server,MySQL等)一样。

Sub RunSQL()
    Dim conn As Object, rst As Object
    Dim strConnection As String, strSQL As String
    Dim iCol As Integer

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    Sheets("DataDump1").Cells.ClearContents

'    strConnection = "DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};" _
'                      & "DBQ=C:\Folder\DatabaseName.accdb;"
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                       & "Data Source='C:\Folder\DatabaseName.accdb';"

    strSQL = " SELECT * FROM [Query 1];"

    ' OPEN DB AND RECORDSET
    conn.Open strConnection
    rst.Open strSQL, conn

    ' COLUMN HEADERS
    For iCol = 1 To rst.Fields.Count
        Sheets("DataDump1").Cells(1, iCol) = rst.Fields(iCol - 1).Name
    Next iCol

    ' DATA ROWS
    Sheets("DataDump1").Range("A2").CopyFromRecordset rst

    rst.Close
    conn.Close    
End Sub

答案 1 :(得分:0)

我认为引用问题会给用户定义的类型无法识别错误。 ADODB而不是DAO应该可以工作:

Sub Query()
Dim db As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim iCol As Integer

db.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Folder\DatabaseName.accdb;"
rst.Open "Query 1", db

For iCol = 1 To rst.Fields.Count
 ActiveSheet.Cells(1, iCol) = rst.Fields(iCol - 1).Name
 Next iCol

ActiveSheet.Range("A2").CopyFromRecordset rst
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing

End Sub

编辑:请添加最新的microsoft activex数据对象库作为此工作的参考