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: -
Public Function CheckForQuotes(someText) As Variant
-
-
If IsNull(someText) Then
-
CheckForQuotes = Space(1)
-
Exit Function
-
End If
-
-
Dim chrSingleQuote As String: chrSingleQuote = "'"
-
Dim chrDoubleQuote As String: chrDoubleQuote = """"
-
Dim start%, length%, remainder%, x%
-
Dim strPart1, strRemainder As String
-
Dim strBase, strNew As Variant
-
-
start% = 1
-
strBase = someText
-
strNew = ""
-
-
Do
-
x% = InStr(start%, strBase, chrSingleQuote, vbTextCompare)
-
-
If x% <> 0 Then
-
strPart1 = Mid(strBase, start%, x% - start%)
-
strNew = strNew & strPart1
-
strNew = strNew & "''"
-
start% = x% + 1
-
End If
-
Loop Until x% = 0
-
-
strNew = strNew & Mid(strBase, start%)
-
-
CheckForQuotes = strNew
-
End Function
-
Any advice on this is much appreciated.
8 6338
Why check for quotes? Why not replace them outright whether or not they're there? - Replace(value, "'", "''")
If all you are trying to do is remove double and single quotes from a string, then just use the replace function. - Dim myString as String
-
-
myString = "Isn't Filtered"
-
myString = Replace(myString, "'", "")
-
myString = Replace(myString, """", "")
-
-
Debug.Print myString
-
'myString now equals "Isnt Filtered"
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.
What I posted will escape it
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.
Not if you run the replace on the value before you append and prepend the starting and ending quotes.
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.
- strValue = "o'connor"
-
strSQL = "SELECT * FROM tableName WHERE " & _
-
"fieldName = '" & Replace(strValue, "'", "''") & "'"
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |