help with error checking

H

HX

I am using the code found on mvps.org, "API: Call the standard Windows
File/Save dialog box" in a small program, and am not sure how to implement
error checking in the case that the user clicks CANCEL on the File/Save
dialog box. Doing so in my program gives a run-time error code '2522'.
Following is the module that I initiate from a macro that is invoked when a
button is pushed:

********************************
Function RunIt()
Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.txt")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

DoCmd.TransferText acImportDelim, "MySpec", "MyTable", strInputFileName

End Function
*********************************

Currently, if the user clicks CANCEL on the dialog box, the run-time error
occurs because no information is being passed to DoCmd.TransferText.

So what can I put in here to check for that error, which will cause the
module to exit -- and IDEALLY, pass something back to the initiating macro
so that I can pop up a "Import Failed" message.

Even just pointing me to a tutorial online would be helpful - all my
searches for "error checking", in various forms, have resulted in people
trying to debug problems they are having with Access! Ugh!

Thanks in advance.
 
B

BruceM

If you click through the error message do you get the desired result? If
so, you can trap the error:

Function RunIt()

On Error GoTo ProcErr

Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.txt")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

DoCmd.TransferText acImportDelim, "MySpec", "MyTable", strInputFileName

ProcExit:
Exit Sub

ProcErr:
If Err.Number = 2522 Then
MsgBox "Import Failed"
Else
MsgBox "Error #" & Err.Number & "(" & Err.Description & ") -
RunIt"
End If
Resume ProcExit

End Function

The exact syntax of the MsgBox messages is up to you.

There is a freeware utility that can insert error checking into a procedure
with a button click (among other things). I haven't used the utility
extensively other than for error checking, but I have been quite satisfied
with what I have seen.
http://www.mztools.com/v3/mztools3.htm
 
J

John Spencer

Check to see if strInputFileName has a length. If so, then execute
transferText; if not don't.

********************************
Function RunIt()
Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.txt")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select an input file...", _
Flags:=ahtOFN_HIDEREADONLY)

IF Len(strInputFileName) > 0 Then
DoCmd.TransferText acImportDelim, "MySpec", "MyTable", strInputFileName
End IF

End Function
*********************************

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 

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