Results 1 to 10 of 10

Thread: Select Folder

  1. #1
    DAW
    Guest

    Select Folder

    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.

  2. #2
    Lounger
    Join Date
    Feb 2001
    Location
    TX, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Folder

    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

  3. #3
    DAW
    Guest

    Re: Select Folder

    WOW!

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

  4. #4
    ChrisR
    Guest

    Re: Select Folder

    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

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Select Folder

    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.
    Charlotte

  6. #6
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Louisville, Kentucky, USA
    Posts
    605
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Folder

    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,
    Roberta Price <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

  7. #7
    ChrisR
    Guest

    Re: Select Folder

    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

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Select Folder

    In that case, try the code posted earlier in the thread or go to <A target="_blank" HREF=http://www.mvps.org/access/>The Access Web</A>, where Dev Ashish has posted several methods for doing just that.
    Charlotte

  9. #9
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Select Folder

    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 <font color=red>Private Const BIF_BROWSEINCLUDEFILES = &H4000, </font color=red>

    and

    .ulFlags = BIF_RETURNONLYFSDIRS
    with <font color=red> .ulFlags = BIF_BROWSEINCLUDEFILES,</font color=red>

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

    Andrew C.

  10. #10
    5 Star Lounger
    Join Date
    Feb 2001
    Location
    Louisville, Kentucky, USA
    Posts
    605
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Select Folder

    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.
    Roberta Price <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •