Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 15th, 2005, 08:53 AM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to delete all the records in a table

Hi

I want to create a button in a form, so that when I click the button it will autumatically delete all the records in three tables: CASH, POSITION and COMPOSITE.

I am a very beginner of VBA and I was following a book to write this:

Private Sub Command32_Click()
Dim sql As Collection
sql.Execute "DELETE FROM CASH, POSITION, COMPOSITE"

End Sub

It should be a very simple thing but....

Please help me!

Thanks.

Kai Lai


 
Old May 15th, 2005, 09:17 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are sort of on the right track. But when you're working with objects (in your example "sql" is an object of type "Collection") you must create an instance of the object. In your example, this can be done by:

Dim sql as New Collection

-OR-

Dim sql as Collection
Set sql = New Collection

But when it comes to Access VBA I think you're headed down the wrong path. You don't need to create objects to accomplish this task. And I think you have a long way to go before you understand how objects work. I don't know what book you're looking at. But it doesn't look like it has much to do with Access.

What you want to do accomplish can be done in Access like this...
Code:
DoCmd.RunSQL "DELETE * FROM CASH"
DoCmd.RunSQL "DELETE * FROM POSITION"
DoCmd.RunSQL "DELETE * FROM COMPOSITE"
Note that you must break the SQL statement up to make valid SQL statements.
If you don't want your users to be warned that records will be deleted, you can put
Code:
DoCmd.SetWarnings False
before the first RunSQL, and
Code:
DoCmd.SetWarnings True
after the last RunSQL.

There are other ways to accomplish this as well. For instance, you could create 3 queries that will delete from each table. Then use a syntax similar to:
Code:
DoCmd.OpenQuery "qryDeleteAllCashRecords"
to run each query.

You might want to play around with the Northwind sample database to see some code.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
The Following User Says Thank You to rjweers For This Useful Post:
Gerrit (March 15th, 2016)
 
Old March 15th, 2016, 07:20 AM
Registered User
 
Join Date: Mar 2016
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I see that this is an old post but I have just registered to say thank you to rjweers.

Great forums site BTW!





Similar Threads
Thread Thread Starter Forum Replies Last Post
delete null records stealthdevil Access VBA 3 January 23rd, 2007 04:48 PM
Delete Duplicate Records prasanta2expert SQL Server 2000 9 December 15th, 2006 10:44 PM
Edit or Delete records tsimsha Classic ASP Basics 6 October 30th, 2004 03:26 AM
delete records in database shoakat Classic ASP Databases 9 October 26th, 2004 08:31 PM
How Can I Delete Multiple Records From a Table? Lucy SQL Server 2000 5 May 12th, 2004 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.