• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1018
  • Last Modified:

CurrentDb.Execute update syntax problem

I am having a problem with vba code to update a table.

I have two tables     tblMatrix and tblMatirxTemp

When I fire the code, I want to update tblMatrix.Hours with the value tblMatrixTemp.Hours1  when the Employee, Customer and Project from both tables match and when tblMatrix.WeekEndingDate = tblMatrixTemp.WED1 match.


Using an update query, I can get the desired results.

UPDATE tblMatrix INNER JOIN tblMatrixTemp ON (tblMatrix.WeekEndingDate = tblMatrixTemp.WED1) AND (tblMatrix.HourType = tblMatrixTemp.HourType) AND (tblMatrix.Project = tblMatrixTemp.Project) AND (tblMatrix.Customer = tblMatrixTemp.Customer) AND (tblMatrix.Employee = tblMatrixTemp.Employee) SET tblMatrix.Hours = [tblMatrixTemp].[Hours1];

I do have a test to confirm that the record I want to update exists in the table.

I attached my vba code below
CurrentDb.Execute "UPDATE tblMatrix SET tblMatrix.Hour = tblMatrixTemp.Hours1 WHERE (tblMatrix.WeekEndingDate = tblMatrixTemp.WED1) AND (tblMatrix.HourType = tblMatrixTemp.HourType) AND (tblMatrix.Project = tblMatrixTemp.Project) AND (tblMatrix.Customer = tblMatrixTemp.Customer) AND (tblMatrix.Employee = tblMatrixTemp.Employee)"

Open in new window

0
Scamquist
Asked:
Scamquist
1 Solution
 
jrdecarliCommented:
You SQL looks fine. I susepct that the issue is with differering field types between the two tables.
0
 
Patrick MatthewsCommented:
Your VBA is not using the right Jet SQL syntax.



CurrentDb.Execute "UPDATE tblMatrix INNER JOIN tblMatrixTemp ON " & _
    "(tblMatrix.WeekEndingDate = tblMatrixTemp.WED1) AND " & _
    "(tblMatrix.HourType = tblMatrixTemp.HourType) AND " & _
    "(tblMatrix.Project = tblMatrixTemp.Project) AND " & _
    "(tblMatrix.Customer = tblMatrixTemp.Customer) AND " & _
    "(tblMatrix.Employee = tblMatrixTemp.Employee) " & _
    "SET tblMatrix.Hours = [tblMatrixTemp].[Hours1];"

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
use this formatting so you can easily the whole sql statement, using
debug.print  sql


Dim sql
sql = "UPDATE tblMatrix SET tblMatrix.Hour = tblMatrixTemp.Hours1"
sql = sql & " WHERE (tblMatrix.WeekEndingDate = tblMatrixTemp.WED1)"
sql = sql & " AND (tblMatrix.HourType = tblMatrixTemp.HourType)"
sql = sql & " AND (tblMatrix.Project = tblMatrixTemp.Project)"
sql = sql & " AND (tblMatrix.Customer = tblMatrixTemp.Customer)"
sql = sql & " AND (tblMatrix.Employee = tblMatrixTemp.Employee)"

debug.print sql

currentdb.execute sql

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ScamquistAuthor Commented:
Capricorn1.
I get an error:

Run-time error '3061':
Too few parameters.  Expected 7.

and it hangs on
CurrentDb.Execute sql
0
 
Rey Obrero (Capricorn1)Commented:
check for the names of fields
0
 
Rey Obrero (Capricorn1)Commented:
hour is a reserved word, place it in []
Dim sql
sql = "UPDATE tblMatrix SET tblMatrix.[Hour] = tblMatrixTemp.Hours1"
sql = sql & " WHERE (tblMatrix.WeekEndingDate = tblMatrixTemp.WED1)"
sql = sql & " AND (tblMatrix.HourType = tblMatrixTemp.HourType)"
sql = sql & " AND (tblMatrix.Project = tblMatrixTemp.Project)"
sql = sql & " AND (tblMatrix.Customer = tblMatrixTemp.Customer)"
sql = sql & " AND (tblMatrix.Employee = tblMatrixTemp.Employee)"

debug.print sql

currentdb.execute sql

Open in new window

0
 
ScamquistAuthor Commented:
Capricorn1.
My mistake.  the field is Hours
I verified the spelling of all the field names.
Now the error is
Run-time error '3061':
Too few parameters.  Expected 6
and still hangs on CurrentDb.Execute sql.


I did try matthewspatrick solution and it works.  I would just like to know why this is failing.  I really looks like it should work, but I don't want to wast you time.  Thanks.

0
 
ScamquistAuthor Commented:
Thank you for the assist.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now