Solved

MS Access SQL DELETE Query

Posted on 2006-12-01
6
526 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

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.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

756 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