Results 1 to 8 of 8
  1. #1
    davedinger is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Location
    South Dakota
    Posts
    63

    Default file location File Picker

    I have been working on a very simple Document Management sytem for a while now and along time ago I was given this code that allows me to click on a form button that opens the Diaogue File Picker, (Thanks for all of your help and Ideas by the way everone, your help is greatly appreciated!) When the file picker opens it then allows a User to select a document and stores the location in a field named Location. it works great!!!


    The issue I am having now is that people cant always find the doccument location so they cant store the locations. Not to mention I have people storing documents everyplace Arrrg. By default the File Picker goes to the users MY Documents Folder and the users cant fgure out how to navigate to the right Network Folder. I would like to set our scanner to scan documents to a specific network folder and then have the file picker open to that Folder by default so that it will reduce issues.
    I've researched this and played with .InitialFileName = "C:\Windows\DocumentStorage" but i cant figure out where it goes in the code.
    Can You Help?


    - - - - - - - - - - - - -

    Private Sub Command26_Click()
    Dim objFD As Object
    Dim strOut As String


    strOut = vbNullString
    'msoFileDialogFilePicker = 3
    Set objFD = Application.FileDialog(3)
    If objFD.Show = -1 Then
    strOut = objFD.SelectedItems(1)
    End If
    Set objFD = Nothing
    FolderSelection = strOut


    Dim strChoice As String
    strChoice = FolderSelection
    If Len(strChoice) > 0 Then
    Me.Location = strChoice


    Else
    ' what should happen if user cancelled selection?
    End If

    End Sub

    ---------------------

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    difficult to read your code without formatting and code tags so I'll just post what I use and I think you'll be able to see where it goes.

    Code:
    Function fBrowse(PickType As MsoFileDialogType, Optional StartFolder As String = "", Optional strTitle As String = "") As String
    
        On Error GoTo fBrowse_Error
    
        Dim mso As Object
        Dim varFile As Variant
    
        If Nz(strTitle, "") = "" Then
    
            Select Case PickType
    
            Case 3
                strTitle = "Select a File"
            Case 4
                strTitle = "Select a Folder"
            Case Else
                strTitle = ""
    
            End Select
    
        Else
    
            strTitle = strTitle
    
        End If
    
        Set mso = Application.FileDialog(PickType)
    
        With mso
    
            .Title = strTitle
            .AllowMultiSelect = False
            .InitialFileName = StartFolder
            .Filters.Clear
    
    If PickType = msoFileDialogFilePicker Then
            
            .Filters.Add "All Files", "*.*"
            .Filters.Add "PDF file", "*.pdf"
            .Filters.Add "Image Files", "*.jpg,*.jpeg,*.BMP,*.Png,*.TIFF,"
            .Filters.Add "Audio Files", "*.Wav,*.mp4"
    
    End If
            
            If .Show = True Then
    
                If .SelectedItems.Count = 0 Then
    
                    fBrowse = ""
    
                End If
    
                For Each varFile In .SelectedItems
    
                    fBrowse = varFile
    
                Next
    
            Else
    
                fBrowse = ""
    
            End If
    
        End With
    
    On Error GoTo 0
    
        Exit Function
    
    fBrowse_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fBrowse, line " & Erl & "."
    
    End Function
    Btw, suggest you write a public function to handle this kind of procedure, or use mine or any other you can find.
    Then in the button click on your form you only need
    Code:
    Me.Location = fBrowse(msoFileDialogFilePicker, "C:\Windows\DocumentStorage")
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Lookup msoFileDialogFilePicker to see all of its properties and nuances. What you want is called .InitialFileName. Your code could test that the returned path or part of it matches what you want it to be. And yes, please do use code tags (# button on posting toolbar) to maintain indentation and readability.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    davedinger is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Location
    South Dakota
    Posts
    63
    Sorry I didnt know about the Code Icon. Let me try the code again. As I said before this works great but it just defaults to the wrong Folder. I hope I'm using the Code Tag below correctly.

    Code:
    Private Sub Command26_Click()
    Dim objFD As Object
    Dim strOut As String
    
    
    strOut = vbNullString
    'msoFileDialogFilePicker = 3
    Set objFD = Application.FileDialog(3)
    If objFD.Show = -1 Then
    strOut = objFD.SelectedItems(1)
    End If
    Set objFD = Nothing
    FolderSelection = strOut
    
    
    Dim strChoice As String
    strChoice = FolderSelection
    If Len(strChoice) > 0 Then
    Me.Location = strChoice
    Else
    ' what should happen if user cancelled selection?
    End If
    
    End Sub
    

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,008
    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

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Thank you for choosing to implement code tags - close, but no indentation, which is a big help.
    I don't think you followed my suggestion to research the dialog or you'd know what you need?

    Playing with your code I have drafted the following as a starting point (and it shows how indentation should look)
    Code:
    Private Sub Command26_Click() 'should give your controls meaningful names
    Dim objFD As Object
    Dim strOut As String
    
    'msoFileDialogFilePicker = 3
    Set objFD = Application.FileDialog(3)
    With objFD
     'set dialog properties here, like 
       .InitialFileName = ??
       MultiSelect = ??
       .etc ??
    End With
    
    If objFD.Show Then
       'do stuff such as
       strOut = objFD.SelectedItems(1) 'this returns path of selected file
       'do more stuff?
    Else
       Exit Sub 'no point in continuing if user chose Cancel
    End If
    
    ' no point in testing if user chose a file because if you have already exited, they didn't
    
    Set objFD = Nothing
    
    End Sub
    Also no need to pass selection data from one variable to another (strChoice = FolderSelection) so I eliminated that situation.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    davedinger is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Location
    South Dakota
    Posts
    63
    Thats that ticket! Thanks Micron. Your comments in the code helped allot. I can see how the code works now
    I also added a line that sent the selection to a field on my form named "Location"

    The final code is below. Thanks again.

    Code:
    Private Sub Command26_Click()
    
       Dim objFD As Object
    Dim strOut As String
    
    
    'msoFileDialogFilePicker = 3
    Set objFD = Application.FileDialog(3)
    With objFD
     'set dialog properties here, like
       .InitialFileName = "C:\Users\Dave\Desktop\Hawaii Photos\Hawaii David"
     
    End With
    
    
    If objFD.Show Then
       'do stuff such as
       strOut = objFD.SelectedItems(1) 'this returns path of selected file
       Me.Location = strOut 'This Sends the file selection to the Field named Location
       
    Else
       Exit Sub 'no point in continuing if user chose Cancel
    End If
    
    
    ' no point in testing if user chose a file because if you have already exited, they didn't
    
    
    Set objFD = Nothing
    
    
    End Sub

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Glad to help and know that you got something working. But I think I'd eliminate some of the comments I interjected as information for you.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 20
    Last Post: 06-19-2022, 11:19 AM
  2. File Picker To Only Allow Single File Section
    By jo15765 in forum Programming
    Replies: 2
    Last Post: 05-29-2017, 04:25 PM
  3. Replies: 3
    Last Post: 07-30-2012, 02:16 PM
  4. VBA - file picker
    By riteoh in forum Programming
    Replies: 1
    Last Post: 10-12-2010, 06:30 AM
  5. bypass file picker?
    By techneophyte in forum Programming
    Replies: 6
    Last Post: 08-17-2010, 11:12 AM

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
  •  
Other Forums: Microsoft Office Forums