Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 15th, 2005, 08:53 AM
Registered User
 
Join Date: May 2005
Location: , , .
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


Reply With Quote
  #2 (permalink)  
Old May 15th, 2005, 09:17 AM
Friend of Wrox
 
Join Date: Nov 2004
Location: Seattle, WA, .
Posts: 248
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote
Reply


Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
delete null records stealthdevil Access VBA 3 January 23rd, 2007 03:48 PM
Delete Duplicate Records prasanta2expert SQL Server 2000 9 December 15th, 2006 09: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



All times are GMT -4. The time now is 03:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.