473,505 Members | 13,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I stop an apostrophe causing a syntax error?

39 New Member
Hi everyone,


I am using a simple lookup function to set the row source of a combo box based on the value set in another combo box.

This is working perfectly but I am getting a syntax error:

syntax error (missing operator) in query expression '[organisations].[governing body] = 'N'Land

This error only occurs when the selected value in box one contains an apostrophe!
I assume this is because the apostrophe effects the way VBA sees the string so it changes the search criteria?

This is the code being used:
Expand|Select|Wrap|Line Numbers
  1. varvalue = Me.Combo29.Column(1) 
  2.  
  3. row = "SELECT  [name]  FROM organisations WHERE [organisations].[governing body] = '" & varvalue & "'" 
  4.  
  5. Name1.RowSource = row 
  6.  
Obviously the simple answer would be to go through and remove any apostrophes but this is not really the way I would like to resolve this if there is a better way!?

Thanks in advance

Carl
edit reply report
Aug 10 '10 #1
5 8939
jimatqsi
1,271 Recognized Expert Top Contributor
You might try changing your code to this:

Expand|Select|Wrap|Line Numbers
  1. row = "SELECT  [name]  FROM organisations WHERE [organisations].[governing body] = """ & varvalue & """
It changes your text delimiter to a double-quote. Of course, if there are any double-quotes in the data, the same thing will happen. There have to be some rules, if you let the users do whatever they want, this sort of thing will happen.

Jim
Aug 11 '10 #2
copleyuk
39 New Member
Spot on thanks Jim!

Unfortunately I inherited the database with the 's already in.

I will make sure going forward the "'s are definately not permitted!!!

Thanks for your help you're a star
Aug 11 '10 #3
OldBirdman
675 Contributor
You don't have to give up either quote, single(') or double("), for this. Substitute Replace(varvalue, """", """""") for varvalue in
Expand|Select|Wrap|Line Numbers
  1. row = "SELECT  [name]  FROM organisations WHERE [organisations].[governing body] = """ & varvalue & """
giving you
Expand|Select|Wrap|Line Numbers
  1. row = "SELECT  [name]  FROM organisations WHERE [organisations].[governing body] = """ & Replace(varvalue, """", """""") & """
This way your organisation can continue to use either quote, as appropriate.
Aug 11 '10 #4
NeoPa
32,557 Recognized Expert Moderator MVP
While OB is right about using Replace() (or other bespoke function) to handle quotes of either type in the data, I would point out that as far as standard SQL is concerned, the Single-Quote (') is the only correct one to use (See Quotes (') and Double-Quotes (") - Where and When to use them).

There is an article by Frinavale that covers the use of this function and why it is a good idea to use it as standard (SQL Injection Attack).
Aug 11 '10 #5
OldBirdman
675 Contributor
The 'name' field of the table contains both single and double quotes. Assume for one record the name is T. J. "Bud" O'Reilly. I saw no reason this couldn't be allowed in the future. copleyuk's statement that:
Expand|Select|Wrap|Line Numbers
  1. I will make sure going forward the "'s are definately not permitted!!!
sort of changes reality, and it was to this sentence I was commenting.
Of course NeoPa is correct - he almost always is. So I would propose:
Expand|Select|Wrap|Line Numbers
  1. row = "SELECT  [name]  " & _
  2.       "FROM organisations " & _
  3.       "WHERE [organisations].[governing body] = '" & _
  4.       Replace(varvalue, "'", "''") & "'"
Aug 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
1
1916
by: Ronny Sigo | last post by:
Hello all, I have made a form containing a combox which must look up values (names) in a table. It works fine until the moment I try to put in a name containg an apostrophe. (e.g. d'Haen). At this...
5
1996
by: Berend | last post by:
when I try to write to a database I get a syntax error I made the code a simple as possible bur also the VS generated code gives the same error. WHY? private void button1_Click(object sender,...
4
3389
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am just starting out create a custom control following the KB example "Create a Data Bound ListView Control". I am receiving a syntax error on the...
4
1532
Tog
by: Tog | last post by:
Hello, I have read several posts with this heading but none of them have helped. I have the following error message: Syntax error (missing operator) in query expression 'left(Your Product...
1
2626
by: jomurf | last post by:
Not sure if this is the right forum. I'm using Microsoft Query to get data from our ERP. However, every time I try to sum a column, I get a syntax error. Here's the sql statement: SELECT...
5
5476
by: Chris Kennedy | last post by:
Hi all I'm running SQL Server 2005 Express Edition. One database One table called Sites Fields as follows: id (bigint, Identity, Primary Key) SiteName (varchar(50), allows nulls) Generation...
6
37999
by: muby | last post by:
Hi everybody :) I'm modifying a C++ code in VC++ 2005 my code snippet void BandwidthAllocationScheduler::insert( Message* msg, BOOL* QueueIsFull,
7
18651
by: HSXWillH | last post by:
I have a field in a database that contains last names. In some of those names, like O'Brien and O'Connor, there is a ' symbol. I am using combo boxes on a form to build a form filter and in...
1
2714
by: inbarik | last post by:
Hi, i'm getting syntax error message for this button that tries to get name (of building) with apostrophe. Any idea how to solve this? Note that i must use name with apostrophe e.g. O'neal: ...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.