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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use form to browse drives 11

Status
Not open for further replies.

BRStevens

Programmer
Jan 15, 2001
2
US
I'm looking for a way to browse to and capture a file path and name.
The purpose for this is so that if a linked table or file has been moved, the user is prompted to locate it and I'll then have the code re-link it.
 
You can use the GetOpenFileName API:

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Form_Load()
'KPD-Team 1998
'URL: 'E-Mail: KPDTeam@Allapi.net
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
'Set the parent window
OFName.hwndOwner = Me.hWnd
'Set the application's instance
OFName.hInstance = App.hInstance
'Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Open File - KPD-Team 1998"
'No flags
OFName.flags = 0

'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox "File to Open: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub
 
Thanks, this worked great. Just a couple things: I needed to declare App as OPENFILENAME to get it to work and I was wondering if there was any way to hide the 'open as read only' check box?
 
probably some bit in the flags member of the openfilename structure, I will let you know asap.
 
yes its a flag,
and some more flags:

'Openfiledialog constants
Const FILEMUSTEXIST = &H1000
Const PATHMUSTEXIST = &H800
Const HIDEREADONLY = &H4
Const SHAREAWARE = &H4000

> I needed to declare App as OPENFILENAME

you can also comment the line:
'OFName.hInstance = App.hInstance

the example was designed for vb. app is a vb class, i don't know if there is a way to get a handle to the msaccess instance. Someone else might jump into this thread to tell you if you're lucky.
 
Just in case you want to have it all :)

Const OFN_READONLY = &H1
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_SHOWHELP = &H10
Const OFN_ENABLEHOOK = &H20
Const OFN_ENABLETEMPLATE = &H40
Const OFN_ENABLETEMPLATEHANDLE = &H80
Const OFN_NOVALIDATE = &H100
Const OFN_ALLOWMULTISELECT = &H200
Const OFN_EXTENSIONDIFFERENT = &H400
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_CREATEPROMPT = &H2000
Const OFN_SHAREAWARE = &H4000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOTESTFILECREATE = &H10000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOLONGNAMES = &H40000 ' force no long names for 4.x modules
Const OFN_EXPLORER = &H80000 ' new look commdlg
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_LONGNAMES = &H200000 ' force long names for 3.x modules

Const OFN_SHAREFALLTHROUGH = 2
Const OFN_SHARENOWARN = 1
Const OFN_SHAREWARN = 0

Flag Meaning

OFN_ALLOWMULTISELECT

Specifies that the File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface.
If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the file names of the selected files. The nFileOffset member is the offset, in bytes or characters, to the first file name, and the nFileExtension member is not used. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. This format enables the Explorer-style dialog boxes to return long file names that include spaces. For old-style dialog boxes, the directory and file name strings are separated by spaces and the function uses short file names for file names with spaces. You can use the FindFirstFile function to convert between long and short file names.

If you specify a custom template for an old-style dialog box, the definition of the File Name list box must contain the LBS_EXTENDEDSEL value.

OFN_CREATEPROMPT

If the user specifies a file that does not exist, this flag causes the dialog box to prompt the user for permission to create the file. If the user chooses to create the file, the dialog box closes and the function returns the specified name; otherwise, the dialog box remains open. If you use this flag with the OFN_ALLOWMULTISELECT flag, the dialog box allows the user to specify only one nonexistent file.

OFN_DONTADDTORECENT

Windows 2000: Prevents the system from adding a link to the selected file in the file system directory that contains the user's most recently used documents. To retrieve the location of this directory, call the SHGetSpecialFolderLocation function with the CSIDL_RECENT flag.

OFN_ENABLEHOOK

Enables the hook function specified in the lpfnHook member.

OFN_ENABLEINCLUDENOTIFY

Windows 2000: Causes the dialog box to send CDN_INCLUDEITEM notification messages to your OFNHookProc hook procedure when the user opens a folder. The dialog box sends a notification for each item in the newly opened folder. These messages enable you to control which items the dialog box displays in the folder's item list.

OFN_ENABLESIZING

