Solved

VBstring "People's Verdict" rejected with Syntex err message

Posted on 1998-08-06
10
207 Views
Last Modified: 2010-04-30
I am sending an string like "People's Verdict" from a VB application to MSSQLServer for an update using DAO. It gives syntex error because of apostrophe i.e. '. (If the input is like "Peoples Verdict" there is no error.)
Can any body suggest how to handle this.
Thanks
0
Comment
Question by:devesh032698
  • 4
  • 3
  • 2
  • +1
10 Comments
 

Author Comment

by:devesh032698
ID: 1468205
Edited text of question
0
 
LVL 2

Accepted Solution

by:
percosolator earned 100 total points
ID: 1468206
Here is a function that I wrote to handle such situations for MS-SQL Server.

It handles both single and double quotes.

Ex:

Brian O'neil             becomes    Brian O' + CHAR(39) + 'neil

6" Screwdriver           becomes    6' + CHAR(34) + ' Screwdriver

-----------------------
this is a very simple piece of code that is designed for fixing quotation problems within variables.

The optional boolean variable defaults to true, and signifies that the string is to be parsed and contained within single quotation marks.
-----------------------

Option Explicit

Public Function FixQuotes(ByVal pstrSource As String, Optional pblnEnquote As Boolean = True) As String

    If (Len(pstrSource) = 0) Then Exit Function
   
    'replace all single quotation marks with a CHR(255)
    'because we need to have apostrophes inside to delineate
    'where the original string stops and starts on either
    'side of the " + CHAR(39) + ". Otherwise you will get
    'an infinite loop
    While (InStr(pstrSource, "'") > 0)

        pstrSource = Left(pstrSource, InStr(pstrSource, "'") - 1) + Chr(255) + " + CHAR(39) + " + Chr(255) + Mid(pstrSource, InStr(pstrSource, "'") + 1, Len(pstrSource))

    Wend

    'now we have only valid apostrophes inside, replace all the
    'Chr(255)'s with apostrophes
    While (InStr(pstrSource, Chr(255)) > 0)

        pstrSource = Left(pstrSource, InStr(pstrSource, Chr(255)) - 1) + "'" + Mid(pstrSource, InStr(pstrSource, Chr(255)) + 1, Len(pstrSource))

    Wend

    'look through the string and replace all quotation marks
    'with their SQL equivalent
    While (InStr(pstrSource, """") > 0)
   
        pstrSource = Left(pstrSource, InStr(pstrSource, """") - 1) + "' + CHAR(34) + '" + Mid(pstrSource, InStr(pstrSource, """") + 1, Len(pstrSource))
   
    Wend
   
    If (pblnEnquote) Then pstrSource = Enquote(pstrSource)
   
    FixQuotes = pstrSource
   
End Function

Public Function Enquote(pstrSource As String) As String

    Enquote = "'" & pstrSource & "'"
   
End Function

0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1468207
devesh,
  If you double the single quotes within your string then they will be interpreted as single.  You can drop the following subroutine into a module and execute it from anywhere within your program by passing the string where this condition can occur to this routine.


Sub Fix_Quotes(ByRef TextIn As String)
' replaces single quotes (') with double ('') so RDO engine can interpret them as single
Dim pos As Long
Dim pos2 As Long
Dim length As Long
pos2 = 1
Do
  pos = InStr(pos2, TextIn, "'", vbBinaryCompare)
  If pos > 0 Then
    length = Len(TextIn)
    TextIn = Mid$(TextIn, 1, pos - 1) & "''" & _
      Mid$(TextIn, pos + 1, length - (pos))
    pos2 = pos + 2 ' reset starting point for instr search after apostrophes
  End If
Loop Until pos = 0
End Sub

I use this as a generic routine over TEXT, CHAR & VARCHAR data. It works very well.  Percosolator answered the question while I was typing. Let me know if you decide you want my answere so I can answer after possible rejection of p's answer.  trkcorp@ixlmemphis.com

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:Razdan
ID: 1468208
Define a variable
Var = "People" & "'" & "s verdict"
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1468209
dim Var as String
Var = "People" & "'" & "s verdict"
Fix_Quotes Var

The result is "People''s verdict".  When this string is sent to the SQL Server it is stored as "People's verdict".
0
 

Author Comment

by:devesh032698
ID: 1468210
I am using VB50

For percosolator :
-----------------
I pass value to pstrsource as "people's verdict"
when I reach the following part of the code, it get's into infinite loop i.e. it does not change value of pstrsource , however the right part of the statement i.e Left(pstrSource... does show the desired value

While (InStr(pstrSource, "'") > 0)
       
        pstrSource = Left(pstrSource, InStr(......

Wend


For trkcorp :
-----------

The behavior is same as above.

Further suggestions please!

0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1468211
I do not understand.  The behavior is the same as above? After returning from Fix_Quotes the value of Var will be "People''s verdict".  There is no way to get caught in my loop...
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1468212
hmmm.

just copied the code off of the web page and ran some tests, works.  maybe I am doing something different.

here's what I tested in the immediate pane:
-----------------------------------------
?fixquotes("people" & chr(39) & "s court")
'people' + CHAR(39) + 's court'   <- result


A = "people" & chr(39) & "s court"
?a
people's court                    <- result

?fixquotes(A)
'people' + CHAR(39) + 's court'   <- result



b = "people's Court
?b
people's Court

?fixquotes(b)
'people' + CHAR(39) + 's Court'

------------------------------------------

what am I doing differently to test?  It seems to work with no infinate loop.
0
 

Author Comment

by:devesh032698
ID: 1468213
Both the solutions work, however just cut and paste does not work
Anyway thanks to you all.

I am sorry trkcorp, The points can not be shared as per the rules of this site, however I am sure you deserve 50% of the points for equally good answer. My sincere regards for your help.
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1468214
That's OK devesh. Thanks.  I hadn't taken double quotes into account and percosolator made me aware that I should.  That is reward enough.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
Spell Check in VB6 13 127
VBA to find and replace multiline text from VBA modules 8 64
VBA: loop recent folder and copy txt file. 8 34
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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