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.
LVL 3
Pegasus100397Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chewhoungCommented:
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

Type OPENFILENAME
  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
filebox.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY

' 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:
filebox.lpstrFilter
in the above example.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chewhoungCommented:
Correction:
Put the following code at the end of the new module you have created.

'start
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_PATHMUSTEXIST = &H800
'end


and change the following line in the command button code:

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

to:

filebox.hwndOwner = 0&

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

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


to

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


0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Pegasus100397Author Commented:
Chewhound,

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

retval=GetOpenFileName(filebox)

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!
Pegasus
0
TrygveCommented:
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" - http://www.netservice.no/Trygve/WIZ/FrameText.asp?ArticleID={D83F04AB-48B7-11D3-A9BB-005004227874}

Copy/Paste the whole link, include the {..} part. EE does not create the proper URL...
0
Pegasus100397Author Commented:
Thanks Trygve but I'm still getting the same error. Wierd! I'm sure its something small but thanks for the input!
0
TrygveCommented:
Post your current code so that we can verify that it is correct.
0
Pegasus100397Author Commented:
it works now, thanks! Was a mispelling in the Start module on my part.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.