Solved

MS Access SQL DELETE Query

Posted on 2006-12-01
6
529 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
[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
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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