Results 1 to 2 of 2
  1. #1
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82

    Import with filedialog and export with filedialog in same sub

    Hi all

    I am having issues with this piece of code. What I need it to do is onClick:



    Open the file dialog (msoFileDialogFilePicker) and import an excel file to a table.
    complete a number of queries.
    Open the file dialog (msoFileDialogSaveAs) and let the user export a txt file to a location of their choosing.

    I have copied code from multiple sources so the code has become convoluted. I am sure there is a cleaner way of writing this. Can any one help.

    Code:
    Private Sub btnImport_Click()
        Dim sMyPathIn As FileDialog
        Dim sPathIn As Variant
        
        Set sMyPathIn = Application.FileDialog(msoFileDialogFilePicker)
            
        With sMyPathIn
          .AllowMultiSelect = False
          .Title = "Select your File"
          .Filters.Add "All Files", "*.*"
            If .Show = -1 Then
                For Each sPathIn In .SelectedItems
                On Error GoTo ErrHandler
                    'Do stuff (run queries)
                Next sPathIn        
            Else
                sPathIn = "No File Selected to Import."
                MsgBox sPathIn
            End If
        End With
        
        Dim sMyPathOut As FileDialog
        Set sMyPathOut = Application.FileDialog(msoFileDialogSaveAs)
        With sMyPathOut
            .InitialFileName = "testFile_" & Format(Date, "yyyy.mm.dd") & ".txt"
            .Show
        End With
        
        
        Exit Sub
    ErrHandler:
        MsgBox "The process was not complete. Contact Admin.", vbCritical, "Error"
        Exit Sub
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Code:
    'user picks a file
    'usage:
    txtBox = UserPick1File("c:\folder\")
    docmd.transferSpreadsheet acImport, txtBox , ...etc
    'save to text
    docmd.transfertext  acExport , ...etc
    end sub
     
    
    Public Function UserPick1File(Optional pvPath)
    Dim strTable As String
    Dim strFilePath As String
    Dim sDialog As String, sDecr  As String, sExt As String
    
    '===================
    'YOU MUST ADD REFERENCE : Microsoft Office xx.0 Object Library, in vbe menu, TOOLS, REFERENCES
    '===================
    With Application.FileDialog(msoFileDialogFilePicker)   
        .AllowMultiSelect = False
        .Title = "Locate a file to Import"
        .ButtonName = "Import"
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls;*.xlsx"
        .Filters.Add "All Files", "*.*"
        .InitialFileName = "c:\"
        .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
        
            If .Show = 0 Then
               'There is a problem
               Exit Function
            End If
        
        'Save the first file selected
        UserPick1File = Trim(.SelectedItems(1))
    End With
    End Function

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

Similar Threads

  1. FileDialog with filters
    By fritesmodern in forum Programming
    Replies: 6
    Last Post: 05-08-2015, 08:28 AM
  2. Replies: 43
    Last Post: 03-27-2014, 01:51 AM
  3. FileDialog Question
    By uaguy3005 in forum Programming
    Replies: 5
    Last Post: 06-27-2012, 02:01 PM
  4. Using FileDialog to export to Excel
    By zapouk in forum Import/Export Data
    Replies: 2
    Last Post: 04-20-2012, 04:31 AM
  5. FileDialog
    By Tomfernandez1 in forum Programming
    Replies: 9
    Last Post: 04-13-2011, 03:45 PM

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