Error Handling on using Common Dialog...

  • Thread starter ThriftyFinanceGirl
  • Start date
T

ThriftyFinanceGirl

I have a procedure that runs an import based on the user choosing a file. I
want them to be able to cancel. I got the call for the common dialog from
the guys here on this group but it doesn't have error handling for the
occasion when the user chooses "cancel" instead of choosing a file.... Help?!
Don't know how to create a handler or where to put it exactly....

Public Function TaxExemptImport()

'Now open the small form that will show progress...
DoCmd.OpenForm "frmTaxExempt"

'Let's get the file name first....
Dim strFilter As String
Dim strInputFileName As String

strFilter = ahtAddFilterItem(strFilter, "Excel files (*.XLS)", "*.XLS")
strInputFileName = ahtCommonFileOpenSave( _
Filter:=strFilter, OpenFile:=True, _
DialogTitle:="Please select file to Import...", _
Flags:=ahtOFN_HIDEREADONLY)




DoCmd.SetWarnings False
Application.Echo False
'bring data into the holding table
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9,
"TaxExemptImport", strInputFileName
============================================
Currently if the user chooses "cancel" in the Open dialog box, the code
stops on the "Docmd.TransferSpreadsheet" line. Where do I trap the error and
how?
 
J

Jack Leach

Regardless of any issues with the common dialog, you should have error
handling in every single procedure. It's simple and essential to set up
basic error handling.

Public Function DoSomething()
On Error Goto Err_SomeName

'Your normal code here

Exit_SomeName:
'Cleanup here
Exit Function
Err_SomeName:
MsgBox Err.Number & " " & Err.Description
Resume Exit_SomeName
Resume
End Function


That's the basic structure that should be in every procedure. Here's some
links on the subject...

http://allenbrowne.com/ser-23a.html
http://www.fmsinc.com/free/NewTips/VBA/BasicErrorHandling.asp
http://www.fmsinc.com/free/NewTips/VBA/Avoid_Procedure_Exits.html

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 

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