Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Standard windows dialogue box for selecting a folder 2

Status
Not open for further replies.

Jonathan

Programmer
Mar 19, 1999
116
GB
I am using the Windows API in my application to allow the user to easily select files to open using the standard windows Open File box. I also need to provide some means of selecting a folder. An example of what I want can be seen by going to the start menu, clicking Find - Files or Folders and clicking the Browse... button next to the Look In combo.<br>
<br>
Does anyone know if there is a Win API function or other easy method for achieving this (preferably without getting new ActiveX controls)<br>
<br>
Cheers<br>
<br>
Jonathan <br>

 
You should not have to use API calls. Use the common dialog control supplied (hopefully) with Access. <br>
<br>
From the form design screen, click on the &quot;More Controls&quot; button and select Microsoft Common Dialog. Add the control to your form. This control is not visible at run time. To use it, write code that... (Following example from Books OnLine)<br>
<br>
<br>
Private Sub mnuFileOpen_Click ()<br>
' CancelError is True.<br>
On Error GoTo ErrHandler<br>
' Set filters.<br>
CommonDialog1.Filter = &quot;All Files (*.*)¦*.*¦Text _<br>
Files (*.txt)¦*.txt¦Batch Files (*.bat)¦*.bat&quot;<br>
' Specify default filter.<br>
CommonDialog1.FilterIndex = 2<br>
<br>
' Display the Open dialog box.<br>
CommonDialog1.ShowOpen <br>
' Call the open file procedure.<br>
OpenFile (CommonDialog1.FileName)<br>
Exit Sub<br>
<br>
ErrHandler:<br>
' User pressed Cancel button.<br>
Exit Sub<br>
End Sub<br>
<br>

 
As far as I know, the CommonDialog control doesn't have the dialogue box I need. I want one which will allow the user to select a folder, not a file (to see what I mean, select &quot;Find - Files or Folders&quot; from your start menu and click the button captioned &quot;Browse&quot;).<br>
<br>
As an aside, I don't use the CommonDialog control; I use a class module which operates in pretty much the same way, but removes the need for having a physical control on the form. Any time you want to use a dialogue box, you just create an instance of the class and then use it like the CD control. I got the code of the Microsoft web site, but if anyone is interested I can mail it to them.<br>
<br>
Cheers,<br>
<br>
Jonathan
 
There was a database I saw that allowed you to select folders and I am looking for it for another reason. It was written by a German programmer as I recall and allowed recursive sub directories to be indexed for pictures that could be shown on a form. It is about 2 years old and would have been written in Access 95 or 97. Cannot recall the name or the database but it probably got to me through an article in Smart Access.<br>
<br>
Anyone help either of us on this one? I need it desparately.<br>
<br>
Z
 
How about the DIR() to load an array and display in an drop-down or lsit box?
 
This was posted as a reply to the same question which I posted in the VB 5/6 forum, and is exactly the solution I was after<br>
<br>
Jonathan<br>
<br>
-------------------------------<br>
<br>
Hi,<br>
<br>
I have the following code snippet to which you are welcome. Provided with usual caveats 'use at peril' etc :&gt;.<br>
<br>
Put this in a module:<br>
<br>
Option Explicit<br>
<br>
Private Type BrowseInfo<br>
hWndOwner As Long<br>
pIDLRoot As Long<br>
pszDisplayName As Long<br>
lpszTitle As Long<br>
ulFlags As Long<br>
lpfnCallback As Long<br>
lParam As Long<br>
iImage As Long<br>
End Type<br>
<br>
'Browsing for directory.<br>
Private Const BIF_RETURNONLYFSDIRS = &H1 'For finding a folder to start document searching<br>
Private Const BIF_DONTGOBELOWDOMAIN = &H2 'For starting the Find Computer<br>
Private Const BIF_STATUSTEXT = &H4<br>
Private Const BIF_RETURNFSANCESTORS = &H8<br>
<br>
Private Const BIF_BROWSEFORCOMPUTER = &H1000 'Browsing for Computers.<br>
Private Const BIF_BROWSEFORPRINTER = &H2000 'Browsing for Printers<br>
Private Const BIF_BROWSEINCLUDEFILES = &H4000 'Browsing for Everything<br>
<br>
Private Const MAX_PATH = 260<br>
<br>
Private Declare Sub CoTaskMemFree Lib &quot;ole32.dll&quot; (ByVal hMem As Long)<br>
Private Declare Function lstrcat Lib &quot;kernel32&quot; Alias &quot;lstrcatA&quot; (ByVal _<br>
lpString1 As String, ByVal lpString2 As String) As Long<br>
<br>
Private Declare Function SHBrowseForFolder Lib &quot;shell32&quot; (lpbi As _<br>
BrowseInfo) As Long<br>
Private Declare Function SHGetPathFromIDList Lib &quot;shell32&quot; (ByVal pidList As _<br>
Long, ByVal lpBuffer As String) As Long<br>
<br>
Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String) As String<br>
<br>
'=================================================<br>
'Opens the system dialog for browsing for a folder<br>
'=================================================<br>
Dim iNull As Integer<br>
Dim lpIDList As Long<br>
Dim lResult As Long<br>
Dim sPath As String<br>
Dim udtBI As BrowseInfo<br>
<br>
With udtBI<br>
.hWndOwner = hWndOwner<br>
.lpszTitle = lstrcat(sPrompt, &quot;&quot;)<br>
<br>
.ulFlags = BIF_RETURNONLYFSDIRS<br>
End With<br>
<br>
lpIDList = SHBrowseForFolder(udtBI)<br>
If lpIDList Then<br>
sPath = String$(MAX_PATH, 0)<br>
lResult = SHGetPathFromIDList(lpIDList, sPath)<br>
Call CoTaskMemFree(lpIDList)<br>
iNull = InStr(sPath, vbNullChar)<br>
If iNull Then<br>
sPath = Left$(sPath, iNull - 1)<br>
End If<br>
End If<br>
<br>
BrowseForFolder = sPath<br>
<br>
End Function<br>
<br>
<br>
<br>
<br>
Put this in a form:<br>
<br>
Private Sub Form_Click()<br>
Dim MyStr As String<br>
MyStr = BrowseForFolder(hWnd, &quot;Hello&quot;)<br>
MsgBox MyStr<br>
End Sub<br>
<br>
<br>
<br>
Click on the form, and see if it's what you want.....<br>
<br>
HTH<br>
<br>
Andrew<br>
<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top