If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Delete data in a table using vba

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Sep 2004
Posts: 112
Delete data in a table using vba

Hi there,
Does anyone know how I can run VBA code to delete all the data in a table??

Thanks
Reply With Quote
  #2 (permalink)  
Old
Registered User
 
Join Date: Jul 2004
Posts: 34
To delete info in a table, in vba, you're best off using the recordset method...

dim dbs as dao.database
dim rst as dao.recordset

set dbs = currentdb

set rst = dbs.openrecordset("tablename")

Then to delete records, find the relevant row in the recordset and...

rst.delete

will delete the row, just cycle to delete more rows.
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Sep 2004
Posts: 112
Great but I need to delete a hole tables contents with one routine so how would I do that?
Thanks
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: May 2004
Location: The Netherlands
Posts: 52
Easier to create a delete query an run that query:
Code:
Dim qdf As DAO.QueryDef
set qdf = CurrentDb.QueryDefs("Query_Name")
qdf.Execute
Or do it runtime:
Code:
Dim qdf as DAO.QueryDef
set qdf.Sql = "Delete * from Table_Name"
qdf.Execute
Note: running stored query's is faster than running queries generated at runtime.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Sep 2004
Posts: 161
dim toto as string
toto="Delete * from Table"
docmd.runsql toto
Reply With Quote
  #6 (permalink)  
Old
Grand Poobah
 
Join Date: Sep 2003
Location: MI
Posts: 3,713
And even easier ...

DoCmd.RunSQL "DELETE * FROM SomeTable;"
__________________
Back to Access ... ADO is not the way to go for speed ...
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: May 2004
Location: The Netherlands
Posts: 52
Quote:
Originally Posted by M Owen
And even easier ...

DoCmd.RunSQL "DELETE * FROM SomeTable;"
If I am right, DoCmd statements don't perform wel. The statement I replied should be faster.
(However, with the fast pc's of today, you probably even won't notice the difference)
__________________
A good developer is a lazy developer - don't be to lazy, share your knowledge
Reply With Quote
  #8 (permalink)  
Old
Grand Poobah
 
Join Date: Sep 2003
Location: MI
Posts: 3,713
Quote:
Originally Posted by Robje73
If I am right, DoCmd statements don't perform wel. The statement I replied should be faster.
(However, with the fast pc's of today, you probably even won't notice the difference)
Ok ... Well then:

CurrentProject.Connection.Execute "DELETE * FROM SomeTable;",,,AdCmdText

Since DELETE is an action query it should not take any appreciable time to execute ...

BTW this example is ADO ...
__________________
Back to Access ... ADO is not the way to go for speed ...
Reply With Quote
  #9 (permalink)  
Old
Registered User
 
Join Date: May 2004
Location: The Netherlands
Posts: 52
Quote:
Originally Posted by M Owen
Ok ... Well then:

CurrentProject.Connection.Execute "DELETE * FROM SomeTable;",,,AdCmdText

Since DELETE is an action query it should not take any appreciable time to execute ...

BTW this example is ADO ...
Hmm, sounds nice! I keep that in mind.
BTW, you have to choose between using ADO or DAO otherwise you can get problems with handling recordsets, querydefs and so on.
__________________
A good developer is a lazy developer - don't be to lazy, share your knowledge
Reply With Quote
  #10 (permalink)  
Old
Grand Poobah
 
Join Date: Sep 2003
Location: MI
Posts: 3,713
Quote:
Originally Posted by Robje73
Hmm, sounds nice! I keep that in mind.
BTW, you have to choose between using ADO or DAO otherwise you can get problems with handling recordsets, querydefs and so on.
Not necessarily ... As long as you have both libraries referenced ... The only confusion will come from the poor programmer who WILL be taking your name in vain ...
__________________
Back to Access ... ADO is not the way to go for speed ...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
Pingbacks are On
Refbacks are On