473,468 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Apostrophe causes error in SQL string: Solution

sashi
1,754 Recognized Expert Top Contributor
Apostrophe

Have you ever tried to send a string variable to MS Access that had apostrophes embedded within an SQL Statement? If YES you will get a run time ERROR. Here is your solution, a function that formats the variable before sending it to the database.

Expand|Select|Wrap|Line Numbers
  1. Public Function Apostrophe(sFieldString As String) As String
  2.   If InStr(sFieldString, "'") Then
  3.     Dim iLen As Integer
  4.     Dim ii As Integer
  5.     Dim apostr As Integer
  6.     iLen = Len(sFieldString)
  7.     ii = 1
  8.  
  9.       Do While ii <= iLen
  10.         If Mid(sFieldString, ii, 1) = "'" Then
  11.           apostr = ii
  12.           sFieldString = Left(sFieldString, apostr) & "'" & _
  13.           Right(sFieldString, iLen - apostr)
  14.           iLen = Len(sFieldString)
  15.           ii = ii + 1
  16.         End If
  17.       ii = ii + 1
  18.       Loop
  19.   End If
  20.  
  21.   Apostrophe = sFieldString
  22. End Function
  23.  
Dec 4 '06 #1
0 7275

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

Similar topics

6
by: S. Rhodes | last post by:
Hello everyone. I am TOTALLY baffled. PLEASE, I need some help here. In the event that one must deal with a name including an apostrophe...I am looking for an appropriate solution. The...
2
by: CSDunn | last post by:
Hello, I have a combo box designed to look up records in a subform based on the selection made in the combo box. The Record Source for the combo box is a SQL Server 2000 View. There is one bound...
13
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices...
11
by: Trevor | last post by:
Hi, I currently have the following code in a subform. If Not rstPDAddresses.EOF And Not rstPDAddresses.BOF Then With rstPDAddresses txtautoAddressID.ControlSource = "='" & Nz(!autoAddressID,...
3
by: spacehopper_man | last post by:
hi - I am "apostrophe in sql" problems ;) I am executing a stored procedure on SQL Server - and passing in a string parameter. the string has a single apostrophe in it. the call is...
7
by: abilashnn | last post by:
Dear All, In one of my appln, I have one pop up window which will select one name from the list and return to calling jsp page. Now one name eg: Kevin O' Connor , having apostrophe. So if we...
2
by: Tom | last post by:
Hi, I have some kind of problems with an apostrophe character ('). I would like to select from DataTable DataRow containing value horses' (with an apostrophe on the end). But when I do it in an...
4
OuTCasT
by: OuTCasT | last post by:
Can anyone tell me why an word with an apostrophe cannot be saved to the db? i get this error Incorrect syntax near 's'. Unclosed quotation mark after the character string ')'. got some...
9
by: Thomas 'PointedEars' Lahn | last post by:
Jukka K. Korpela wrote: IBTD. For example, in English it is customary (and AIUI expected) to use the character that ’ represents should be used to delimit a quotation within direct speech...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.