Use of variables in SQL (1 Viewer)

A. Turtle

Lost and Confused
Local time
Today, 18:25
Joined
Aug 20, 2007
Messages
17
Will someone please help with a definitive answer as to how to indicate that that a word in an Update query is a variable, and that it is the content of that variable that is to be written to the required field?

I have a variable, strThisUser (the current user's name) - that I want to store in a field called Created_By in a table called tblOrg_Name. The variable exists, and holds the required value as proved using Debug.Print strThisUser. When the user name is stored I also want to store the current date and time in the field Created_On.

I tried to do this using:

DoCmd.RunSQL "UPDATE tblOrg_Name " & _
"SET " & _
"tblOrg_Name.Created_On = Now(), " & _
"tblOrg_Name.Created_By = strThisUser;"

and got a syntax error in update statement.

I removed the line with Now(), and still got the error. I replaced the line with Now() and removed the line with strThisUser, and it worked OK, apart from not storing the user name of course. The error is in the way I'm using the variable, it seems

Reading through various posts on this forum, and others, produced a bewildering array of things, and combinations of things, in which to enclose the variable name. These include parentheses, quotes single and double, square brackets, ampersands, chr(xx), plus a few others which I don't remember right now. I have tried all of them, and nothing works.

As I understand that different versions of Windows, and different versions of Access, can cause things to behave other than expected and these may need to be known, Access 2000 running under Windows 2003 server.

First aid for the skull damage caused by banging my head against the wall over this one would also be appreciated :mad: Not angry - bleeding!!
 
Last edited:

KenHigg

Registered User
Local time
Today, 13:25
Joined
Jun 9, 2004
Messages
13,327
DoCmd.RunSQL is simply looking for a string so you just need to concatenate correctly:

DoCmd.RunSQL "UPDATE tblOrg_Name " & _
"SET " & _
"tblOrg_Name.Created_On = #" & Now() & "#, " & _
"tblOrg_Name.Created_By = '" & strThisUser & ";"

I think this is part of the problem...

:)
ken
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 18:25
Joined
Jul 9, 2003
Messages
16,360
What about changing this :

"tblOrg_Name.Created_By strThisUser;"
to this:
"tblOrg_Name.Created_By = strThisUser;"
 

A. Turtle

Lost and Confused
Local time
Today, 18:25
Joined
Aug 20, 2007
Messages
17
To KenHigg

Many many thanks. That was one combination that I apparently hadn't tried during my perm(x) from y exercise. And - IT WORKS!! <VERY HAPPY GRIN>

To Uncle Gizmo

Ooops, it's that kind of a day. That was a typo, corrected later, but before I had updated my browser and seen your replies.

To both of you - thanks for the incredibly quick responses.
 

A. Turtle

Lost and Confused
Local time
Today, 18:25
Joined
Aug 20, 2007
Messages
17
No problem. Send money.

:p:p

ken

Apologies Ken. It really is a bad day. I seem to have mislaid not only your address for the cheque, but your bank account details for credit transfer as well. ;)
 

KenHigg

Registered User
Local time
Today, 13:25
Joined
Jun 9, 2004
Messages
13,327
Carefull, you'll date yerself - :)
 

A. Turtle

Lost and Confused
Local time
Today, 18:25
Joined
Aug 20, 2007
Messages
17
The account number is br549... :D
That was a high one - I'm 6' 2" and it went right over my head!!

Maybe something to do with me living on the wrong side of the pond. I don't think that one has ever been shown over here.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 10:25
Joined
Aug 30, 2003
Messages
36,133
To address the conceptual question, you basically want to include literal portions of the SQL inside the quotes, and things that have to be interpreted for their values (variables, form references) outside the quotes. Then you have to remember the bit about surrounding text values with single quotes and dates with #.
 

A. Turtle

Lost and Confused
Local time
Today, 18:25
Joined
Aug 20, 2007
Messages
17
To address the conceptual question, you basically want to include literal portions of the SQL inside the quotes, and things that have to be interpreted for their values (variables, form references) outside the quotes. Then you have to remember the bit about surrounding text values with single quotes and dates with #.
Thanks for the explanation but, if I can be forgiven for saying so, it does seem like complexity for complexity's sake. Other RDBMS's seem to manage with less convoluted methods - iirc.
 

Users who are viewing this thread

Top Bottom