Solved

MS Access SQL DELETE Query

Posted on 2006-12-01
6
519 Views
Last Modified: 2008-02-01
I am trying to use this code to delete related records for a item in inventory if the item itself is removed.

Here's what I have for an On-Click event:

Dim SQL As String

If MsgBox("Are you sure you want to delete this item from inventory and all corresponding items?", vbYesNo) = vbYes Then

SQL = "DELETE DISTINCTROW [Equipment Inventory Main].* FROM [Equipment Inventory Main] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Main].UIC = [Equipment Inventory].UIC"

SQL = "DELETE DISTINCTROW [Equipment Inventory Check InOut].* FROM [Equipment Inventory Check InOut] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Check InOut].UIC = [Equipment Inventory].UIC"

SQL = "DELETE DISTINCTROW [Equipment Inventory Maintenance Log].* FROM [Equipment Inventory Maintenance Log] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Maintenance Log].UIC = [Equipment Inventory].UIC"

    DoCmd.RunSQL SQL
Else

End If

The table EQUIPMENT INVENTORY is where the items are stored and the field UIC is the Unique Identifier Code that relates the item to the other tables. When I click on the button no errors come up but it doesn't delete anything. Any ideas?
0
Comment
Question by:MDauphinais1
  • 3
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 18054017
First off, you have to execute yourDoCmd.RunSQL SQL line after every declaration
Second, I don't think you can use DISTINCTROW in a delete query.

SQL = "DELETE * FROM [Equipment Inventory Main] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Main].UIC = [Equipment Inventory].UIC"
DoCmd.RunSQL SQL

SQL = "DELETE * FROM [Equipment Inventory Check InOut] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Check InOut].UIC = [Equipment Inventory].UIC"
DoCmd.RunSQL SQL

SQL = "DELETE * FROM [Equipment Inventory Maintenance Log] RIGHT JOIN [Equipment Inventory] ON [Equipment Inventory Maintenance Log].UIC = [Equipment Inventory].UIC"
DoCmd.RunSQL SQL
0
 

Author Comment

by:MDauphinais1
ID: 18054317
Say I have inventory item 22 open, when I click the delete button I want it to only delete all the records in the other tables that have 22 as the UIC code.

With the above codes it compairs the Equipment Inventory tables with the other tables and deletes any record where the UIC codes match (in essence every record in the other tables). How to I change it to only compair with the UIC code of the item that I am looking at?
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 18054392
A better idea would be to grab the ID, and feed it into your Delete query, instead of using a JOIN


SQL = "DELETE * FROM [Equipment Inventory Main] WHERE InventoryItem=" & Me.YourInventoryItemControl
DoCmd.RunSQL SQL

0
 

Author Comment

by:MDauphinais1
ID: 18054471
That worked perfectly!  Thank you.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 18054812
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

840 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