Results 1 to 5 of 5
  1. #1
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14

    Write Multiple file names to a text box

    Good Morning,



    I'm trying to use a button to bring in multiple file names (chosen by the user) to a text box so I can then split the string by 'new line character' and use the .LoadFromFile method to attach the files to a new record. When I click the button with the code below nothing happens, however if I remove the while loops and arrays, I'm able to do it with one file. I've been banging my head against my desk and trying to play around with it but I can't make it work. Please help!

    This code works to bring the name of one file:
    Code:
    Private Sub cmdBrowse_Click()
        Dim filepath()
        Dim fd As FileDialog
        
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        
        With fd
            .AllowMultiSelect = True
            .Title = "Please select file to attach"
            If .show = True Then
                    filepath = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
        Set fd = Nothing
        If Len(filepath) = 0 Then
            Debug.Assert "No file selected"
            Exit Sub
        End If
        
        Me.Attachments = filepath
        
    End Sub
    This Code does NOT work:
    Code:
    Private Sub cmdBrowse_Click()
        Dim i, temp
        Dim filepath()
        Dim fd As FileDialog
        
        i = 1
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        
        With fd
            .AllowMultiSelect = True
            .Title = "Please select file to attach"
            If .show = True Then
                While .SelectedItems(i) <> ""
                    filepath(i) = .SelectedItems(i)
                Wend
            Else
                Exit Sub
            End If
        End With
        Set fd = Nothing
        If Len(filepath) = 0 Then
            Debug.Assert "No file selected"
            Exit Sub
        End If
        
        i = 1
        While filepath(i) <> ""
            temp = Me.Attachments
            Me.Attachments = temp & filepath(i) & vbcrlf
        Wend
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,511
    You wouldnt put multiple items in a single text box...they would go into an array or collection (better).

    collFiles.add .SelectedItems(i)

    then just loop thru the collection
    LoadFromFile collFiles(i)

  3. #3
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14
    I will definitely try that thanks! It's going to be 2 different subroutines though. The first I want to just display the file names on the form and allow the user to fill out the rest of the fields for the record, and then a second to add the record to the table. Is there a way to maintain the values in a collection between the first subroutine and the second?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,752
    Options:

    1. write the items to a listbox

    2. globally declare the array then it would be available for reference from anywhere - be aware that code execution interruption empties variables
    I have used array like this but no experience with custom collection
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14
    Thanks! Writing to a listbox worked like a charm. Appreciate the help!

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

Similar Threads

  1. Replies: 2
    Last Post: 07-09-2014, 12:43 PM
  2. Exporting Multiple Queries to a single Text File
    By sam.eade in forum Import/Export Data
    Replies: 6
    Last Post: 05-13-2014, 09:24 AM
  3. Replies: 2
    Last Post: 11-05-2013, 04:14 PM
  4. Replies: 5
    Last Post: 08-23-2012, 11:20 AM
  5. How do you store multiple file locations in a text box
    By ChuckColeman1812 in forum Access
    Replies: 3
    Last Post: 12-16-2011, 10:46 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