pass global variable to Update query

access 2010
Trying to pass a global variable to an update query ?

This keeps prompting me a parameter query ?

Public Function ReturnVariable()
  ReturnVariable = str
End Function




Dim strSQL As String
str = Me.List4.Value



'build the SQL string
strSQL = "UPDATE"
strSQL = strSQL & " tbl_History_Import_Flag "
strSQL = strSQL & "SET "
strSQL = strSQL & "tbl_History_Import_Flag.Field2 = "
strSQL = strSQL &     ReturnVariable  '    also tried just   str
strSQL = strSQL & ";"


With DoCmd
.RunSQL strSQL
End With

Open in new window



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.

James ElliottManaging DirectorCommented:
Public Function ReturnVariable()
  ReturnVariable = str
End Function




Dim strSQL As String
str = Me.List4.Value



'build the SQL string
strSQL = "UPDATE"
strSQL = strSQL & " tbl_History_Import_Flag "
strSQL = strSQL & "SET "
strSQL = strSQL & "tbl_History_Import_Flag.Field2 = "
strSQL = strSQL &    "'" & ReturnVariable & "'"  
strSQL = strSQL & ";"


With DoCmd
.RunSQL strSQL

Open in new window

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
mbizupCommented:
Try enclosing it in quotes (assuming Field2 is a TEXT field)

strSQL = "UPDATE"
strSQL = strSQL & " tbl_History_Import_Flag "
strSQL = strSQL & "SET "
strSQL = strSQL & "tbl_History_Import_Flag.Field2 = '"
strSQL = strSQL &     ReturnVariable & "'"  '    also tried just   str
strSQL = strSQL & ";"

Open in new window

0
FordraidersAuthor Commented:
Thanks Guys !!!
0
Dale FyeCommented:
you might want to consider using Tempvars instead of globally defined variables.  The big advantages of a tempvar are:

1.  it retains its value even after encountering an unhandled error, which will reset regular global variables.
2.  you can set it in the immediate window, so you can test your query without having the rest of the application running.
3.  you can refer to them as:

Tempvars!yourTempVar
Tempvars("YourTempVar")
[Tempvars]![yourTempvar]
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"you might want to consider using Tempvars "
Double ditto on that ... a much more elegant solution,

mx
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.