VBA - Generate Excel File from Access (QueryTable)

后端 未结 3 603
梦谈多话
梦谈多话 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: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"
    

提交回复
热议问题