473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Parse Single and Double Quotes from SQL String

124 100+
I have set up a form that filters based on the selection in a dropdown box. If the filter is [Subject] = 'subject', that works properly. However, if the filter sql string is [Subject]='isn't subject', that generates an error and the form will not filter. I tried the following without success:
Expand|Select|Wrap|Line Numbers
  1. Public Function CheckForQuotes(someText) As Variant 
  2.  
  3.     If IsNull(someText) Then
  4.         CheckForQuotes = Space(1)
  5.         Exit Function
  6.     End If
  7.  
  8. Dim chrSingleQuote      As String: chrSingleQuote = "'"
  9. Dim chrDoubleQuote      As String: chrDoubleQuote = """"
  10. Dim start%, length%, remainder%, x%
  11. Dim strPart1, strRemainder  As String
  12. Dim strBase, strNew         As Variant
  13.  
  14.     start% = 1
  15.     strBase = someText
  16.     strNew = ""
  17.  
  18.     Do
  19.         x% = InStr(start%, strBase, chrSingleQuote, vbTextCompare)
  20.  
  21.         If x% <> 0 Then
  22.             strPart1 = Mid(strBase, start%, x% - start%)
  23.             strNew = strNew & strPart1
  24.             strNew = strNew & "''"
  25.             start% = x% + 1
  26.         End If
  27.     Loop Until x% = 0
  28.  
  29.     strNew = strNew & Mid(strBase, start%)
  30.  
  31.     CheckForQuotes = strNew
  32. End Function
  33.  
Any advice on this is much appreciated.
Apr 30 '13 #1
8 6338
Rabbit
12,516 Expert Mod 8TB
Why check for quotes? Why not replace them outright whether or not they're there?
Expand|Select|Wrap|Line Numbers
  1. Replace(value, "'", "''")
Apr 30 '13 #2
Seth Schrock
2,965 Expert 2GB
If all you are trying to do is remove double and single quotes from a string, then just use the replace function.
Expand|Select|Wrap|Line Numbers
  1. Dim myString as String
  2.  
  3. myString = "Isn't Filtered"
  4. myString = Replace(myString, "'", "")
  5. myString = Replace(myString, """", "")
  6.  
  7. Debug.Print myString
  8. 'myString now equals "Isnt Filtered"
Apr 30 '13 #3
BikeToWork
124 100+
Thanks for the response, Rabbit. Actually, I need the string with the single or double quote in it but "escaped". For example, if I am trying to filter the form on a field with [Subject] = 'This isn't a subject', I need to filter on the literal string with the single quote but the compiler doesn't know that and interprets the single quote as a string delimiter. So it needs to be like [Subject] = 'This isn''t a subject'. I hope I am making sense.
Apr 30 '13 #4
Rabbit
12,516 Expert Mod 8TB
What I posted will escape it
Apr 30 '13 #5
BikeToWork
124 100+
The problem with just using the replace function is that it will change [Subject]= 'doesn't matter' with [Subject]= ''doesn''t matter'', where the first and last single quote string delimiters are replaced with two single quotes. This will not compile. I need to retain the leading and ending string delimiter single quotes and just replace the single quote in "doesn't matter" with two single quotes so the whole string is [Subject] = 'doesn''t matter'. Thanks in advance for any help with this.
Apr 30 '13 #6
Rabbit
12,516 Expert Mod 8TB
Not if you run the replace on the value before you append and prepend the starting and ending quotes.
Apr 30 '13 #7
BikeToWork
124 100+
Rabbit, thank you for your help. At the risk of sounding ignorant, how would one go about removing the single quotes at the beginning and end of the string before replacing the single quote embedded in the string with ''? For example, how would you turn [Subject] = 'doesn't matter' into [Subject] = doesn't matter. From that point I can see how replace would work on the embedded single quote. I just need to massage the string to begin with so it does not have the beginning and ending single quotes. Thanks in advance.
Apr 30 '13 #8
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. strValue = "o'connor"
  2. strSQL = "SELECT * FROM tableName WHERE " & _
  3.    "fieldName = '" & Replace(strValue, "'", "''") & "'"
Apr 30 '13 #9

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

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
9
by: Dynamo | last post by:
Hi, I am still confused as when to use single or double quotes. This works: echo "<td>" . $row . "</td>"; and this does not
11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
5
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. ...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
4
by: (PeteCresswell) | last post by:
Is his just a flat-out "No-No" or is there some workaround when it comes time for SQL searches and DAO.FindFirsts against fields containing same? I can see maybe wrapping the value searched for...
6
by: G. | last post by:
This is an obvious bug in the String.Replace function: //load a XML string into a document XmlDocument doc = new XmlDocument(); doc.LoadXml("<test id='' />"); //Obtain the string...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.