Link to home
Start Free TrialLog in
Avatar of JudithARyan
JudithARyanFlag for United States of America

asked on

SQL syntax error in VBA

I'm having trouble with a SQL update statement, getting a syntax error.  When I go thru other code in my application, I see similar statements that get no error.  WS-ID is defined as SHORT TEXT in the sysUsers table.

sHostName  is DESK_FOUR


DoCmd.RunSQL ("UPDATE sysUsers SET WS-ID = '" & sHostName & "'")

what am I doing wrong?

Judith
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

What error are you getting?
Your SQL looks OK ... Something else is wrong.  Tell us exactly what error are you getting.

ET
Avatar of Norie
Norie

Judith

Where is sHostName declared and where is it given a value?
ASKER CERTIFIED SOLUTION
Avatar of Eric Sherman
Eric Sherman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The dash is an invalid character in a column name.  If you use it, you must enclose the column name in square brackets.
Yep, kind of what I was leading up to ... You should never use a dash in field names as Pat mentioned.

ET
you can use a dash but you need to wrap your field name in brackets

DoCmd.RunSQL ("UPDATE sysUsers SET [WS-ID] = '" & sHostName & "'")
Didn't I just say that?
Avatar of JudithARyan

ASKER

You're a mind reader.  I changed the table field to WSID and the SQL statement to match.  It worked fine.  Apparently the syntax didn't like the dash/hyphen in a field name.  Is this something new?  I'm converting my application from Access 2003 to Access 2013.

Thanks for your help.

Judith
A dash should never have been allowed in a name. Access is just more strict about this now.

If you have   a-b    in a SQL, it means    a minus b    for sure ;-)
Access allows you to use certain punctuation characters and the space in column names even though technically no programming language would allow the characters due to reasons such as what Qlemo quoted.  However, Access has ALWAYS forced the square brackets syntax for these offending names so converting from 2003 to 2013 isn't what caused the problem.  Newer versions of Access warn you against using certain property and function names as column names but still allow them.  I don't think you get warnings for bad characters except the ones that are simply not allowed such as ! and . and `[ and ]

It is always best to avoid ALL punctuation characters including the space and to also avoid using property names and function names as those will either cause runtime errors or perhaps result in referencing the built in object rather than your user-defined object.