473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ 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 7283

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

Similar topics

6
8940
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 flow of data is that the name is entered in via a form text box, stored in a MySQL table and then read back into another form or echoed out as needed.
2
8093
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 column for the combo box called 'StudentName', and is a concatenated field comprised of 'LastName','FirstName'. These two fields are both the same...
13
11366
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 Restaurant" etc. automatically and programmatically during data entry? This would eliminate my concatinated strings from producing errors when I...
11
4008
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, "") & "'" txtlngAddressTypeID.ControlSource = "='" & Nz(!strAddressType, "") & "'"
3
1841
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 failing with an "Incorrect syntax" message.
7
3753
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 clicked on that name from the displayed list in pop up window, it shows an error since delimitting that returning arguments by apostrophe. So if any one...
2
12313
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 obvious way, like this: DataTable dt = new DataTable(); DataColumn id = new DataColumn("ID", Type.GetType("System.Int32"));
4
2153
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 textboxes on a web form. and i encountered this error when i tried to save for example, sweet's ?? is there a solution for it ?
9
3590
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 (which itself should be delimited by “ and ”. (I gathered that from reading several English books.) I think you would agree that it would make...
0
7409
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7766
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5341
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.