|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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] |
|
#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 |
|
#6
|
|||
|
|||
|
Thanks
Quote:
Thanks, I used suggestions from both you and smellykc and things are working great. Just as I had hoped. dennywj |
|
#7
|
|||
|
|||
|
Thanks
Quote:
Your sugestion worked great. I did add the code to eliminate the "Do you really want to do this" that the system aasks. dennywj |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft Access Development > How to delete the contents of a table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|