473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Deleting records from multiple tables

Midzie
25
Hi All, first of all please don't delete my post... I need some answers on it that's why I posted a question here. If anyone there could help me please, I am deleting records from multiple tables but it doesn't work for me.

Expand|Select|Wrap|Line Numbers
  1. StrSQL = ""
  2.  
  3. PurgeMsg = MsgBox("Do you want to purge data?", 1, "Purging Data")
  4.     If PurgeMsg = 1 Then
  5.         'delete from order
  6.         StrSQL = StrSQL & "DELETE [Order] FROM [Order] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] BETWEEN NOW AND DATEADD('yyyy', -2, NOW); "
  7.         'delete from Schedule
  8.         StrSQL = StrSQL & "DELETE [Schedule] FROM [Schedule] A INNER JOIN [Closed] B ON A.[PNum] = B.[Wnum] WHERE B.[CompDate] BETWEEN NOW AND DATEADD('yyyy', -2, NOW); "
  9.         'delete from Note
  10.         StrSQL = StrSQL & "DELETE [Note] FROM [Note] A INNER JOIN [Closed] B ON A.[NoteNum] = B.[Wnum] WHERE B.[CompDate] BETWEEN NOW AND DATEADD('yyyy', -2, NOW); "
  11.         'delete from Yearly
  12.         StrSQL = StrSQL & "DELETE [Yearly] FROM [Yearly] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] BETWEEN NOW AND DATEADD('yyyy', -2, NOW); "
  13.         'delete from Closed
  14.         StrSQL = StrSQL & "DELETE FROM [Closed] WHERE [CompDate] BETWEEN NOW AND DATEADD('yyyy', -2, NOW)"
  15.  
  16.         MsgBox "Purging Successful!"
  17.     Else
  18.         MsgBox "Purging Failed!"
  19.     End If
Feb 20 '12 #1

✓ answered by NeoPa

Hi Midzie.

First of all, posts are never deleted unless they break the rules of the site. I suggest you concentrate on asking questions properly rather than asking for exceptions to be made for you regarding site rules. We would rather answer your questions than delete them anyway. Sometimes you make that impossible though. I'm sure we can all be happy though, that your posts will never be deleted if they conform to the rules.

Please read Before Posting (VBA or SQL) Code. There are various threads which explain how questions should be asked. Please read at least one of them before submitting any other questions. It will help to ensure your questions don't get deleted in future.

This question is borderline, as you haven't even explained what you mean by :
Midzie:
I am deleting records from multiple tables but it doesn't work for me.
I'm guessing that nothing ever gets deleted, because there's actually no code there to do the deleting.

It may help to understand that SQL is not VBA. That sounds pretty obvious, but actually many beginners see code with SQL command data in strings, without realising that this command data doesn't ever do anything unless it is somehow passed to the SQL engine to be processed and executed. Your (VBA) code sets up the SQL strings ok, but never actually calls the SQL engine (Jet) to process them, which means nothing will happen.

Also, Jet SQL (used in Access) doesn't allow multiple SQL commands in the same invocation, so each DELETE query needs to be separate.

Try instead :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim cdb As DAO.Database
  3.  
  4. Set cdb = CurrentDb
  5. StrSQL = ""
  6. If MsgBox(Prompt:="Do you want to purge data?", _
  7.           Buttons:=vbYesNo, _
  8.           Title:="Purging Data") = vbYes Then
  9.     'delete from order
  10.     StrSQL = "DELETE [Order] FROM [Order] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  11.     Call cdb.Execute(strSQL)
  12.     'delete from Schedule
  13.     StrSQL = "DELETE [Schedule] FROM [Schedule] A INNER JOIN [Closed] B ON A.[PNum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  14.     Call cdb.Execute(strSQL)
  15.     'delete from Note
  16.     StrSQL = "DELETE [Note] FROM [Note] A INNER JOIN [Closed] B ON A.[NoteNum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  17.     Call cdb.Execute(strSQL)
  18.     'delete from Yearly
  19.     StrSQL = "DELETE [Yearly] FROM [Yearly] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  20.     Call cdb.Execute(strSQL)
  21.     'delete from Closed
  22.     StrSQL = "DELETE FROM [Closed] WHERE [CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  23.     Call cdb.Execute(strSQL)
  24.     MsgBox "Purging Successful!"
  25. Else
  26.     MsgBox "Purging Failed!"
  27. End If

3 2221
Seth Schrock
2,965 Expert 2GB
First of all, you need to define 5 different StrSQL variables, one for each DELETE query. You can't run them all together as they are now. You also have to tell the VBA to run the SQL code. I have no way of knowing if your SQL code is good since the question doesn't give me enough information (possibly why your post has been deleted in the past?). What I would recommend doing is to create a query object and test your code in there. This allows you to see what is the problem if a problem does exist. You can then use VBA to open the queries using the DoCmd.OpenQuery command or you can paste the SQL code back into your VBA code. I personally would do the former just because I'm more familiar with this method and I don't remember how to trigger the SQL code to be performed. I have done it, but I would have to go look in some of my other projects tomorrow when I'm at work.
Feb 20 '12 #2
NeoPa
32,556 Expert Mod 16PB
Hi Midzie.

