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
JudithARyanTech SupportAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Eric ShermanConnect With a Mentor Accountant/DeveloperCommented:
Try this ...

DoCmd.RunSQL ("UPDATE sysUsers SET WS_ID = '" & sHostName & "'")

You may have the field name mispelled.  I changed WS-ID to WS_ID.


ET
0
 
Shaun KlineLead Software EngineerCommented:
What error are you getting?
0
 
Eric ShermanAccountant/DeveloperCommented:
Your SQL looks OK ... Something else is wrong.  Tell us exactly what error are you getting.

ET
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
NorieVBA ExpertCommented:
Judith

Where is sHostName declared and where is it given a value?
0
 
PatHartmanCommented:
The dash is an invalid character in a column name.  If you use it, you must enclose the column name in square brackets.
0
 
Eric ShermanAccountant/DeveloperCommented:
Yep, kind of what I was leading up to ... You should never use a dash in field names as Pat mentioned.

ET
0
 
Rey Obrero (Capricorn1)Commented:
you can use a dash but you need to wrap your field name in brackets

DoCmd.RunSQL ("UPDATE sysUsers SET [WS-ID] = '" & sHostName & "'")
0
 
PatHartmanCommented:
Didn't I just say that?
0
 
JudithARyanTech SupportAuthor Commented:
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
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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 ;-)
0
 
PatHartmanCommented:
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.
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.

All Courses

From novice to tech pro — start learning today.