Home
DBPix
Download
Order
Testimonials
Support
Tutorials
Samples
KnowledgeBase
Links
Revision History
Documentation
Search
Contact
Site Map

Graph: DBPix image storage vs OLE Embedding and Linking in Microsoft Access


    
The Access Image FAQ < Previous ArticleNext Article >

How to display the Common 'File-Open' Dialog to Choose a File


    
Sample Newsgroup Questions 
Use the File Open Browse box
Browsing for a file within Access and returning the Path
Common Dialog Control in Access 2003?
HELP making file selection dialog boxes
File Browse code
CommonDialog in Access 2002
Need file browse on Access form
Open File Dialog
Browse Hard Drive in MS Access Form
FileDialog and Access Runtime
FileDialog in Access XP
Common Dialogs from Access?
File Dialog Box


A common requirement when building an image database is to give the user some way to select an image file. The 'Common File-Open' dialog provides a flexible and consistent way to do this, using the standard dialog which most Windows users are already familiar with. The dialog can also be used in 'Save' mode to choose the target for saving a file.

The 'Common Dialog OCX' controls provide a simple way to do this, however these controls can give rise to versioning and distribution problems (especially in runtime applications), and are generally not recommended. Instead, you can call the Win32 API directly to use the common dialogs. Although the API code is somewhat complex, fortunately Ken Getz has generously written a module that you can reuse in your applications, reducing the effort to a cut-and-paste, then writing just a few lines to show the dialog.


Usage

To use a 'File-Open' or a 'File-Save' dialog, download or open the following file containing Ken's code, then copy and paste the entire contents into a new module.   Download Code


'File-Open' Mode.

The following code shows how to display a File-Open dialog in respose to a button-click. See the screenshot below for the results.


Private Sub btnOpen_Click()

    Dim strFilter As String
    Dim strInputFileName As String

    strFilter = ahtAddFilterItem(strFilter, "JPEG Files (*.jpg, *.jpeg)", "*.jpg;*.jpeg")
    strFilter = ahtAddFilterItem(strFilter, "bmp Files (*.bmp)", "*.bmp")
    strFilter = ahtAddFilterItem(strFilter, "all Files (*.*)", "*.*")

    strInputFileName = ahtCommonFileOpenSave( _
                            Filter:=strFilter, _
                            OpenFile:=True, _
                            DialogTitle:="Choose an image file...", _
                            Flags:=ahtOFN_HIDEREADONLY)

    If Len(strInputFileName) > 0 Then
        ' Do something with the selected file
    Else
        'No file chosen, or user canceled
    End If

End Sub


In the code above, the three lines containing 'ahtAddFilterItem' build a filter string, which determines the options displayed in the 'Files of type' box and the file-filters that are applied when each option is selected.

You can modify the string 'Choose an image file...' to control what is displayed in the dialog caption.

The dialog can be further customized using flags. See the module code for the available flags, and the API Documentation for detailed flag descriptions. Combine multiple flags using 'Or'.


The 'File-Open' Dialog





'File-Save' Mode.

Using 'File-Save' mode is almost identical to 'File-Open' - just set the 'OpenFile' option to 'false'. The following code shows how to display a File-Save dialog in respose to a button-click.


Private Sub btnSave_Click()

    Dim strFilter As String
    Dim strInputFileName As String

    strFilter = ahtAddFilterItem(strFilter, "JPEG Files (*.jpg)", "*.jpg")

    strInputFileName = ahtCommonFileOpenSave( _
                            Filter:=strFilter, _
                            OpenFile:=False, _
                            DialogTitle:="Save Image As...", _
                            Flags:=ahtOFN_HIDEREADONLY)

    If Len(strInputFileName) > 0 Then
        ' Do something with the selected file
    Else
        'No file chosen, or user canceled
    End If

End Sub


In the code above, the line containing 'ahtAddFilterItem' builds a filter string, which determines the options displayed in the 'Save as type' box and the file-filters that are applied when each option is selected. The chosen extension is automatically added to the returned path if no extension is provided.

You can modify the string 'Save Image As...' to control what is displayed in the dialog caption.

The dialog can be further customized using flags. See the module code for the available flags, and the API Documentation for detailed flag descriptions. Combine multiple flags using 'Or'.


The Common 'File-Save' Dialog



Related Articles

How to display the Common 'Browse for Folder' Dialog to Choose a Folder




Imaging for Access that's Easy, Efficient & Fast
  • NO OLE Bloat
  • NO App Dependencies
  • NO Complex Coding
  • NO Performance Penalty
  •  DBPix logo
    Read More


    Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.