Solved

MS Access SQL DELETE Query

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

23 Experts available now in Live!

Get 1:1 Help Now