Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2004
    Manchester, England

    Talking Unanswered: File Browser Dialog Box


    I need to come up with some code to open a file browser dialog box that can be used to select another mdb file. I then want to put the file path into a string variable. Any ideas greatly appreciated!


  2. #2
    Join Date
    Aug 2002
    Northampton, England
    Have a look here.

    Look for API's in the index, then Call Windows browse for Folder Dialog box.


  3. #3
    Join Date
    Aug 2004
    Manchester, England

    Looks helpful. Many Thanks.


  4. #4
    Join Date
    Nov 2003
    File Dialogs are posted all over this forum. A simple search on it will bring up all kinds.

    Never the less....if you're referencing the Microsoft Office Object Library, you can also use something like this:

    ‘ Need ‘Microsoft Office 10.0 Object Library’ set in References  
    ‘ for this to work. Place the code into the OnClick event of a 
    ‘ Command Button or place it into a Function procedure. 
    On Error GoTo dlg_Error 
    Dim dlg As Object 
    Dim vrtSelectedItem As Variant 
    ' 1 is the File Open Dialog	 - msoFileDialogOpen 
    ' 2 is the File SaveAs Dialog	 - msoFileDialogSaveAs 
    ' 3 is the File Browser (Picker) - msoFileDialogFilePicker 
    ' 4 is the Folder Browser (Picker)- msoFileDialogFolderPicker 
    	Set dlg = Application.FileDialog(msoFileDialogFilePicker) 
    	With dlg 
    	 ' Set the File Dialog Window Title 
    	 .Title = "Select Student Photo..." 
    	 ' Clear any File Dialog Filters 
    	 ' Add a New File Dialog Filters 
    	 .Filters.Add "Images", "*.bmp; *.gif; *.jpg; *.jpeg; *.wmf", 1 
    	 .Filters.Add "All files", "*.*"
    	 ' Set the Name for the action button in our File Dialog 
    	 .ButtonName = "Take Student"
    	 ' Turn OFF File multi Select. We don't need this now. 
    	 .AllowMultiSelect = False
    	 ' Set the File Dialog window to show Thumbnamils 
    	 .InitialView = msoFileDialogViewThumbnail
    	 ' Set folder in which the File Dialog will start from. 
    	 .InitialFileName = CurDir
    	 ' If the Action button is selected and a file has indeed been
    	 ' selected then place the path & file name into the PhotoPath 
    	 ' field on Form and set the Image Control (StudentPicture) to 
    	 ' display it. 
    	 If .show = -1 And Not IsNull(.SelectedItems) Then 
    		 For Each vrtSelectedItem In .SelectedItems 
    			Me.PhotoPath = vrtSelectedItem 
    			Me.StudentPicture.Picture = Me.PhotoPath 
    	 End If 
    	End With 
    	Set dlg = Nothing ' Free memory 
    	Exit Sub
    	MsgBox Error 
    	Resume Exit_dlg_Error
    Another way is to use the dialog in Windows this:

    Sub OpenExplorer 
       Dim x As Variant 
       Const msgTitle As String = "Open Explorer" 
       Const cExplorerPath As String = "C:\WINDOWS\EXPLORER.EXE" 
       Const cExplorerSwitches As String = " /n,/e" 
       Const cFilePath As String = "YourFilePath" 
       On Error GoTo Error_Handler 
       If Dir(cExplorerPath, vbDirectory) = "" Then 
    	  MsgBox "Explorer Path '" & cExplorerPath & "' could not be found.", vbCritical, msgTitle
    x = Shell(cExplorerPath, vbNormalFocus) 
       ElseIf Dir(cFilePath, vbDirectory) = "" Then 
    	  MsgBox "Path '" & cFilePath & "' could not be found.", vbCritical, msgTitle 
    	  x = Shell(cExplorerPath, vbNormalFocus) 
    	  x = Shell(cExplorerPath & cExplorerSwitches & "," & cFilePath, vbNormalFocus) 
       End If 
    Exit Sub
       MsgBox "An error has occured trying to open Explorer", vbCritical, msgTitle 
    End Sub

    And of course you can use good old faithfull API's to pull up a File Dialog browse box...something like this....just place the code into a database code module then call the function, BrowseForFile():

    Const MB_DEFBUTTON1 = &H0& 
    Const MB_DEFBUTTON2 = &H100& 
    Const MB_DEFBUTTON3 = &H200& 
    Private Const VER_PLATFORM_WIN32_NT = 2 
    Private Type OSVERSIONINFO 
    	dwOSVersionInfoSize As Long 
    	dwMajorVersion As Long 
    	dwMinorVersion As Long 
    	dwBuildNumber As Long 
    	dwPlatformId As Long 
    	szCSDVersion As String * 128 
    End Type
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
    Private Declare Function GetFileNameFromBrowseW Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As Long, ByVal nMaxFile As Long, ByVal lpstrInitialDir As Long, ByVal lpstrDefExt As Long, ByVal lpstrFilter As Long, ByVal lpstrTitle As Long) As Long
    Private Declare Function GetFileNameFromBrowseA Lib "shell32" Alias "#63" (ByVal hwndOwner As Long, ByVal lpstrFile As String, ByVal nMaxFile As Long, ByVal lpstrInitialDir As String, ByVal lpstrDefExt As String, ByVal lpstrFilter As String, ByVal lpstrTitle As String) As Long
    Public Function BrowseForFile(Handle As Long, Optional StartLocation As String, Optional DefaultExten As String, _ 
    					  Optional TheFilter As String, Optional TheTitle As String) As String 
    	Dim sSave As String 
    	sSave = Space(255)
    	If StartLocation = "" Then StartLocation = "C:\" 
    	If TheTitle = "" Then TheTitle = "Browse For File..." 
    	If DefaultExten = "" Then DefaultExten = "txt" 
    	If TheFilter = "" Then TheFilter = "Text files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    	'If we're on WinNT, call the unicode version of the function 
    	If IsWinNT Then 
    		GetFileNameFromBrowseW Handle, StrPtr(sSave), 255, StrPtr(StartLocation), StrPtr(DefaultExten), StrPtr(TheFilter), StrPtr(TheTitle) 
    	'If we're not on WinNT, call the ANSI version of the function 
    		GetFileNameFromBrowseA Handle, sSave, 255, StartLocation, DefaultExten, TheFilter, TheTitle 
    	End If 
    	' Return the result..... 
    	' ====================== 
    	' Remove eccess spaces from returned string. 
    	sSave = Trim(sSave) 
    	' Remove any Null characters from the end of string (if any). 
    	If Right$(sSave, 1) = Chr$(0) Then 
    	 sSave = Left$(sSave, Len(sSave) - 1) 
    	 GoTo Recheck 
    	End If
    	' Return the path and file name. 
    	BrowseForFile = sSave 
    End Function 
    Private Function IsWinNT() As Boolean 
    	myOS.dwOSVersionInfoSize = Len(myOS) 
    	GetVersionEx myOS 
    	IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT) 
    End Function
    Last edited by CyberLynx; 12-24-05 at 02:45.
    Self Taught In ALL Environments.....And It Shows!

  5. #5
    Join Date
    Aug 2004
    Manchester, England
    CyberLynx, Thanks for these examples.

    I'm trying to use your third example (APIs), and get the following error-

    "byref argument type mismatch"

    ...because I'm not passing the long variable 'Handle' to the function. A little more reasearch leads me to believe that handle should refer to the window where the dialog box opens, yes? Can you let me know what that value should be, or provide a function which gets it?

    Thanks again,


  6. #6
    Join Date
    Nov 2003
    Sorry for the late reply (been romping around Mexico for a while)....

    I assume you are talking about the BrowseForFile function where the Current window handle is required in the first parameter of the function. Simply put in the handle of the calling window (Form). For example:

    Dim ReturnedString as String
    ReturnedString = BrowseForFile(Me.Hwnd, , , ,"My Text Files")
    MsgBox ReturnedString
    Last edited by CyberLynx; 01-26-06 at 13:51.
    Self Taught In ALL Environments.....And It Shows!

  7. #7
    Join Date
    Aug 2004
    Manchester, England

    That's it- works perfectly. Hope Mexico was good!


Posting Permissions

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