?
Solved

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

Posted on 2011-02-27
5
Medium Priority
?
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 750 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 750 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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