Windows 2000, Windows 98: Enables the Explorer-style dialog box to be resized using either the mouse or the keyboard. By default, the Explorer-style Open and Save As dialog boxes allow the dialog box to be resized regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template. The old-style dialog box does not permit resizing.

OFN_ENABLETEMPLATE

Indicates that the lpTemplateName member is a pointer to the name of a dialog template resource in the module identified by the hInstance member.
If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.

OFN_ENABLETEMPLATEHANDLE

Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName if this flag is specified.
If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.

OFN_EXPLORER

Indicates that any customizations made to the Open or Save As dialog box use the new Explorer-style customization methods. For more information, see Explorer-Style Hook Procedures and Explorer-Style Custom Templates.
By default, the Open and Save As dialog boxes use the Explorer-style user interface regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag.

If you want the old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style template or hook procedure. If you want the old style but do not need a custom template or hook procedure, simply provide a hook procedure that always returns FALSE.

OFN_EXTENSIONDIFFERENT

Specifies that the user typed a file name extension that differs from the extension specified by lpstrDefExt. The function does not use this flag if lpstrDefExt is NULL.

OFN_FILEMUSTEXIST

Specifies that the user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used.

OFN_FORCESHOWHIDDEN

Windows 2000: Forces the showing of system and hidden files, thus overriding the user setting to show or not show hidden files. However, a file that is marked both system and hidden is not shown.

OFN_HIDEREADONLY

Hides the Read Only check box.

OFN_LONGNAMES

For old-style dialog boxes, this flag causes the dialog box to use long file names. If this flag is not specified, or if the OFN_ALLOWMULTISELECT flag is also set, old-style dialog boxes use short file names (8.3 format) for file names with spaces.
Explorer-style dialog boxes ignore this flag and always display long file names.

OFN_NOCHANGEDIR

Restores the current directory to its original value if the user changed the directory while searching for files.

OFN_NODEREFERENCELINKS

Directs the dialog box to return the path and file name of the selected shortcut (.LNK) file. If this value is not specified, the dialog box returns the path and file name of the file referenced by the shortcut.

OFN_NOLONGNAMES

For old-style dialog boxes, this flag causes the dialog box to use short file names (8.3 format).
Explorer-style dialog boxes ignore this flag and always display long file names.

OFN_NONETWORKBUTTON

Hides and disables the Network button.

OFN_NOREADONLYRETURN

Specifies that the returned file does not have the Read Only check box selected and is not in a write-protected directory.

OFN_NOTESTFILECREATE

Specifies that the file is not created before the dialog box is closed. This flag should be specified if the application saves the file on a create-nonmodify network share. When an application specifies this flag, the library does not check for write protection, a full disk, an open drive door, or network protection. Applications using this flag must perform file operations carefully, because a file cannot be reopened once it is closed.

OFN_NOVALIDATE

Specifies that the common dialog boxes allow invalid characters in the returned file name. Typically, the calling application uses a hook procedure that checks the file name by using the FILEOKSTRING message. If the text box in the edit control is empty or contains nothing but spaces, the lists of files and directories are updated. If the text box in the edit control contains anything else, nFileOffset and nFileExtension are set to values generated by parsing the text. No default extension is added to the text, nor is text copied to the buffer specified by lpstrFileTitle.
If the value specified by nFileOffset is less than zero, the file name is invalid. Otherwise, the file name is valid, and nFileExtension and nFileOffset can be used as if the OFN_NOVALIDATE flag had not been specified.

OFN_OVERWRITEPROMPT

Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.

OFN_PATHMUSTEXIST

Specifies that the user can type only valid paths and file names. If this flag is used and the user types an invalid path and file name in the File Name entry field, the dialog box function displays a warning in a message box.

OFN_READONLY

Causes the Read Only check box to be selected initially when the dialog box is created. This flag indicates the state of the Read Only check box when the dialog box is closed.

OFN_SHAREAWARE

Specifies that if a call to the OpenFile function fails because of a network sharing violation, the error is ignored and the dialog box returns the selected file name.
If this flag is not set, the dialog box notifies your hook procedure when a network sharing violation occurs for the file name specified by the user. If you set the OFN_EXPLORER flag, the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you do not set OFN_EXPLORER, the dialog box sends the SHAREVISTRING registered message to the hook procedure.

