ACC: How to Use the Windows OpenFile Dialog Box (1.x/2.0)

Article translations Article translations
Article ID: 96114 - View products that this article applies to.
This article was previously published under Q96114
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

Summary

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article describes how to use the OpenFile dialog box from Access Basic.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual in version 2.0.

More information

The OpenFile common dialog box can be implemented in Microsoft Access through the use of the Windows GetOpenFileName() application programming interface (API) function. Using this function will simplify programming issues for the developer. This function creates a system-defined dialog box, familiar throughout Windows, that makes it possible for the user to select a file to open. This function will return a valid file to the programmer that is fully qualified with the path name. The developer can customize how the system will handle specific situations, such as specifying that the file must exist when the user wants to save a file through the use of flags.

To use the code below, paste the declarations and function into a module.

NOTE: You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code.

   '-------------------------------------------------------
   ' Global Declaration Section
   '-------------------------------------------------------
   Option Compare Database
   Option Explicit

   Type tagOPENFILENAME
        lStructSize As Long
           hwndOwner As Integer
        hInstance As Integer
        lpstrFilter As Long
        lpstrCustomFilter As Long
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As Long
        nMaxFile As Long
        lpstrFileTitle As Long
        nMaxFileTitle As Long
        lpstrInitialDir As Long
        lpstrTitle As Long
        Flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As Long
        lCustData As Long
           lpfnHook As Long
        lpTemplateName As Long
   End Type

   Declare Function GetOpenFileName% Lib "COMMDLG.DLL" (_
                       OPENFILENAME As tagOPENFILENAME)
   Declare Function GetSaveFileName% Lib "COMMDLG.DLL" (_
                       OPENFILENAME As tagOPENFILENAME)
   Declare Function lstrcpy& Lib "Kernel" (ByVal lpDestString As Any,_
                       ByVal lpSourceString As Any)

   Dim OPENFILENAME As tagOPENFILENAME

   Global Const OFN_READONLY = &H1
   Global Const OFN_OVERWRITEPROMPT = &H2
   Global Const OFN_HIDEREADONLY = &H4
   Global Const OFN_NOCHANGEDIR = &H8
   Global Const OFN_SHOWHELP = &H10
   Global Const OFN_ENABLEHOOK = &H20
   Global Const OFN_ENABLETEMPLATE = &H40
   Global Const OFN_ENABLETEMPLATEHANDLE = &H80
   Global Const OFN_NOVALIDATE = &H100
   Global Const OFN_ALLOWMULTISELECT = &H200
   Global Const OFN_EXTENSIONDIFFERENT = &H400
   Global Const OFN_PATHMUSTEXIST = &H800
   Global Const OFN_FILEMUSTEXIST = &H1000
   Global Const OFN_CREATEPROMPT = &H2000
   Global Const OFN_SHAREAWARE = &H4000
   Global Const OFN_NOREADONLYRETURN = &H8000
   Global Const OFN_NOTESTFILECREATE = &H10000

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

   '-------------------------------------------------------
   ' Open Common Dialog Function
   '-------------------------------------------------------
   Function OpenCommDlg ()
     Dim Message$, Filter$, FileName$, FileTitle$, DefExt$
     Dim Title$, szCurDir$, APIResults%

       '*Define the filter string and allocate space in the "c" string
       Filter$ = "Access(*.mdb)" & Chr$(0) & "*.MDB;*.MDA" & Chr$(0)
       Filter$ = Filter$ & "Text(*.txt)" & Chr$(0) & "*.TXT" & Chr$(0)
       Filter$ = Filter$ & "Batch(*.bat)" & Chr$(0) & "*.BAT" & Chr$(0)
       Filter$ = Filter$ & Chr$(0)

       '* Allocate string space for the returned strings.
       FileName$ = Chr$(0) & Space$(255) & Chr$(0)
       FileTitle$ = Space$(255) & Chr$(0)

       '* Give the dialog a caption title.
       Title$ = "My File Open Dialog" & Chr$(0)

       '* If the user does not specify an extension, append TXT.
       DefExt$ = "TXT" & Chr$(0)

       '* Set up the default directory
       szCurDir$ = CurDir$ & Chr$(0)

       '* Set up the data structure before you call the GetOpenFileName

       OPENFILENAME.lStructSize = Len(OPENFILENAME)

       'If the OpenFile Dialog box is linked to a form use this line.
          'It will pass the forms window handle.

       OPENFILENAME.hwndOwner = Screen.ActiveForm.hWnd

       'If the OpenFile Dialog box is not linked to any form use this line.
       'It will pass a null pointer.

       OPENFILENAME.hwndOwner = 0&

       OPENFILENAME.lpstrFilter = lstrcpy(Filter$, Filter$)
       OPENFILENAME.nFilterIndex = 1
       OPENFILENAME.lpstrFile = lstrcpy(FileName$, FileName$)
       OPENFILENAME.nMaxFile = Len(FileName$)
       OPENFILENAME.lpstrFileTitle = lstrcpy(FileTitle$, FileTitle$)
       OPENFILENAME.nMaxFileTitle = Len(FileTitle$)
       OPENFILENAME.lpstrTitle = lstrcpy(Title$, Title$)
       OPENFILENAME.Flags = OFN_FILEMUSTEXIST Or OFN_READONLY
       OPENFILENAME.lpstrDefExt = lstrcpy(DefExt$, DefExt$)
       OPENFILENAME.hInstance = 0
       OPENFILENAME.lpstrCustomFilter = 0
       OPENFILENAME.nMaxCustFilter = 0
       OPENFILENAME.lpstrInitialDir = lstrcpy(szCurDir$, szCurDir$)
       OPENFILENAME.nFileOffset = 0
       OPENFILENAME.nFileExtension = 0
       OPENFILENAME.lCustData = 0
       OPENFILENAME.lpfnHook = 0
       OPENFILENAME.lpTemplateName = 0

       '* This will pass the desired data structure to the Windows API,
       '* which in turn uses it to display the Open Dialog form.

       APIResults% = GetOpenFileName(OPENFILENAME)

       If APIResults% <> 0 Then

           '* Note that FileName$ will have an embedded Chr$(0) at the
           '* end. You may wish to strip this character from the string.

         FileName$ = Left$(FileName$, InStr(FileName$, Chr$(0)) -1)

           Message$ = "The file you chose was " + FileName$
       Else
           Message$ = "No file was selected"
       End If

       MsgBox Message$

   End Function
				

References

"Microsoft Windows Software Development Kit Programmer's Reference, Volume 2: Functions," version 3.1

Properties

Article ID: 96114 - Last Review: October 26, 2013 - Revision: 3.0
Applies to
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
Keywords: 
kbnosurvey kbarchive kbhowto kbprogramming KB96114

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com