Results 1 to 4 of 4

Thread: Sleeper: Exporting query to Excel file saved as attachment in my Access database

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Newbie
    May 2023

    Sleeper: Exporting query to Excel file saved as attachment in my Access database

    Hallo Everybodey,

    I want a vba script that export my query result to an excel file which it is saved in my access database as attachment.
    My code is working if the excel template is on the hard drive but i need to be saved as attachment.

    Dim lngColumn As Long
    Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim strPathFileName As String, strWorksheetName As String
    Dim strRecordsetDataSource As String
    Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
    blnEXCEL = False
    Set strFileName = CurrentDb.OpenRecordset("SELECT attachement.FileName FROM tbl_Attachement WHERE Id=1", dbOpenDynaset, dbseechanges)
    strRecordsetDataSource = "Select * from mytable"
    blnHeaderRow = True
    On Error Resume Next
    Set xlx = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
    End If
    On Error GoTo 0
    xlx.Visible = True
    ' Create a new EXCEL workbook
    Set xlw = xlx.Workbooks.Open("C:\Temp\Report.xlsx")
    Set xls = xlw.Worksheets("help_tab")
    'xls.Name = Trim(Left("1.1", 31))
    Set xlc = xls.Range("B2")
    Set dbs = CurrentDb()
    Set rst = dbs.OpenRecordset(strRecordsetDataSource, dbOpenDynaset, dbReadOnly)
    If rst.EOF = False And rst.BOF = False Then
          xlc.CopyFromRecordset rst
    End If
    Set rst = Nothing
    Set dbs = Nothing
    Set xlc = Nothing
    Set xls = Nothing
    Set xlw = Nothing
    If blnEXCEL = True Then xlx.Quit
    Set xlx = Nothing
    Last edited by Aussiebear; 05-08-2023 at 12:02 PM. Reason: Added code tags to supplied code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts