• Select Folder

    Author
    Topic
    #1768535

    Access 97 App
    ————-
    I want to prompt the user for a folder location. I don’t like using the CommonDialog.showOpen for this because it requires you to select a filename. I only want the user to select a folder.

    In VB6 I have used the DriveListBox and DirList box controls to achieve what I want. I have seen (in one of my many Access or VBA books I think) an Access 97 solution that uses a combo box (with relevant drives in it) and a list box (with relevant folder names in it) to achieve the same thing. They are populated using API calls I think. The combo box of drives certainly was – the list box of folders might have been constructed using the dir() function.

    I can’t remember where I saw this solution though. Can anyone offer any help?

    Thanks.

    Dave.

    Viewing 1 reply thread
    Author
    Replies
    • #1779839

      paste the followinig into a new module. It does exactly what you are looking for

      Option Compare Database
      Option Explicit

      ‘************** Code Start **************
      ‘This code was originally written by Terry Kreft.
      ‘It is not to be altered or distributed,
      ‘except as part of an application.
      ‘You are free to use it in any application,
      ‘provided the copyright notice is left unchanged.

      ‘Code courtesy of
      ‘Terry Kreft

      Private Type BROWSEINFO
      hOwner As Long
      pidlRoot As Long
      pszDisplayName As String
      lpszTitle As String
      ulFlags As Long
      lpfn As Long
      lParam As Long
      iImage As Long
      End Type

      Private Declare Function SHGetPathFromIDList Lib “shell32.dll” Alias _
      “SHGetPathFromIDListA” (ByVal pidl As Long, _
      ByVal pszPath As String) As Long

      Private Declare Function SHBrowseForFolder Lib “shell32.dll” Alias _
      “SHBrowseForFolderA” (lpBrowseInfo As BROWSEINFO) _
      As Long

      Private Const BIF_RETURNONLYFSDIRS = &H1
      Public Function BrowseFolder(szDialogTitle As String) As String
      Dim X As Long, bi As BROWSEINFO, dwIList As Long
      Dim szPath As String, wPos As Integer

      With bi
      .hOwner = hWndAccessApp
      .lpszTitle = szDialogTitle
      .ulFlags = BIF_RETURNONLYFSDIRS
      End With

      dwIList = SHBrowseForFolder(bi)
      szPath = Space$(512)
      X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)

      If X Then
      wPos = InStr(szPath, Chr(0))
      BrowseFolder = Left$(szPath, wPos – 1)
      Else
      BrowseFolder = “”
      End If
      End Function

      • #1779840

        WOW!

        Much better than the one I had seen before!
        Thanks a whole big bunch Jim.
        Dave.

        • #1786705

          Hi,

          I know this thread is a bit old now, but is there any way to get this API (or another different API) to actually open at a specific folder within the tree view rather than having to navigate from the top level?

          I know that one of the parameters can select some different system options (Printers, Control Panels etc), but I’d like the dialog box to open at a folder of my choosing.

          TIA,
          Chris

          • #1786710

            Private Declare Function GetFileOpenName32 Lib “comdlg32.dll” Alias “GetOpenFileNameA” (pOpenfilename As OPENFILENAME32) As Long

            The argument to this one is actually an array and one of the elements is the initial directory.

            • #1786725

              Charlotte,

              Thanks for the reply. I’ve used GetOpenFileName before, but I’m actually wanting to return a folder / directory rather than a file and don’t really want to confuse the user with a list of files.

              GetOpenFileName only seems to want to deal in files, not at the folder level (or have I grabbed the wrong end of the stick here??)

              Regards,
              Chris

            • #1786730

              In that case, try the code posted earlier in the thread or go to The Access Web, where Dev Ashish has posted several methods for doing just that.

    • #1786713

      Well….I have spent most of today trying to find a way to do the exact same thing but I want the file also. If you could point me in the right direction, I would be so greatful.

      Also, once Ithe user has selected the file, I want the path stored in a bound text box. Can this be done?

      Thanks,

      • #1786742

        The code posted above (21768) can be adapted to browse for files as well, but I think will only work with Win 98, Win Me or Win 2000. (I have not tried, but think it will not work with Windows 95).
        If you replace

        Private Const BIF_RETURNONLYFSDIRS = &H1
        with Private Const BIF_BROWSEINCLUDEFILES = &H4000,

        and

        .ulFlags = BIF_RETURNONLYFSDIRS
        with .ulFlags = BIF_BROWSEINCLUDEFILES,

        and if your OS version is ok, the function will return the full pathname of the selected file.

        Andrew C.

        • #1786749

          Thanks for your help everyone. DAW sent me exactly what I was looking for. Such a small amount of code…. so much time saved.

          In case anyone else is looking for this, the code is pasted below:

          Dim myComDlg As ComDlg
          Set myComDlg = New ComDlg

          myComDlg.DialogTitle = “Locate CAD Files”
          myComDlg.InitDir = “c:bin”
          myComDlg.MaxFileSize = 256
          myComDlg.ShowOpen

          ‘You can put the filename in a bound textbox if you like, but I am just showing it
          ‘in a message box.
          Me!txtFilename = myComDlg.filename
          MsgBox “You chose ” & myComDlg.filename

          Set myComDlg = Nothing

          Again, thanks everyone, especially DAW.

    Viewing 1 reply thread
    Reply To: Select Folder

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: