views:

271

answers:

2

I have imported a tab-delimited text file in an Access database on a button click event.

The file is properly imported, the issue comes if the user selects a text file of different format, the code will import the improper data into the database and creates a new ErrorLog table.

How do I restrict improper data from table? How to do error handling? If a user selects a file that is of improper format (rather than improper data, which generatings an ErrorLog), it will pop up a MsgBox, telling the user that the file if not in proper format.

Will be highly thankful for your Help.

+1  A: 

For error handling in VBA you have to use On Error statement. One of best practices to do so in VBA is:

Sub example()

On Error GoTo err_hndl

(.....do something....)

Exit Sub
err_hndl:
MsgBox("We got an error!")
End Sub

Please note "Exit Sub" right before error handler. It prevents the code of error handler from being executed every time.

juckobee
That doesn't really help, as by the time the error has happened, the import errors table will have been created. What I do with this is to try linking the import file first and if it doesn't match the import spec, it will throw an error. If it doesn't, then do the import instead of the link.
David-W-Fenton
If there is a considered case of having an import file that doesn't have an import spec, you should try to quit gracefully from it rather than throw an error. Errors should be to handle unexpected events by trapping then and, if possible, continue execution safely.
A. Scagnelli
A: 

I don't really understand from your message how you detect improper data and create a new ErrorLog table, but in this case you should use Transactions to rollback the modifications once you detected that the ErrorLog table has been created.

Michael Pereira