Results 1 to 3 of 3
  1. #1
    Rev12 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2016
    Posts
    11

    Export to Excel On Click of a button

    Dear All,

    I have listbox name lstfilename with a code below in forms class module

    I have set the property to Multi Select,

    This list box will display all my files in my TESTFILE folder

    I need

    1. that On Click from button cmdexcel it will export ALL the file names to excel if no selection
    2. export to excel only if there were selected filenames

    Please any help, thanks in advance. Rev12

    Private Function ListFiles()


    Dim sPath As String
    Dim sFile As String
    Dim sExtension As String


    Me.lstfilename.RowSource = ""


    If Me.chkdisplayalltype Then
    sExtension = "*.*"
    Else
    sExtension = "*.pdf"
    End If


    sPath = "C:\TESTFILE"


    sFile = Dir(sPath & sExtension)


    Do While sFile <> vbNullString
    If sFile <> "." And sFile <> ".." Then
    Me.lstfilename.AddItem sFile
    End If
    sFile = Dir
    Loop


    End Function

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 10 Access 2016
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    Maybe something like this will be helpful when run from Excel

    Code:
    Sub ListAllFile()
    
    
        Dim objFSO As Object
        Dim objFolder As Object
        Dim objFile As Object
        Dim ws As Worksheet
        Dim sPath As String
        Dim lrA As Long
        Dim lrB As Long
    
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set ws = Worksheets.Add
    
    
        'Get the folder object associated with the directory
        sPath = InputBox("What is the full Path to Search?")
        Set objFolder = objFSO.GetFolder(sPath)
        ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"
        ws.Cells(1, 2).Value = "The files found have modified dates:"
        ws.Cells(1, 3).Value = "The file Size is:"
    
    
        'Loop through the Files collection
        For Each objFile In objFolder.Files
            lrA = Range("A" & Rows.Count).End(xlUp).Row
            lrB = Range("B" & Rows.Count).End(xlUp).Row
            ws.Range("A" & lrA + 1).Value = objFile.Name
            ws.Range("B" & lrB + 1).Value = objFile.DateLastModified
            ws.Range("C" & lrB + 1).Value = objFile.Size
        Next
        'ws.Cells(2, 1).Delete
        'Clean up!
        Set objFolder = Nothing
        Set objFile = Nothing
        Set objFSO = Nothing
    
    
    End Sub

  3. #3
    NTC is offline VIP
    Windows 10 Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    tricky stuff; the prior post may be spot on...... I don't like multi select Listboxes as they are a hassle to work with under more simple designs.

    If you modify your module to write your list to a table, rather than a listbox control. Then the Listbox control source of a table is a more standard design. Meaning that: IF isnull(Listbox) THEN .... export entire table to excel - this is simple enough.

    Then the question is how to use a multi select box value as criteria in a query - you can Bing around for Q&A on this topic - the controls value is going to be non normal i.e.: this, that

    Because I dislike multi select Listbox controls - I myself would probably use that table that I suggest hold the list, add a second yes/no field - - and then display this so the users can put a check box in that field.... then querying this table becomes straight forward....

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

Similar Threads

  1. Export to Excel Button
    By New.User in forum Forms
    Replies: 17
    Last Post: 04-11-2018, 02:29 PM
  2. Stored Procedure output to excel file on button click
    By anavagomez in forum Programming
    Replies: 3
    Last Post: 05-24-2017, 09:24 PM
  3. command button to export to excel
    By jains in forum Forms
    Replies: 5
    Last Post: 06-30-2015, 06:27 PM
  4. Export to excel on button click
    By Jim.H. in forum Access
    Replies: 2
    Last Post: 01-29-2012, 12:16 PM
  5. Export Excel data to access on a button click
    By Grooz13 in forum Import/Export Data
    Replies: 0
    Last Post: 03-15-2011, 10:02 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