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

How to create a file/folder dialog box in access 1

Status
Not open for further replies.

tar28un

Technical User
Nov 29, 2005
58
GB
Hi there,

can anybody help me with creating file/folder dialog box in access using VB code. IF you could help me with which control to use and how to code etc...
 
Step 1:
Add a reference to the microsoft common dialog box. If you are in design mode of a vba module, you should see references under the tools menu.

Step 2: add the common dialog box control to your form.

Step 3: add vba code to open dialog
Code:
  On Error GoTo ErrHandle
  dlg.FileName = DownLoadFile.Value
  dlg.ShowOpen
  DownLoadFile.Value = dlg.FileName
  
Exitsub:
  Exit Sub
  
ErrHandle:
  If Err.Number = 32755 Then Resume Exitsub
  Resume

If you do a google search on Microsoft Common Dialog Control, you will find links that will give you more properties and methods if my example was not sufficient.
 
I personally think using the common dialog control is easier than using the api, but that's personal preference. Many argue using the api has less overhead than using the control. On a modern PC, I don't think you would notice the difference.
 
How can I let the user to select only the folder via microsoft common dialog control because it normally allow the user to select a file
 
It isn't so much a question of overhead as deployment. You can be sure the API is available, but you risk dll hell looking for the right common dialog library. Besides, using the API builds character, or something like that.

Brian Begy
Chicago Data Solutions
 
I don't know if you can use the common dialog control to just select a folder. That may be another reason to use the API.
 
The common dialog control has a flag property that you may find helpful. Unfortunately, none of the flags specify folders only.
CONSTANTS FOR FLAGS PROPERTY

cdlOFNAllowMultiselect = &H200
Specifies that the File Namelist box allows multiple selections. The user can select more than one file at a run time by pressing the SHIFT key and using the UP ARROW and DOWN ARROW keys to select the desired files. When this is done, the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.

cdlOFNCreatePrompt = &H2000
Specifies that the dialog box prompts the user to create a file that doesn't currently exist. This flag automatically sets the cdlOFNPathMustExist and cdlOFNFileMustExist flags.

cdlOFNExplorer = &H80000
Use the Explorer-like Open A File dialog box template. Works with Windows 95 and Windows NT 4.0.

cdlOFNExtensionDifferent = &H400
Indicates that the extension of the returned filename is different from the extension specified by the DefaultExt property. This flag isn't set if the DefaultExt property is Null, if the extensions match, or if the file has no extension. This flag value can be checked upon closing the dialog box.

cdlOFNFileMustExist = &H1000
Specifies that the user can enter only names of existing files in the File Name text box. If this flag is set and the user enters an invalid filename, a warning is displayed. This flag automatically sets the cdlOFNPathMustExist flag.

cdlOFNHelpButton = &H10
Causes the dialog box to display the Help button.

cdlOFNHideReadOnly = &H4
Hides the Read Onlycheck box.

cdlOFNLongNames = &H200000
Use long filenames.

cdlOFNNoChangeDir = &H8
Forces the dialog box to set the current directory to what it was when the dialog box was opened.

cdlOFNNoDereferenceLinks = &H100000
Do not dereference shell links (also known as shortcuts). By default, choosing a shell link causes it to be dereferenced by the shell.

cdlOFNNoLongNames = &H40000
No long file names.

cdlOFNNoReadOnlyReturn = &H8000
Specifies that the returned file won't have the Read Only attribute set and won't be in a write-protected directory.

cdlOFNNoValidate = &H100
Specifies that the common dialog box allows invalid characters in the returned filename.

cdlOFNOverwritePrompt = &H2
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.

cdlOFNPathMustExist = &H800
Specifies that the user can enter only valid paths. If this flag is set and the user enters an invalid path, a warning message is displayed.

cdlOFNReadOnly = &H1
Causes the Read Only check box to be initially checked when the dialog box is created. This flag also indicates the state of the Read Only check box when the dialog box is closed.

cdlOFNShareAware = &H4000
Specifies that sharing violation errors will be ignored
 
I agree with BrianB about deployment. API can be more work on your part but can be less hassle down the road. There are more than one commondialog box controls with different dll files. Therein lies the rub when different versions are trying to "use" the wrong dll file. Having said that this is what we use the "set" some parameters for the commondialog.

commondialog1.CancelError = True
commondialog1.InitDir = "C:\My Documents"
commondialog1.Filter = "All DataBase Files (*.mdb)|*.mdb"
commondialog1.Flags = &H1000& Or &H800&
commondialog1.FilterIndex = 1
commondialog1.DialogTitle = " Select The File That You Want To Open. "
commondialog1.ShowOpen
sFileName = commondialog1.FileName

Docmd.Close acForm, "frmOpening"

If Right(sFileName, 4) <> ".mdb" Then
sFileName = sFileName & ".mdb"
End If
 
If ac2002 or above, simply use the Application.FileDialog object.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top