OFN_SHOWHELP

Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button.
An Explorer-style dialog box sends a CDN_HELP notification message to your hook procedure when the user clicks the Help button.


 
Sorry Can somebody please tell me how I can get this to work when you click a button
 
I'm using this method to save the filename and path in a text box. Is there a way to designate the path using the server name instead of the drive letter? We hook up to file servers and the drive letter can be diffferent for different computers.

Thank you.
 
Can someone help me figure out how to call this API from my form? I can't figure out what I need to include after the GetOpenfilename in the ().
 
camidon,
the argument of the GetOpenFilename API is a user defined type, see "Private Type OPENFILENAME" and "Dim OFName As OPENFILENAME" in the example above.

lesy,
you can use \\server\share\dir\... instead of a drive letter. You'll need to change the value of OFName.lpstrInitialDir.
 
So how would I call it from the form since it's a user defined type? Sorry I'm not really familiar with the API.
 
copy the code from my Jan,16 post in a form module and remove the line: "OFName.hInstance = App.hInstance".
when the form is loaded it shows the dialog.
 
Hennep,

I have used this type of code before, but I gave you a star just for going through the effort of describing all it so everyone can understand...

Thanks... Terry M. Hoey

Please read the following FAQ to learn how to help me help you...

faq183-874
 
I found this info very useful...but how do you change the open dialog to a save dialog?? I thought accouding to the definitions in win32api that you would just change GetOpenFileName to GetSaveFileName but access dosen't seem to recognize the latter as a function. Can anyone lend me a hand? Kris McCuller
Programmer Portiva Corp.
kmcculler@portiva.com
 
Did you declare the Win32API?

Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Terry M. Hoey
 
HI there

This is great I have a couple wuestions if anyone still looks at this posting.

Q1. how do you select a Directory not a file?
Q2. I also have and access 2 database which i need a browse function for any ideas.

Cheers
 
'ENJOY THIS === GET THAT FOLDER NAME =====

'Author:==SHARED=====And Share this to the world==================

'===========DECLARATIONS FOR BROWSE FOLDER (API)==================
Public 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

'===BROWSEINFO.ulFlags values:==============
Public Const BIF_RETURNONLYFSDIRS = &H1
Public Const BIF_DONTGOBELOWDOMAIN = &H2
Public Const BIF_STATUSTEXT = &H4
Public Const BIF_RETURNFSANCESTORS = &H8
Public Const BIF_BROWSEFORCOMPUTER = &H1000
Private Const BIF_BROWSEFORPRINTER = &H2000
'-------------------------------------------

Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
'=========END DECLARATIONS FOR BROWSE FOLDER (API)==================

'-------------------------------------------------------------------
'This function returns the Folder/Path Selected from the "Browse Folder"
'-------------------------------------------------------------------
Function udfGetPath(xOwner As String) As String
Dim bInf As BROWSEINFO
Dim PathID As Long
Dim RetPath As String

'Set the properties of the folder dialog
'bInf.hOwner = Me.hWnd
bInf.hOwner = Forms(xOwner).hwnd

'Pointer to the item identifier list specifying the
'location of the "root" folder to browse from.
'If NULL, the desktop folder is used.
bInf.pidlRoot = 0&

bInf.lpszTitle = "Please select a folder... "
bInf.ulFlags = BIF_RETURNONLYFSDIRS

'Show the Browse For Folder dialog
PathID = SHBrowseForFolder(bInf)

RetPath = Space$(512)

If SHGetPathFromIDList(ByVal PathID, ByVal RetPath) Then
'Trim off the null chars ending the path
udfGetPath = udfTrimNull(RetPath)
Else
udfGetPath = ""
End If

End Function

 
HI thanks for that but what do i do with it

Will it work in access 2

Cheers for your help so far
 
Where do i put the different code segments??!??????

When i just paste it in the space that commes up when i browse from the event line of the button properties i get errors saying that declarations cant be where they are and so on..

??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top