VB Code To Delete Records Using Multiple Relationships

foxhound

Board Regular
Joined
Mar 21, 2003
Messages
182
Code:
Okay, I have 4 tables that I have to keep separate.  Employees, PayHistory, Codes and AwardDates.

Employees has 2 fields: EmpNo, Group
PayHistory has 4 fields: EmpNo, DBA, Date, Hours
AwardDates has 4 fields: Group, Type, Current, CalcDate
Codes has 3 fields: DBA, Sick, Vac

Each table has various relationships:  
   Employees.EmpNo = PayHistory.EmpNo
	PayHistory.DBA = Codes.DBA
	AwardDates.Group = Employees.Group

					
I need to delete all records from PayHistory where Date > CalcDate when Current = true and Type = “Vacation” and Vac = “Y”

Can anyone tell me the VB code for this?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I don't think you need VBA. Check out the built-in Access help on "Create a delete query".
 
Upvote 0
As a tip, you don't need VBA but it can be done, but you have to know how to write the SQL to do it efficiently.

Read up on jmiskey's suggestion, setup a query to do it. Then look at the SQL View of the query and copy it into Word.

You can then setup a vba routine that does something like:

Code:
Dim strSQL As String

strSQL = "put_the_query here"
strSQL = strSQL & "add_more_of_the_query_here_as_needed"

DoCmd.RunSQL strSQL

But to emphasize, this is unnecessary if you're just doing things interactively. Assuming you'll have a long query, make sure you watch the spacing between lines. Also, convert all " to ' within the query that you're copying.

Mike
 
Upvote 0
Thanks for the suggestions guys. Although, I am still having difficulties. Here is my code:

Code:
DELETE [PayHistory].*, [PayHistory].EmpNo, [PayHistory].Date, [PayHistory].DBA, [PayHistory].Hours, [AwardDates].CalcDate, [AwardDates].Current, [AwardDates].Type, [Codes].Vac
FROM (Employees INNER JOIN ([PayHistory] INNER JOIN [Codes] ON [PayHistory].DBA = [Codes].DBA) ON Employees.EmpNo = [PayHistory].EmpNo) INNER JOIN [AwardDates] ON Employees.Group = [AwardDates].Group
WHERE ((([PayHistory].Date)>[calcdate]) AND (([AwardDates].Current)=True) AND (([AwardDates].Type)="vacation") AND (([Codes].Vac)="Y"));

I created a query that pulls the correct records that I want pulled, changed it to a delete query, and pulled the * to the bottom for the table that I need to delete the records from. But I get an error stating that I can't delete records from that table. Is it because of the joins? Please advise :rolleyes:
 
Upvote 0
I got it. All I had to do was change "delete" to "delete distinctrow." I may need some further help later on another part of this. So, please keep me in mind :biggrin:
 
Upvote 0

Forum statistics

Threads
1,221,618
Messages
6,160,853
Members
451,674
Latest member
TJPsmt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top