Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 13th, 2005, 11:37 AM
Dennywj Dennywj is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 6 Dennywj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 23 sec
Reputation Power: 0
Talking How to delete the contents of a table

I am a novice at Access and can do many of the basic things. In my program I have a single column table that captures a number from a barcode reader. I need to create a macro/command that will delete the contents of that table so it can be re-populated at a later date. I can do it manually but don't want users to use that approach. Would like to give them a single button that does it automatically. Thanks, dennywj

Reply With Quote
  #2  
Old May 13th, 2005, 03:16 PM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
You can use the following code behind the On Click event for a command button:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT TableName.* FROM TableName"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
Do Until rs.RecordCount = 0
With rs
.MoveFirst
.Delete
.MoveNext
End With
Loop

Just use the name of your table in the above code.

lwells

Reply With Quote
  #3  
Old May 17th, 2005, 12:40 PM
smellykc smellykc is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 2 smellykc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 31 sec
Reputation Power: 0
You can also try putting the following line in the same spot and it should do the same thing:

DoCmd.RunSQL ("DELETE TableName.* FROM TableName")

This deletes every line of every column at once instead of one record at a time.

Reply With Quote
  #4  
Old May 17th, 2005, 02:06 PM
Dennywj Dennywj is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 6 Dennywj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 23 sec
Reputation Power: 0
Smile Deleting Contets of a Table

Thanks for the support. The code is not work. I get a compile error that says "User-defined type not defined" and the "Dim db As DAO.Database" line is highlighted. It also returns the same error if I delete the first line. I am using Access 2000. Do I need to add any additional information.

Thanks
QUOTE=lwells]You can use the following code behind the On Click event for a command button:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
strSQL = "SELECT TableName.* FROM TableName"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
Do Until rs.RecordCount = 0
With rs
.MoveFirst
.Delete
.MoveNext
End With
Loop

Just use the name of your table in the above code.

lwells[/QUOTE]

Reply With Quote
  #5  
Old May 18th, 2005, 09:19 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 5
Make sure you have the reference checked to the DAO Object Library. While in the code window, click Tools->References from the tool bar and make sure that the library is checked. If not scroll down the list until you find it. Then click Debug -> Compile and that should correct the problem. Also the post given by smellykc will work quite well but you will receive a warning message that you are about to delete records. If you don't want to receive the warning message each time the user runs the code, you can turn the default warning message off just before you run the code and then turn it back on after you run the code. DoCmd.SetWarnings False and then DoCmd.SetWarnings True

lwells

Reply With Quote
  #6  
Old June 1st, 2005, 05:19 PM
Dennywj Dennywj is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 6 Dennywj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 23 sec
Reputation Power: 0
Thanks

Quote:
Originally Posted by lwells
Make sure you have the reference checked to the DAO Object Library. While in the code window, click Tools->References from the tool bar and make sure that the library is checked. If not scroll down the list until you find it. Then click Debug -> Compile and that should correct the problem. Also the post given by smellykc will work quite well but you will receive a warning message that you are about to delete records. If you don't want to receive the warning message each time the user runs the code, you can turn the default warning message off just before you run the code and then turn it back on after you run the code. DoCmd.SetWarnings False and then DoCmd.SetWarnings True

lwells


Thanks, I used suggestions from both you and smellykc and things are working great. Just as I had hoped.

dennywj

Reply With Quote
  #7  
Old June 1st, 2005, 05:23 PM
Dennywj Dennywj is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 6 Dennywj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 15 m 23 sec
Reputation Power: 0
Thanks

Quote:
Originally Posted by smellykc
You can also try putting the following line in the same spot and it should do the same thing:

DoCmd.RunSQL ("DELETE TableName.* FROM TableName")

This deletes every line of every column at once instead of one record at a time.


Your sugestion worked great. I did add the code to eliminate the "Do you really want to do this" that the system aasks.
dennywj

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > How to delete the contents of a table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT