Connecting Tech Pros Worldwide Forums | Help | Site Map

Clearing table

Newbie
 
Join Date: Nov 2008
Posts: 21
#1: Nov 20 '08
Is there a clever way of enabling a user to clear all data from a table by simply clicking a button on a form?
At present I'm doing it a rather clumsy and unreliable way using macros to -
Transferdatabase (importing to a new temporary table)
Transferdatabase (exporting the structure back)
Deleteobject (deleting temporary table)
There must be a better way!
Thanks.
Kev

DonRayner's Avatar
Expert
 
Join Date: Sep 2008
Location: Canada
Posts: 494
#2: Nov 20 '08

re: Clearing table


Quote:

Originally Posted by KEV999

Is there a clever way of enabling a user to clear all data from a table by simply clicking a button on a form?
At present I'm doing it a rather clumsy and unreliable way using macros to -
Transferdatabase (importing to a new temporary table)
Transferdatabase (exporting the structure back)
Deleteobject (deleting temporary table)
There must be a better way!
Thanks.
Kev

Why not try a delete query
Newbie
 
Join Date: Nov 2008
Posts: 21
#3: Nov 20 '08

re: Clearing table


Thanks for the reply.
Could you expand a bit on what you mean please?
(I'm very new to this!)
Thanks.
Kev
DonRayner's Avatar
Expert
 
Join Date: Sep 2008
Location: Canada
Posts: 494
#4: Nov 20 '08

re: Clearing table


Quote:

Originally Posted by KEV999

Thanks for the reply.
Could you expand a bit on what you mean please?
(I'm very new to this!)
Thanks.
Kev

In the On Click event for your button you need to put something like the following. Replace MyTable with the name of the table you want to delete all the records from.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings = False
  2. Dim SqlStr as string
  3. SqlStr = "Delete[MyTable].* From [MyTable]"
  4. DoCmd.RunSql SqlStr
  5. DoCmd.SetWarnings = True
  6.  
If you are not comfortable with VBA then you will have to create the delete query using the access query builder. Just make it like you were creating a normal select query. Then before you save it you need to select Delete Query from the Query menu on the menu bar and then save it. You can then use whatever means your comfortable with to run the query.
Newbie
 
Join Date: Nov 2008
Posts: 21
#5: Nov 20 '08

re: Clearing table


Many thanks.
Got there eventually using your script.
Very grateful!
Kev
DonRayner's Avatar
Expert
 
Join Date: Sep 2008
Location: Canada
Posts: 494
#6: Nov 20 '08

re: Clearing table


Quote:

Originally Posted by KEV999

Many thanks.
Got there eventually using your script.
Very grateful!
Kev

Glad I could be of some help.
Reply


Similar Microsoft Access / VBA bytes