Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL syntax error in VBA

Posted on 2016-10-21
11
Medium Priority
?
58 Views
Last Modified: 2016-10-21
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
0
Comment
Question by:JudithARyan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 27

Expert Comment

by:Shaun Kline
ID: 41854187
What error are you getting?
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 41854201
Your SQL looks OK ... Something else is wrong.  Tell us exactly what error are you getting.

ET
0
 
LVL 35

Expert Comment

by:Norie
ID: 41854207
Judith

Where is sHostName declared and where is it given a value?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 19

Accepted Solution

by:
Eric Sherman earned 2000 total points
ID: 41854208
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
 
LVL 39

Expert Comment

by:PatHartman
ID: 41854221
The dash is an invalid character in a column name.  If you use it, you must enclose the column name in square brackets.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 41854223
Yep, kind of what I was leading up to ... You should never use a dash in field names as Pat mentioned.

ET
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 41854287
you can use a dash but you need to wrap your field name in brackets

DoCmd.RunSQL ("UPDATE sysUsers SET [WS-ID] = '" & sHostName & "'")
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 41854292
Didn't I just say that?
0
 

Author Closing Comment

by:JudithARyan
ID: 41854297
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 41854328
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
 
LVL 39

Expert Comment

by:PatHartman
ID: 41854350
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question