Solved

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

Posted on 2011-02-27
5
419 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now