• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

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

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
DB_newbie_NZ
Asked:
DB_newbie_NZ
  • 3
2 Solutions
 
ThomasianCommented:
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
 
Eric ShermanAccountant/DeveloperCommented:
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
 
Eric ShermanAccountant/DeveloperCommented:
Sorry Thomasain ...  I did not refresh before I made my post.  Your solution is pretty close to my suggestion.

ET
0
 
DB_newbie_NZAuthor Commented:
Thanks very much! Works now. The only thing I changed was to remove the " " around the Yes in the myrec definition.
0
 
Eric ShermanAccountant/DeveloperCommented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now