Results 1 to 4 of 4
  1. #1
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101

    FileDialog pass starting Folder as a variable

    I have the following in my DB and it works as long a s the line .InitialFileName is a hard coded string. I would however like to be able to pass a variable to the function that hones in on the proper directory a little closer. I'm tryin g to pass a variable and use it in .InitialFileName= Any one know if/How to do this???

    Public Function fGetPath(Optional vStartFolder As String) As String
    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Dim strStartFolder As String
    strStartFolder = vStartFolder
    strStartFolder = "C:\Users\bbrazeau\Desktop\MyDB\" & strStartFolder
    Set f = Application.FileDialog(3)
    MsgBox (strStartFolder)
    With f
    .InitialFileName = "C:\Users\bbrazeau\Desktop\MyDB\"
    f.AllowMultiSelect = True
    If f.Show Then
    For Each varItem In f.SelectedItems


    strFile = Dir(varItem)
    strFolder = Left(varItem, Len(varItem) - Len(strFile))
    MsgBox "Folder: " & strFolder & vbCrLf & _
    "File: " & strFile
    fGetPath = varItem
    Next
    End If
    End With
    Set f = Nothing
    End Function

  2. #2
    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 add a second variable to the function declaration, just like vStartFolder (though if you use Optional I'd test it for a value before using).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101
    Hi PBaldy, Thanks for the reply. I do have a second variable "strStartFolder", and I did test it's output, the msgbox displays the correct string, but if I replace .InitialFileName = "C:\Users\bbrazeau\Desktop\MyDB\" with .InitialFileName = strStartFolder
    I get method not supported

  4. #4
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101
    Hi again, I got it! It was simple. Trick was to just use the variable as passed. No need to assign it to a local variable I used it as "vStartFolder" and it worked just fine.
    What this does is put the user in the proper sub directory of the current customer directory, which keeps the user from having to remember and navigate to the correct directory. Less error prone.

    Public Function fGetPath(vStartFolder As String) As String
    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Set f = Application.FileDialog(3)
    With f
    .InitialFileName = vStartFolder
    f.AllowMultiSelect = True
    If f.Show Then
    For Each varItem In f.SelectedItems
    strFile = Dir(varItem)
    strFolder = Left(varItem, Len(varItem) - Len(strFile))
    MsgBox "Folder: " & strFolder & vbCrLf & _
    "File: " & strFile
    fGetPath = varItem
    Next
    End If
    End With
    Set f = Nothing
    End Function

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

Similar Threads

  1. Replies: 15
    Last Post: 04-21-2011, 02:50 PM
  2. Pass variable name to another forms
    By Thomman in forum Access
    Replies: 3
    Last Post: 04-07-2011, 10:01 AM
  3. Pass Variable to Table Field with VBA
    By dgj32784 in forum Programming
    Replies: 3
    Last Post: 03-28-2011, 09:36 PM
  4. VBA Pass-through Query with Variable
    By smaumau in forum Programming
    Replies: 0
    Last Post: 12-06-2010, 09:10 AM
  5. Replies: 5
    Last Post: 04-05-2010, 11:43 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