VBA - Generate Excel File from Access (QueryTable)

后端 未结 3 610
梦谈多话
梦谈多话 2021-02-09 00:08

I have a project that basically the goal is to generate Excel (Report) starting the Click of a button in Access using VBA.

The contents of this report is the result of a

相关标签:
3条回答
  • 2021-02-09 00:19

    You don't say what Office version, but in Excel 2007/10 a QueryTable is a property of a Listobject so your code would be like:

    With MeuExcel.Worksheets.ListObjects.Add(Connection:=rs, Destination:=Range("A2")).QueryTable
    
    0 讨论(0)
  • 2021-02-09 00:28

    In Access, you need to prefix the Excel application objects with the Excel application instance, for example:

    With MeuExcel.Worksheets(4).QueryTables.Add( _
        connection:=recordset, _
        Destination:=Range("A2"))
    End With
    

    Furthermore, unless you have a reference to the Excel library, ypu will need to provide the value for built-in Excel constants.

    It is a very bad idea to use the name of objects for variables. Do not say:

    Dim recordset As recordset
    Set recordset = New recordset
    

    Say, for example:

    Dim rs As recordset
    

    Or much better:

    Dim rs As New ADODB.Recordset
    

    If you have a suitable reference. You can then skip CreateObject.

    EDIT

    The provider must be the Access OLEDB 10 provider, as used to bind recordsets. This works for me to create a data table via Access using SQL Server:

    strConnect = "Provider=Microsoft.Access.OLEDB.10.0;Persist Security Info=True;" _
    & "Data Source=XYZ\SQLEXPRESS;Integrated Security=SSPI;" _
    & "Initial Catalog=TestDB;Data Provider=SQLOLEDB.1"
    
    0 讨论(0)
  • 2021-02-09 00:45

    FWIW, two things stand out:

    1. As @Remou pointed out, Excel references need to be qualified. Currently, Range("A2") is unqualified. When running the code in Excel, the ActiveSheet is assumed. However, when running from another application, that application will look for a method or property in its own library called Range, which will give you that error in Microsoft Access.

    2. There isn't any code in the With block, so you can remove the With and End With keywords; when you do this also remove the outer (), like this:

    wb.Worksheets(4).QueryTables.Add Connection:=rs, Destination:=wb.Worksheets(4).Range("A2")

    Alternatively, shift the With block to the Worksheet level:

    With wb.Worksheets(4)
        .QueryTables.Add Connection:=rs, Destination:=.Range("A2")
    End With
    

    Update—Access to Excel Sample

    This sample code automates Excel from Access, creating a new workbook and adding a Querytable to the first sheet. The source data is an Access table. This runs in Office 2007.

    Public Sub ExportToExcel()
      Dim appXL As Excel.Application
      Dim wbk As Excel.Workbook
      Dim wst As Excel.Worksheet
      Dim cn As ADODB.Connection
      Dim rs As ADODB.Recordset
    
      Set appXL = CreateObject("Excel.Application")
      appXL.Visible = True
      Set wbk = appXL.Workbooks.Add
      Set wst = wbk.Worksheets(1)
    
      Set cn = CurrentProject.AccessConnection
      Set rs = New ADODB.Recordset
      With rs
        Set .ActiveConnection = cn
        .Source = "SELECT * FROM tblTemp"
        .Open
      End With
    
      With wst
        .QueryTables.Add Connection:=rs, Destination:=.Range("A1")
        .QueryTables(1).Refresh
      End With
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题