Solved

Delete Query Problem

Posted on 2008-06-16
6
194 Views
Last Modified: 2013-11-05
Hi,

I'm trying to create a query that would delete a record out of a table (NeedMethodsTable) if the record's specific fields (Invoice, Item) don't exist in a different query (InvoiceItems), and also if the record entered in is not more than 1 day old

This is what i have so far, but apparently the deletition of the record only works on the 1 day old part, but not the rest.

DELETE *
FROM NeedMethodTable
WHERE (Exists (SELECT [InvoiceItems].INVOICE, [InvoiceItems].ITEM  FROM  [InvoiceItems] WHERE (
      ((NeedMethodTable.Complete) Is Null Or (NeedMethodTable.Complete)<>-1)
AND
((NeedMethodTable.[Date/Time])>DateAdd("h",-24,Now()))
AND ([InvoiceItems]![INVOICE] <> [NeedMethodTable.INVOICE])
AND ([InvoiceItems]![ITEM] <> [NeedMethodTable.Part Number])
))<>False);
0
Comment
Question by:repco
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:masterjojobinks
ID: 21795246
Try this:

SELECT [needmethodstable]![Invoice] & [needmethodstable]![partnumber] AS [key], [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False AS Expr1
FROM needmethodstable
WHERE ((([needmethodstable]![Invoice] & [needmethodstable]![partnumber]) Not In (SELECT [invoiceitems]![invoice] & [invoiceitems]![item] AS [Key] FROM invoiceitems GROUP BY [invoiceitems]![invoice] & [invoiceitems]![item];) And [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False));
0
 
LVL 8

Expert Comment

by:masterjojobinks
ID: 21795252
opps forgot the delete part:

DELETE needmethodstable.*, [needmethodstable]![Invoice] & [needmethodstable]![partnumber] AS [key], [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False AS Expr1
FROM needmethodstable
WHERE ((([needmethodstable]![Invoice] & [needmethodstable]![partnumber]) Not In (SELECT [invoiceitems]![invoice] & [invoiceitems]![item] AS [Key] FROM invoiceitems GROUP BY [invoiceitems]![invoice] & [invoiceitems]![item];) And [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False));
0
 
LVL 8

Accepted Solution

by:
masterjojobinks earned 500 total points
ID: 21795268
Darn typos (forget a space)

DELETE needmethodstable.*, [needmethodstable]![Invoice] & [needmethodstable]![part number] AS [key], [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False AS Expr1
FROM needmethodstable
WHERE ((([needmethodstable]![Invoice] & [needmethodstable]![part number]) Not In (SELECT [invoiceitems]![invoice] & [invoiceitems]![item] AS [Key] FROM invoiceitems GROUP BY [invoiceitems]![invoice] & [invoiceitems]![item];) And [needmethodstable]![date/time]>DateAdd("h",-24,Now())<>False));
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:repco
ID: 21795293
hi thank you for quick response but it gives me a syntax error
0
 
LVL 8

Expert Comment

by:masterjojobinks
ID: 21795306
Check the field names I put in the query to make sure they match your database.
0
 

Author Comment

by:repco
ID: 21795309
nevermind, had to put needmethodstable in brackets. it worked great! thanks!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

947 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

21 Experts available now in Live!

Get 1:1 Help Now