[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-02-27
5
Medium Priority
?
437 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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