In my VB application I’m taking the recordset and doing a filter on it to get some information. This works fine until it comes across a string value with an apostrophe. I know why this is occuring, but how do I code around that?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Using apostrophes in filters (VB6/Access 2K)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Using apostrophes in filters (VB6/Access 2K)
- This topic has 16 replies, 5 voices, and was last updated 23 years, 1 month ago.
AuthorTopicWSMike Shea
AskWoody LoungerFebruary 8, 2002 at 1:58 pm #366623Viewing 0 reply threadsAuthorReplies-
WSFrancois
AskWoody Lounger -
WSMike Shea
AskWoody LoungerFebruary 8, 2002 at 2:28 pm #569114Thanks Francois. This should have worked. When I looked at it in the debug window the syntax looked absolutely correct. Can you see what is wrong in the FILTER = section.
Private Sub cmdSave_Click()
”’Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
On Error GoTo ErrorHandler
‘****************************************************************
‘Section to handle Diagnosis if provided
‘****************************************************************lblStatus.Caption = “Processing . . .”
Screen.MousePointer = vbHourglass
Me.Refresh
If Not (cboDiag.Text = “” Or IsNull(cboDiag.Text)) Then
cn.Open sConnString
With rs
.Source = “SELECT Diagnosis FROM tblDiagnosis ORDER BY Diagnosis”
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockPessimistic
End Withrs.Open , cn, , , adCmdText
Debug.Print HandleQuotes(cboDiag.Text, “‘”)
‘ rs.Filter = “Diagnosis = ‘” & cboDiag.Text & “‘”
rs.Filter = “Diagnosis = ” & Chr(34) & cboDiag.Text & Chr(34)
If rs.RecordCount = 0 Then
With rs
.Filter = adFilterNone
.AddNew
!Diagnosis = cboDiag.Text
.Update
.RequeryEnd With
‘Call RefreshDiag
‘ rs.Filter = adFilterNone
End If ‘rs.RecordCount = 0 Then
End If ‘Not (cboDiag.Text = “” Or IsNull(cboDiag.Text)) Then
rs.Filter = adFilterNone
‘****************************************************************
‘Record Data
‘****************************************************************If cn.State = 1 Then
cn.Close
End If”’Set cn = Nothing
If bUpdateMode = False Then
Call SaveFormData
Else
Call UpdateFormData
End IfExit_cmdSave_Click:
Set rs = Nothing
If cn.State = 1 Then
cn.Close
End IfcmdSave.Enabled = False
cmdDelete.Enabled = True
bIsDirty = FalselblStatus.Caption = “Ready”
Screen.MousePointer = vbNormal
Exit Sub
ErrorHandler:
MsgBox “Error #” & Err.Number & ” ” & Err.Description, vbCritical, “Error cmdSave_Click”
Resume Exit_cmdSave_Click
End Sub
-
WSFrancois
AskWoody LoungerFebruary 8, 2002 at 2:50 pm #569117I’m not familiar with ado but I would change the code to
With rs .Source = "SELECT Diagnosis FROM tblDiagnosis WHERE Diagnosis = " & Chr(34) & cboDiag.Text & Chr(34) .CursorType = adOpenDynamic .CursorLocation = adUseClient .LockType = adLockPessimistic End With rs.Open , cn, , , adCmdText If rs.RecordCount = 0 Then ...
-
WSMike Shea
AskWoody Lounger
-
-
WSrory
AskWoody Lounger -
WSMike Shea
AskWoody LoungerFebruary 8, 2002 at 4:17 pm #569132It still had the problem with the apostrophe in the string. If I were to do a debug.print, copy and paste the value defining that filter into an Access query on the back end, it pulls it up just fine. The syntax looks correct with the code you gave, it is just being persistent about that apostrophe. . .
-
WSrory
AskWoody Lounger -
WSMike Shea
AskWoody Lounger -
WSrory
AskWoody LoungerFebruary 11, 2002 at 7:34 am #569221Edited by rory on 11-Feb-02 08:34.
Hi Mike,
I think you’ve got two options. Either run some code to double the apostrophe in the string you’re searching for (which should cause ADO to treat it as a literal) or use the following syntax:
rs.filter “Diagnosis = #” & cboForgotTheName.Text & “#”
The latter will handle the eventuality that there’s more than one apostrophe in the text (which the former won’t) but has the drawback that there can’t be a ‘#’ sign in the text – I don’t know if that could be an issue for you.
Hope that helps.**Later edit:
My statement that the former method wouldn’t work for multiple apostrophes was based on something I read – having experimented this morning, it actually seems to work OK for more than one apostrophe. – Rory 11/2/2002** -
WSjscher2000
AskWoody LoungerFebruary 8, 2002 at 9:08 pm #569242For a problem that must happen to every ADO developer on Earth, it is surprisingly difficult to find this trick!
MS has a VB code sample for a class/ActiveX DLL that doubles the apostrophe: A Class to Prepare Strings for Submission to a Database
-
WSchrisgreaves
AskWoody LoungerApril 3, 2002 at 7:43 pm #580315> every ADO developer on Earth,
And then some ….
SELECT * from [Corporate] WHERE [business]='Hudson's Bay Company'
I just tried the doubling-trick, but it didn’t work for me; the line below returns an empty record set (Run-time error ‘3021’ No Current record).
SELECT * from [Corporate] WHERE [business]='Hudson''s Bay Company'
I am familiar with enclosing in double-quotes if the search-string contains a single quote; and, of course, there’s the Australian method of enclosing in single-quotes if the search-string contains a double quote.
However What does one do in general with a field that could contain several delimiters? What about :
Mary's "Fairy" Flowers
as a company name? Neither the double-embrace or the single-embrace will satisfy me.
Does anyone have a general solution that will permit development of a search string for any characters?
If Access97 ( to use but one example) will allow me to enter any character (not just any keyboard character, but ANY ASCII code 000 through 255) in a field, how could one possibly issue a search string for that record if doubling a delimiter won’t work?
Or am I more lost in this than I think I am?
-
WSchrisgreaves
AskWoody LoungerApril 3, 2002 at 8:35 pm #580328Apologies. My doubled-up-quote search wasn’t working because I didn’t code my utility function correctly. My initial essay was appending two extra (!!) single-quotes to the end of the fabricated string, and that is why no records were found.
FWIW I read Gupta’s SQL Base “SQL reference”, and right there on page 2-17 it says:
A string constant must be enclosed in single quotes when used in a SQL command. To include a single quote in a string constant, use two adjacent single quotes.
Now Access/VBA/SQL ain’t Gupta, but that definition fits in pretty well with my understanding of the world.
That’s what made me go back and wonder whether maybe, just maybe, I’d screwed up (again) in my programming.
I had.
Good:
' replace each occurrence of the delimiter by TWO instances of it Dim strResult As String ' transient result field strResult = strResult & u.strSplitStringAt(strText, strdelim, True) strText = u.strSplitStringAt(strText, strdelim, False) While strText "" strResult = strResult & strdelim & strdelim strResult = strResult & u.strSplitStringAt(strText, strdelim, True) strText = u.strSplitStringAt(strText, strdelim, False) Wend strDouble = strResult
Bad:
' replace each occurrence of the delimiter by TWO instances of it Dim strResult As String ' transient result field strResult = strResult & u.strSplitStringAt(strText, strdelim, True) strText = u.strSplitStringAt(strText, strdelim, False) While strText "" strResult = strResult & strdelim & strdelim strResult = strResult & u.strSplitStringAt(strText, strdelim, True) strText = u.strSplitStringAt(strText, strdelim, False) Wend
-
WSMike Shea
AskWoody Lounger -
WSrory
AskWoody Lounger -
WSFrancois
AskWoody Lounger -
WSMike Shea
AskWoody Lounger
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Giving UniGetUi a test run.
by
RetiredGeek
5 hours, 43 minutes ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
13 hours, 20 minutes ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
11 hours, 18 minutes ago -
Auto Time Zone Adjustment
by
wadeer
17 hours, 49 minutes ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
15 hours, 29 minutes ago -
Manage your browsing experience with Edge
by
Mary Branscombe
4 hours, 34 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
7 hours ago -
Hobbies — There’s free software for that!
by
Deanna McElveen
11 hours, 18 minutes ago -
Apps included with macOS
by
Will Fastie
10 hours, 55 minutes ago -
Xfinity home internet
by
MrJimPhelps
10 hours, 51 minutes ago -
Convert PowerPoint presentation to Impress
by
RetiredGeek
10 hours, 48 minutes ago -
Debian 12.11 released
by
Alex5723
1 day, 14 hours ago -
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
1 day, 18 hours ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
22 hours, 5 minutes ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
14 hours, 30 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
2 days, 11 hours ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
2 days, 1 hour ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
2 days, 14 hours ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
2 days, 6 hours ago -
Some advice for managing my wireless internet gateway
by
LHiggins
1 day, 14 hours ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
16 hours ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
2 days, 23 hours ago -
Sometimes I wonder about these bots
by
Susan Bradley
11 hours, 50 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
2 days, 10 hours ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
3 days, 13 hours ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
1 day, 21 hours ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
1 day, 10 hours ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
3 days, 16 hours ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
3 days, 16 hours ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
4 hours, 36 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.