FileDialog box and preventing browsing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rdemarco
    New Member
    • Feb 2008
    • 5

    FileDialog box and preventing browsing

    Hi

    I specify the initial location of my Application.Fil eDialog(msoFile DialogFilePicke r) box by setting the initialFileName property to the location I want the user to pick a file from.

    So far so good, but is it possible to prevent users from being able to browse to another dir?
    I would like to be able to restrict them to the location I specify!

    Any thoughts?

    Thanks
    Rob
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Havent looked specifically so here is a general description of what I would try.

    The file dialog control should have an event that fires when the directory is chosen.

    In the code for that event you would check to see if the user has left the allowed directory.
    If they have then you would reset the path back to the one that you want them to be stuck in and refresh the controls display.

    Something like that anyway!

    Comment

    • Scott Price
      Recognized Expert Top Contributor
      • Jul 2007
      • 1384

      #3
      This is a quick and dirty code sample that checks for a specified path in the selected item, if the user selected a file from another location it pops up a message box.

      [CODE=vb] Dim dlgOpen As FileDialog
      Dim varSelectedItem As Variant
      Set dlgOpen = Application.Fil eDialog(msoFile DialogFilePicke r)

      With dlgOpen
      If .Show = True Then
      For Each varSelectedItem In .SelectedItems
      If Left(varSelecte dItem, 15) <> "E:\Access Code\" Then
      MsgBox ("Please select a valid path")
      End If
      Next varSelectedItem
      End If

      End With[/CODE]

      Unfortunately I don't know of any way to limit the browse ability of the dialog box in Access. Your users will be able to browse to whichever location they wish, but this will prevent them from opening the file if it isn't in the directory you specify.

      You'll need to add the execute command in order to make Access open a valid file.

      Regards,
      Scott

      Comment

      • rdemarco
        New Member
        • Feb 2008
        • 5

        #4
        Thanks very much, Scott and Delerna

        It works well, and for my purpose it will be acceptable to remind users not to stray away from the directory they are supposed to use.
        Unfortunately, the FileDialog closes when they OK the msgbox - I'll try to tweak your code to keep it open and as Delerna had suggested, navigate them back to the propper dir.

        Thanks again

        Rob







        Originally posted by Scott Price
        This is a quick and dirty code sample that checks for a specified path in the selected item, if the user selected a file from another location it pops up a message box.

        [CODE=vb] Dim dlgOpen As FileDialog
        Dim varSelectedItem As Variant
        Set dlgOpen = Application.Fil eDialog(msoFile DialogFilePicke r)

        With dlgOpen
        If .Show = True Then
        For Each varSelectedItem In .SelectedItems
        If Left(varSelecte dItem, 15) <> "E:\Access Code\" Then
        MsgBox ("Please select a valid path")
        End If
        Next varSelectedItem
        End If

        End With[/CODE]

        Unfortunately I don't know of any way to limit the browse ability of the dialog box in Access. Your users will be able to browse to whichever location they wish, but this will prevent them from opening the file if it isn't in the directory you specify.

        You'll need to add the execute command in order to make Access open a valid file.

        Regards,
        Scott

        Comment

        • Scott Price
          Recognized Expert Top Contributor
          • Jul 2007
          • 1384

          #5
          It's not really the accepted practice since MS is moving away from the GoTo command, but you could set a label on the line where the With dlgOpen statement starts, and then set a GoTo [Label]: statement after the message box. This forces it back through the loop and reopens the dialog box after an incorrect choice.

          I just set it up in my test database using 9: as the label for line number 9 (where the With statement starts), then put GoTo 9: in the next line after the message box.

          Regards,
          Scott

          Comment

          Working...