MS Access '97 Open File Dialog?

How can I program an Open File dialog behind a command button in Access '97? This should be easy but I'm tearing my hair out trying to figure it out. Thanks in advance for any assistance.
Who is Participating?
chewhoungConnect With a Mentor Commented:
You have to use api.
Create a new module under the Module tab from the database window and put the following line in it.

'Start module

  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustomFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As String
  lpstrFileTitle As String
  nMaxFileTitle As String
  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

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME)

'end module

then under your command button, put the following code:

'start code

' Call the Open File dialog box and look for *.txt files
Dim filebox As OPENFILENAME  ' structure that sets the dialog box
Dim fname As String  ' will receive selected file's name
Dim retval As Long  ' return value

' Configure how the dialog box will look
filebox.lStructSize = Len(filebox)  ' the size of the structure
filebox.hwndOwner = Form1.hWnd  ' handle of the form calling the function
filebox.lpstrTitle = "Open File"  ' text displayed in the box's title bar
' The next line sets up the file types drop-box
filebox.lpstrFilter = "Text Files" & vbNullChar & "*.txt" & vbNullChar & "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
filebox.lpstrFile = Space(255)  ' initalize buffer that receives path and filename of file
filebox.nMaxFile = 255  ' length of file and pathname buffer
filebox.lpstrFileTitle = Space(255)  ' initialize buffer that receives filename of file
filebox.nMaxFileTitle = 255  ' length of filename buffer
' Allow only existing files and hide the read-only check box

' Execute the dialog box
retval = GetOpenFileName(filebox)
If retval <> 0 Then  ' if the dialog box completed successfully
  ' Remove null space from the file name
  fname = Left(filebox.lpstrFile, InStr(filebox.lpstrFile, vbNullChar) - 1)
  Debug.Print "The selected file: "; fname
End If

'end code

The example above will display a open file dialog box for you to select all file with extension *.txt.
You can set your own filter by setting the line with:
in the above example.
Put the following code at the end of the new module you have created.

Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_PATHMUSTEXIST = &H800

and change the following line in the command button code:

filebox.hwndOwner = Form1.hWnd  ' handle of the form calling the function


filebox.hwndOwner = 0&

Sorry another correction:
In the new module, change the following line:

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME)


Declare Function GetOpenFileName% Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME)

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Pegasus100397Author Commented:

  It seems to work right up to the point where it encounters the line:


where it generates an exception error and exits access. The exception is error 0xc0000005 at address 0x77f34e70

FYI: I'm running Access '97 with Windows NT 4.0

Thanks in advance for your assistance!
I would have used (but it is probably the same)

Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOPENFILENAME As OPENFILENAME) as Boolean

See this article for a full list of declarations and functions:

"Call the standard Windows File Open/Save dialog box" -{D83F04AB-48B7-11D3-A9BB-005004227874}

Copy/Paste the whole link, include the {..} part. EE does not create the proper URL...
Pegasus100397Author Commented:
Thanks Trygve but I'm still getting the same error. Wierd! I'm sure its something small but thanks for the input!
Post your current code so that we can verify that it is correct.
Pegasus100397Author Commented:
it works now, thanks! Was a mispelling in the Start module on my part.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.