Call the standard Windows File Open/Save dialog box

G

Guest

I'm using the code found in www.mvps.org/access/api/api0001.htm. It works
great, but I'm trying to more gracefully handle the situation when the user
clicks Cancel in the dialog box. Currently, when you click cancel you get an
"Action or method requires a file name argument" message.

Ideally, I'd like to suppress this message and exit the function - I was
kind of thinking this would do it, but no luck - it's not trapping that error.

Public Function Get_FileNamexls() As String
Dim strFilter As String
Dim lngFlags As Long

On Error GoTo Err_Get_FileNamexls
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
Get_FileNamexls = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Select File To Import/Export")

Exit_Get_FileNamexls:
Exit Function

Err_Get_FileNamexls:
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Get_FileNamexls

End Function

How can I more gracefully handle the Cancel button click?
 
G

Guest

It's a question of trapping the error that is generated when the user cancels
the operation.

So what error number is displayed?

Once you identified that you simply need to change your error handler to
handle that specifc error number; such as:

Function ....()
Dim lngFlags As Long

On Error GoTo Err_Get_FileNamexls
strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", "*.xls")
Get_FileNamexls = ahtCommonFileOpenSave(InitialDir:="C:\", _
Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
DialogTitle:="Select File To Import/Export")

Exit_Get_FileNamexls:
Exit Function

Err_Get_FileNamexls:
If err.number = YourErrorNumberForCancellingGoesHere then
'Do whatever you'd like
Exit Function 'perhaps
Else
MsgBox Err.Number & ": " & Err.Description
Resume Exit_Get_FileNamexls
End if
End Function

Daniel P
 
G

Guest

The problem is, my error trapping isn't throwing an error number so I can't
handle it.
 
D

Douglas J. Steele

I don't think that function is what's raising the error: it's where you're
using the result of Get_FileNamexls.

Rather than simply plugging the return value of the function into your code,
check that its length is greater than 0 before proceeding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top