In order to display Browse for Folder dialog box using VB6, what object should i use? What is the method to display this dialog box when I click BROWSE... button?
Please help me someone......
Printable View
In order to display Browse for Folder dialog box using VB6, what object should i use? What is the method to display this dialog box when I click BROWSE... button?
Please help me someone......
If you double-click on the picture of the folder, in your toolbox, you will get a DIR listbox
Try this:
VB Code:
Option Explicit Private Sub Dir1_Click() MsgBox Dir1.Path End Sub
There are two methods basically..
API or the OCX, the Common Dialog OCX is in the control list you get when right clicking on your Toolbox and browsing for more. (Microsoft Common Dialog etc.etc)
The API version can be found multiple times here on VBForums if you use search :)
http://www.vbforums.com/search.php?
Use the CommonDialog control.
If you don't see it in your toolbox, Click on Project, Components and set the Microsoft Common Dialog control.
You open it with something like CommonDialog1.ShowOpen probably in a command button click event.
There are quite a lot of things you can set before you open it. It's pretty well documented in VB6 help.
Give it a try and post your code here if you have any problems.
One thing about the Common Dialog which is not intuitive: If the user cancels without selecting a file, it can generate an error - so you do need an error trap.
.CancelError = false :)
Here's an example. A bit rough, but it gives you the general idea.
VB Code:
Private Sub Command1_Click() Dim DefaultSharedDBLocation As String Dim ErrorMessage As String Dim ErrCount As Integer DefaultSharedDBLocation = App.Path If Right(DefaultSharedDBLocation, 1) <> "\" Then DefaultSharedDBLocation = DefaultSharedDBLocation & "\" End If DefaultSharedDBLocation = DefaultSharedDBLocation & "Workbook.xls" CommonDialog1.FileName = "*.xls" 'SharedDBLocation CommonDialog1.DialogTitle = "My Dialog Title'" ' Set CancelError is True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' Set flags CommonDialog1.Flags = cdlOFNHideReadOnly ' Set filters CommonDialog1.Filter = "All Files (*.*)|*.*|Excel Files (*.xls)|*.xls" ' Specify default filter CommonDialog1.FilterIndex = 2 ' Display the Open dialog box CommonDialog1.ShowOpen MsgBox "You selected " & CommonDialog1.FileName Exit Sub ErrHandler: 'Invalid file name If Err = 20477 And ErrCount = 0 Then 'Use the default file name instead CommonDialog1.FileName = DefaultSharedDBLocation ErrCount = ErrCount + 1 'In case Default name is bad Resume 'User pressed quit ElseIf Err = 32755 Then Exit Sub 'Other error Else ErrorMessage = Err & " " & Error$ If MsgBox(ErrorMessage, vbRetryCancel) = vbRetry Then Resume Else Exit Sub End If End If End Sub
Quote:
Originally Posted by Devion
Brian.BrainNotWorkingToday = True ;)
The common dialog is a "Browse For File" control, not a "Browse For Folder" control. To folder browse, use this:VB Code:
Public Function BrowseForFolder(hWndOwner As Long, sPrompt As String) As String Dim iNull As Integer Dim lpIDList As Long Dim lResult As Long Dim sPath As String Dim udtBI As BrowseInfo With udtBI .hWndOwner = hWndOwner .lpszTitle = lstrcat(sPrompt, "") .ulFlags = BIF_RETURNONLYFSDIRS .lpfnCallback = PassBackAddress(AddressOf BrowseCallBackProc) End With lpIDList = SHBrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH, 0) lResult = SHGetPathFromIDList(lpIDList, sPath) Call CoTaskMemFree(lpIDList) iNull = InStr(sPath, vbNullChar) If iNull Then sPath = Left$(sPath, iNull - 1) End If End If BrowseForFolder = sPath End Function Private Sub cmdBrowse_Click() Dim sDocFolderPath As String sDocFolderPath = BrowseForFolder(Form1.hWnd, "Select A Folder") If sDocFolderPath <> "" Then 'store selected folder in textbox Text1.Text = sDocFolderPath End If End Sub
Hi, you can also get the Broswe for folder using the Shell Object here a example if helps.
VB Code:
Private Sub CmdBrowse_Click() Dim oShell As Object, oFolder As Object Set oShell = CreateObject("Shell.Application") Set oFolder = oShell.BrowseForFolder(0, "Select a folder", 0) If Not oFolder Is Nothing Then MsgBox oFolder.Items.Item.Path End If Set oFolder = Nothing Set oShell = Nothing End Sub