First of all, posts are never deleted unless they break the rules of the site. I suggest you concentrate on asking questions properly rather than asking for exceptions to be made for you regarding site rules. We would rather answer your questions than delete them anyway. Sometimes you make that impossible though. I'm sure we can all be happy though, that your posts will never be deleted if they conform to the rules.

Please read Before Posting (VBA or SQL) Code. There are various threads which explain how questions should be asked. Please read at least one of them before submitting any other questions. It will help to ensure your questions don't get deleted in future.

This question is borderline, as you haven't even explained what you mean by :
Midzie:
I am deleting records from multiple tables but it doesn't work for me.
I'm guessing that nothing ever gets deleted, because there's actually no code there to do the deleting.

It may help to understand that SQL is not VBA. That sounds pretty obvious, but actually many beginners see code with SQL command data in strings, without realising that this command data doesn't ever do anything unless it is somehow passed to the SQL engine to be processed and executed. Your (VBA) code sets up the SQL strings ok, but never actually calls the SQL engine (Jet) to process them, which means nothing will happen.

Also, Jet SQL (used in Access) doesn't allow multiple SQL commands in the same invocation, so each DELETE query needs to be separate.

Try instead :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. Dim cdb As DAO.Database
  3.  
  4. Set cdb = CurrentDb
  5. StrSQL = ""
  6. If MsgBox(Prompt:="Do you want to purge data?", _
  7.           Buttons:=vbYesNo, _
  8.           Title:="Purging Data") = vbYes Then
  9.     'delete from order
  10.     StrSQL = "DELETE [Order] FROM [Order] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  11.     Call cdb.Execute(strSQL)
  12.     'delete from Schedule
  13.     StrSQL = "DELETE [Schedule] FROM [Schedule] A INNER JOIN [Closed] B ON A.[PNum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  14.     Call cdb.Execute(strSQL)
  15.     'delete from Note
  16.     StrSQL = "DELETE [Note] FROM [Note] A INNER JOIN [Closed] B ON A.[NoteNum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  17.     Call cdb.Execute(strSQL)
  18.     'delete from Yearly
  19.     StrSQL = "DELETE [Yearly] FROM [Yearly] A INNER JOIN [Closed] B ON A.[Wnum] = B.[Wnum] WHERE B.[CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  20.     Call cdb.Execute(strSQL)
  21.     'delete from Closed
  22.     StrSQL = "DELETE FROM [Closed] WHERE [CompDate] Between DateAdd('yyyy', -2, Date()) And Date()"
  23.     Call cdb.Execute(strSQL)
  24.     MsgBox "Purging Successful!"
  25. Else
  26.     MsgBox "Purging Failed!"
  27. End If
Feb 20 '12 #3
Midzie
25
Hi Neopa and Seth, thanks for helping me. My purging button is already working. I have a follow up question on this, which can be found at How do I Log Multiple Values in One Message.
Feb 21 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: TeleTech1212 | last post by:
I am trying to select specific columns from multiple tables based on a common identifier found in each table. For example, the three tables: PUBACC_AC PUBACC_AM PUBACC_AN each have a...
3
by: svdh2 | last post by:
I have been looking at a problem the entire week in access, I have still not been able to find a solution. Hope that you could maybe tell where to look Concerns the link between Access and Word....
1
by: Brian | last post by:
I'm trying to find a way to search multiple tables for the same record. Say I have 3 tables all with a name column, I need to search all 3 tables and find matching names. Is there an easy way to...
13
by: ricky.agrawal | last post by:
I'm really not sure how to go about this in Access. What I've created is a table for each location. Those tables are identical in format but different in information. The tables are named after...
6
by: ApexData | last post by:
I have 2 tables: Table1 and Table2. Neither one has a primary key because each table will only have 1-record. My form is a SingleForm unbound with tabs (my desire here). Using this form, in...
8
by: fonzie | last post by:
Is it possible to have a data entry form where the information is stored in several different tables (5 or 6)? I have an inventory database where Table1 stores all of the data common to all...
12
daniel aristidou
by: daniel aristidou | last post by:
hi i was wondering if it is possible to filter multiple numbers of tables at the same time. the filter would be automatically applied ie. as in a query. However i want only one list to appear...
4
by: aalmakto | last post by:
Hi all, Whats wrong with this syntax? DELETE from fighterSponsor fs, sponsors s, imageData id where fs.sponsorId=s.sponsorId AND id.imageId=s.imageId AND (s.imageId=81 OR s.imageId=82) I...
0
by: redpears007 | last post by:
Morning all! :) I have a database with multiple linked tables. I have created a search form with one txt box, for entering search criteria, and a listbox for each of the tables to isplay the...
1
by: DMAGIC448 | last post by:
I am trying to figure out how to add records to multiple tables w/ one form. I am making a database for a Home DayCare and I want to add/update records for 5 tables (Student info, Parent 1 Info,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.