Error code to delete five tables if they exist

  • Thread starter Thread starter Sandy
  • Start date Start date
S

Sandy

Hello!

I have code that creates five temporary tables and then
destroys them after use. I would like to put in error
code that will delete the table and proceed with the rest
of the stuff if the table exists.

Is there a shortcut way of doing this to avoid putting
code in for each individual table? In other words, I
don't want to have to put an if statement for each table.

Any suggestions will be greatly appreciated.

Sandy
 
Just go ahead and delete the table and then trap for the error if it doesn't
exist:

On Error Resume Next
DoCmd.DeleteObject acTable, "TableName1"
DoCmd.DeleteObject acTable, "TableName2"
DoCmd.DeleteObject acTable, "TableName3"
DoCmd.DeleteObject acTable, "TableName4"
DoCmd.DeleteObject acTable, "TableName5"

Above code will continue on to the next command because of the On Error
Resume Next. Be sure that you use a different On Error statement once this
section of code is done so that your other error trapping can be used. If
you want to trap for the specific error that occurs when you try to delete a
nonexistent table, then check for Error Number 7874.
 
Thanks, Ken. I'll give it a try.

Sandy
-----Original Message-----
Just go ahead and delete the table and then trap for the error if it doesn't
exist:

On Error Resume Next
DoCmd.DeleteObject acTable, "TableName1"
DoCmd.DeleteObject acTable, "TableName2"
DoCmd.DeleteObject acTable, "TableName3"
DoCmd.DeleteObject acTable, "TableName4"
DoCmd.DeleteObject acTable, "TableName5"

Above code will continue on to the next command because of the On Error
Resume Next. Be sure that you use a different On Error statement once this
section of code is done so that your other error trapping can be used. If
you want to trap for the specific error that occurs when you try to delete a
nonexistent table, then check for Error Number 7874.


--
Ken Snell
<MS ACCESS MVP>




.
 
Back
Top