Results 1 to 7 of 7
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151

    Is there a better way to create this Excel file?

    Hi all

    Thanks for taking the time to read this post.

    I need to create an excel workbook with 3 sheets in it. The contents of the sheets will be determined by the value of a field in a query. The field is Region and will have a value of either N, M or B

    The result I want is that all records where Region = "N" are written to sheet1 in the spreadsheet. Records where Region = "M" are written in to sheet2 and so on...

    I have a solution that I use, but it uses 3 queries each based on the Region value, rather than 1 query with the region values in it. It works OK, but I feel that there must be a more "elegant" solution using a single query.

    It's not a "deal breaker" but I'm keen to try and be as efficient in my code as possible.



    This is my current code

    Code:
    Private Sub ExportTechRefbtn_Click()
    On Error GoTo Err_ExportTechRefbtn_Click
        Dim wsShell As Object
        Dim stDocName As String
        Dim strSaveFile As String
        strSaveFile = "C:\Temp\TechRefAppServiceContactExport.xlsx"  'location to write excel file
        
        If MsgBox("Confirm creation of output file..." & _
            vbCrLf & vbCrLf & strSaveFile & _
            vbCrLf & vbCrLf & _
            "(Folder location will open after successful creation of the file.)", vbYesNoCancel + vbQuestion, " Confirm Export") <> vbYes Then
            Exit Sub
        End If
        
        If Dir("c:\Temp\") = "" Then
            MkDir ("c:\Temp\")
        End If
            
        If Dir(strSaveFile) <> "" Then
            Kill strSaveFile
        End If
        
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TechAppExport-N", strSaveFile
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TechAppExport-M", strSaveFile
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "TechAppExport-B", strSaveFile
        
        Set wsShell = CreateObject("WScript.Shell")
        wsShell.Run "c:\Temp"
     
        Set wsShell = Nothing
           
    Exit_ExportTechRefbtn_Click:
        Exit Sub
    :confused:
    Err_ExportTechRefbtn_Click:
        If Err.Number <> 2501 Then
            If Err.Number = 70 Then
                MsgBox "The spreadsheet " & strSaveFile & " file is open and cannot be written to.", vbOKOnly + vbExclamation, "Error"
            End If
            Else
            MsgBox "Error Code " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "Error"
        End If
    
        Resume Exit_ExportTechRefbtn_Click
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Other than Excel automation, I cannot see how you can do it in one pass?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Untested but what about a single query with a criteria pointing to a form control? Your code would place each value into the control and then export. Still 3 exports but a single query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Quote Originally Posted by pbaldy View Post
    Untested but what about a single query with a criteria pointing to a form control? Your code would place each value into the control and then export. Still 3 exports but a single query.
    Hi Pbaldy
    That's an interesting thought. Currently there is no form associated with this code as it really doesn't need it. It runs from a command button on a general form that doesn't have these controls on it. I could add them, but it would be a bit superfluous.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You could also try a TempVar instead of a form control.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, and the form doesn't need to be associated with the data. I use hidden controls on main menu forms for criteria sometimes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thanks. I'll give that a try.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. create access file from excel
    By Mehdi in forum Access
    Replies: 2
    Last Post: 12-13-2019, 08:22 AM
  2. Replies: 3
    Last Post: 08-07-2019, 02:57 PM
  3. Replies: 5
    Last Post: 11-24-2017, 07:28 AM
  4. Create Excel File Through File Browser
    By kdbailey in forum Access
    Replies: 6
    Last Post: 04-21-2016, 10:56 PM
  5. create a button for import excel file
    By tggsun in forum Forms
    Replies: 3
    Last Post: 01-17-2012, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums