trying to run pass through query in vba code..synyax error

2 tables:
 "tbl_Import_history"      local access table
 dbo_t_dispensing_metrics_detail_schedules     -- sql server linked table

 running an update pass through query on sql table, for a specific field based on a selection of field names(field in sql table) in a list4 box.

 is the following code correct ?


' no we update the sql server table
' DO THE UPDATE ON SQL TABLE ' PASS THROUGH QUERY
strSQL = ""
Dim strS As String
Dim strField As String

strField = [Forms]![frm_Choice].[List4].Value

strS = "Update dbo_t_dispensing_metrics_detail_schedules as Res " & _
"Inner join tbl_Import_Flag as rand " & _
"on Res.Account_Number = rand.Field1 " & _
"set Res." & strField & " = rand.Field2"

'Debug.Print strS
'turn warnings off, rune the query, and turn warnings back on
CurrentDb.Execute strS

MsgBox "Updates Completed", vbDefaultButton1

Open in new window


Still getting syntax errors. ?

Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
You can't mix an SQL table (with no dbo_ prefix) with a local table in a PT query:

t_dispensing_metrics_detail_schedules as Res Inner join tbl_Import_Flag

/gustav
0
FordraidersAuthor Commented:
gustav
Thanks but still do not work.
strS = "Update t_dispensing_metrics_detail_schedules as Res " & _
"Inner join tbl_Import_Flag as rand " & _
"on Res.Account_Number = rand.Field1 " & _
"set Res." & strField & " = rand.Field2"
0
Dale FyeCommented:
The way you have this written, it is not a pass-through query.

In order to create a pass-through, you are going to have to actually create a querydef.  You can do that at runtime with CreateQuerydef if you must, but I generally have two pass-through queries , one which returns records, one which doesn't already defined, with connection strings.  Then, you would simply use:

currentdb.querydefs("SQL_Passthru_Doesnt_return_Records").SQL = strS
currentdb.querydefs("SQL_Passthru_Doesnt_return_Records").execute  dbfailonerror

But as Gustav indicated, you cannot mix an Access local table with a SQL Server table in an actual pass-through.  You might want to try wrapping your strField variable in brackets.  

strS = "Update dbo_t_dispensing_metrics_detail_schedules as Res " _
        & "Inner join tbl_Import_Flag as rand on Res.Account_Number = rand.Field1 " _
        & "set Res.[" & strField & "] = rand.Field2"

I have to say, I'm not sure why you would want to write this type of update query at run-time without checking to make sure the two fields are of the same time, and that the field defined by strField actually exists.

Have you tried:

debug.print strS

and then copying that to a new query?  Usually, when you do this, you will get an error message and it will highlight the section of the SQL that is causing the error.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FordraidersAuthor Commented:
Thanks, This solution worked great. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

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.