Microsoft KB Archive/96114

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

Article ID: 96114

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q96114



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 &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x, or the &quot;Building Applications&quot; 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 &quot;COMMDLG.DLL&quot; (_                      OPENFILENAME As tagOPENFILENAME) Declare Function GetSaveFileName% Lib &quot;COMMDLG.DLL&quot; (_                      OPENFILENAME As tagOPENFILENAME) Declare Function lstrcpy& Lib &quot;Kernel&quot; (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 &quot;c&quot; string Filter$ = &quot;Access(*.mdb)&quot; & Chr$(0) & &quot;*.MDB;*.MDA&quot; & Chr$(0) Filter$ = Filter$ & &quot;Text(*.txt)&quot; & Chr$(0) & &quot;*.TXT&quot; & Chr$(0) Filter$ = Filter$ & &quot;Batch(*.bat)&quot; & Chr$(0) & &quot;*.BAT&quot; & 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$ = &quot;My File Open Dialog&quot; & Chr$(0)

'* If the user does not specify an extension, append TXT. DefExt$ = &quot;TXT&quot; & 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$ = &quot;The file you chose was &quot; + FileName$ Else Message$ = &quot;No file was selected&quot; End If

MsgBox Message$

End Function

