Solved

Deleting records in multiple tables in Access 2007 using one SQL statement

Posted on 2011-02-27
5
422 Views
Last Modified: 2012-05-11
Hi,

I am trying to delete records in several Access 2007 without writing a delete query for each table.

Here's what i have done so far.

1. I have madea  table called zz_tables_used_by_this_database which has all the relevant table names under the field [Table_Name]
I also have a field called Del_Table with yes or no values - yes being true for delete. :-)

When i open my database I want to delete any previous data that is sitting in tables in the DB.

Here's the code i have so far:

My problem with this code is that it only deletes a few tables then i get an error:  Syntax error in FROM clause.

As you can see my way of doing things is relatively simple so an answer that suits my level would be much appreciated.

Thanks!!!


Function Delete_Records()

Dim MyDB As Database '...........................................................'current database
Dim myrec As Recordset '..........................................................' the recordset
Dim strTableName_pas As String '......................................................'table with records to be dele
Dim strToDelete_pas As String '.........................................................'yes if records in table are to be deleted
Dim strSQL As String '.......................................................'sql string to delete records

Set MyDB = CurrentDb()
Set myrec = MyDB.OpenRecordset("zzTables_used_by_this_database")

DoCmd.SetWarnings False

myrec.MoveFirst
Do Until myrec.EOF
   
strTableName_pas = myrec![Table_name]
strToDelete_pas = myrec![Del_Table]


   strSQL = "DELETE * FROM " & strTableName_pas & " WHERE " & strToDelete_pas & "= 'True';"

   
    DoCmd.RunSQL (strSQL)
     

myrec.MoveNext
Loop
DoCmd.SetWarnings True


myrec.Close


End Function
0
Comment
Question by:DB_newbie_NZ
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 250 total points
ID: 34994456
It is possible that you have table names with special characters. If so, you will need to enclose them with square brackets.

You can also filter out the Del_Table='True' when opening the recordset.


Function Delete_Records()

Dim MyDB As Database '...........................................................'current database
Dim myrec As Recordset '..........................................................' the recordset
Dim strTableName_pas As String '......................................................'table with records to be dele
Dim strSQL As String '.......................................................'sql string to delete records

Set MyDB = CurrentDb()
Set myrec = MyDB.OpenRecordset("SELECT * FROM zzTables_used_by_this_database WHERE Del_Table = 'True'")

DoCmd.SetWarnings False

myrec.MoveFirst
Do Until myrec.EOF
   
   strTableName_pas = myrec![Table_name]

  strSQL = "DELETE * FROM [" & strTableName_pas & "];"
   
   DoCmd.RunSQL (strSQL)
     

myrec.MoveNext
Loop
DoCmd.SetWarnings True


myrec.Close


End Function
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 34994465
Try this ....

Function Delete_Records()

Dim MyDB As Database '...........................................................'current database
Dim myrec As Recordset '..........................................................' the recordset
Dim strTableName_pas As String '......................................................'table with records to be dele
Dim strSQL As String '.......................................................'sql string to delete records

Set MyDB = CurrentDb()
Set myrec = MyDB.OpenRecordset("SELECT * FROM zzTables_used_by_this_database WHERE Del_Table = 'yes'")

DoCmd.SetWarnings False

If myrec.RecordCount > 0  Then   'Recordset not empty
myrec.MoveFirst
Do Until myrec.EOF
   
strTableName_pas = myrec![Table_name]

   strSQL = "DELETE * FROM " & strTableName_pas & ";"

   
    DoCmd.RunSQL (strSQL)
     

myrec.MoveNext
Loop
DoCmd.SetWarnings True

End If

myrec.Close


End Function


ET
0
 
LVL 19

Assisted Solution

by:Eric Sherman
Eric Sherman earned 250 total points
ID: 34994476
Sorry Thomasain ...  I did not refresh before I made my post.  Your solution is pretty close to my suggestion.

ET
0
 

Author Closing Comment

by:DB_newbie_NZ
ID: 34995042
Thanks very much! Works now. The only thing I changed was to remove the " " around the Yes in the myrec definition.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 34999062
Ok, thanks for the points.  

If Del_Table is a Yes/No field then  ....

Yes and No values and fields that contain only one of two values (Yes/No, True/False, or On/Off). 1 bit.


ET
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 30
Question about DB Schema 27 56
date diff with Fiscal Calendar 4 31
MySQL Backup Strategy 15 25
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question