PDA

View Full Version : Drop TempTable whener sub puits running



dhartford
01-06-2009, 05:45 PM
I create a temptable to hold data for a report, then drop it after report is generated. It work fine. What I need now is to drop the temptable whener the Sub stopping running regardless, e.g got error or in middle of debugging. Can DoCmd do that?

Any help is appreciated. Thanks

CreganTur
01-07-2009, 06:16 AM
Personally, I use DDL to handle table actions. An example would be:
DoCmd.RunSQL "DROP TABLE TableName"
The above example will delete the named table. Just replace TableName with the table's actual name.


What I need now is to drop the temptable whener the Sub stopping running regardless, e.g got error or in middle of debugging.
You need to setup some error trapping to handle this. Example:
Sub SubName()
'variable declarations
On Error GoTo ErrorHandler
'code you want to run
DoCmd.RunSQL "DROP TABLE TableName"
Exit Sub

ErrorHandler:
DoCmd.RunSQL "DROP TABLE TableName"
End Sub

HTH:thumb

dhartford
01-07-2009, 08:19 AM
You need to setup some error trapping to handle this. Example:
Sub SubName()
'variable declarations
On Error GoTo ErrorHandler
'code you want to run
DoCmd.RunSQL "DROP TABLE TableName"
Exit Sub

ErrorHandler:
DoCmd.RunSQL "DROP TABLE TableName"
End Sub

HTH:thumb

Randy, Thanks for your help.

What if during the debug, I want to quit the program by click on stop (reset) buttom after the temptable is created? How do I set up error trapping on that?

CreganTur
01-07-2009, 08:38 AM
What if during the debug, I want to quit the program by click on stop (reset) buttom after the temptable is created? How do I set up error trapping on that?
You can't. You'll need to delete the table by hand, or have a sub in a module that you can run to delete the table for you.

Errors only respond to program errors. Manually stopping your code when debugging is not an error.