Posible update query syntax error

R

richietj77

Ok this is the situation.

I have these tables

tblEmployee
tblEmpDeductions (Employee Deductions)

with field EmployeeID as primary Key,

and these tables

tblPayroll
tblPayrollFile

with field PayrollID as primary Key.


On tblEmpDeductions, I have these fields:

EmployeeID - EmployeeDedID - EmployeeDedAmount
------------------------------------------------------
123-45-6789 - FICA - 123.45
123-45-6789 - Medicare - 67.89


On tblPayrollFile, I have these fields:

PayrollID - EmployeeID - GrossPay - TotalDeduc
------------------------------------------------------
2006-00 - 123-45-6789 - 500.00 - __________



I want to add the total of each Employee Deductions from
tblEmpDeductions to the field TotalDeduc in tblPayrollFile.

I've already tried with an update query but I received an error, I
think syntax is wrong:

DoCmd.RunSQL "UPDATE tblPayrollFile INNER JOIN tblEmpDeductions ON
(tblPayrollFile.EmployeeID = tblEmpDeductions.EmployeeID) SET
tblPayrollFile.TotalDeduc =
Sum([tblEmpDeductions].[EmployeeDedAmount])"

Please Help! Thanks.
 
J

John Vinson

I want to add the total of each Employee Deductions from
tblEmpDeductions to the field TotalDeduc in tblPayrollFile.

In general, you should NOT store the TotalDeduc field.

Storing derived data such as this in your table accomplishes
three things: it wastes disk space; it wastes time (almost
any calculation will be MUCH faster than a disk fetch); and
most importantly, it risks data corruption. If one of the
underlying fields is subsequently edited, you will have data
in your table WHICH IS WRONG, and no automatic way to detect
that fact.

Just redo the calculation whenever you need it, either as a
calculated field in a Query or just as you're now doing it -
in the control source of a Form or a Report textbox.
I've already tried with an update query but I received an error, I
think syntax is wrong:

DoCmd.RunSQL "UPDATE tblPayrollFile INNER JOIN tblEmpDeductions ON
(tblPayrollFile.EmployeeID = tblEmpDeductions.EmployeeID) SET
tblPayrollFile.TotalDeduc =
Sum([tblEmpDeductions].[EmployeeDedAmount])"

The problem is that no query involving a SUM or GroupBy is ever
updateable - it's a JET query restriction.

Try using the DSum() function instead, if you think you want to do
this:

DoCmd.RunSQL "UPDATE tblPayrollFile SET
tblPayrollFile.TotalDeduc =
DSum(""[EmployeeDedAmount]"", ""tblEmpDeductions"", ""[EmployeeID] =
"" & tblPayrollFile.EmployeeID )"

Note that even if your expression had worked, it would have summed
every deduction in the entire tblEmpDeductions and assigned that grand
sum to each and every employee, since you weren't grouping by
employeeID!

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top