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!

Select a directory instead of file 1

Status
Not open for further replies.

EwS

Programmer
Dec 30, 2002
398
US
I use a common dialog control for selecting files:

Private Sub cmdFileSelect_Click()
cdlgFiles.ShowOpen
If cdlgFiles.CancelError = False Then
If cdlgFiles.FileName <> &quot;&quot; Then
txtMas90Path = cdlgFiles.FileName
End If
End If
End Sub

I want the user to be able to select a directory, not a file. How do I do this?

Thanks for the help.
 
this is the code you need to put behind the button:

Const BIF_RETURNONLYFSDIRS = &H1
Const BIF_EDITBOX = &H10
Const BIF_DONTGOBELOWDOMAIN = &H2

Private Sub Command1_Click()
Dim oShell As Object
Dim sPath As String
Set oShell = CreateObject(&quot;Shell.Application&quot;)
On Error Resume Next
sPath = oShell.BrowseForFolder(hWnd, &quot;Select a folder&quot;, BIF_RETURNONLYFSDIRS Or BIF_EDITBOX).Items.Item.Path
If sPath = &quot;&quot; Then sPath = &quot;C:&quot;
txtpath.text = sPath
Set oShell = Nothing
End Sub
 
Sorry, as far as I know, you can't do it with the common dialog control. You would have to use the Directory control that is part of the basic VB toolbox.

Robert
 
krikke - Thanks a LOT!

When I click on the button I want the default directory (which is &quot;My Computer&quot; right now) to be what is currently entered in the text box (txtpath). Can you, please, tell me how to do this?

If the directory entered doesn't exist, is it going to find the closest match or display an error? I guess if it displays an error, I should verify that the specified directory exits before I display the dialog. If it does exist, I pass it to the dialog, and if it doesn't exist, I let the dialog display &quot;My Computer&quot;.

 
I'm looking at MSDN but can't figure it out.
 
Check out thread222-498611 for an API solution. It allows you to specify a default directory initially selected when the dialog box is displayed.
If the directory you specify exists, it is selected.
If it doesn't, then My Computer is selected by default without causing an error.

You can use the Dir$ function before displaying the dialog box to find out whether a directory exists or not.
 
Hypetia,

I really appreciate your help! Thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top