Article ID: 113955
Article Last Modified on 1/8/2003
Criteria$ = "author = """ & a$ & """" data1.recordset.findfirst Criteria$Visual Basic recognizes two double quotation marks in a row as an embedded double quotation mark. If a$ = "O'Conner" the Criteria$ will now read:
author = "O'Conner"Because the value you are searching is surrounded by double quotation marks, the Microsoft Access database engine will deal correctly with the embedded apostrophe.
Atre, Shaku
Atre', Shaku
Brackett, Michael H.
Brackett, Michael H."
VERSION 2.00
Begin Form Example
Caption = "Sample of searching for apostrophes in strings"
ClientHeight = 4020
ClientLeft = 1095
ClientTop = 1485
ClientWidth = 7365
Height = 4425
Left = 1035
LinkTopic = "Form1"
ScaleHeight = 4020
ScaleWidth = 7365
Top = 1140
Width = 7485
Begin CommandButton Command2
Caption = "With Apostrophe"
Height = 375
Left = 4680
TabIndex = 4
Top = 720
Width = 2055
End
Begin CommandButton Command1
Caption = "With out Apostrophe"
Height = 375
Left = 840
TabIndex = 1
Top = 720
Width = 2055
End
Begin ListBox List1
Height = 1005
Left = 2160
TabIndex = 0
Top = 2400
Width = 3375
End
Begin Data Data1
Caption = "Data1"
Connect = ""
DatabaseName = "C:\VB3\BIBLIO.MDB"
Exclusive = 0 'False
Height = 495
Left = 5040
Options = 0
ReadOnly = 0 'False
RecordSource = "Authors"
Top = 1440
Visible = 0 'False
Width = 2175
End
Begin Label Label4
Caption = "<---- Samples ----> Strings to search"
Height = 495
Left = 3000
TabIndex = 6
Top = 240
Width = 1575
End
Begin Label Label3
Caption = "Atre', Shaku."
Height = 255
Left = 4800
TabIndex = 5
Top = 240
Width = 1935
End
Begin Label Label1
Caption = "Brackett, Michael H."""
Height = 255
Left = 840
TabIndex = 3
Top = 240
Width = 1935
End
Begin Label Label2
Caption = "Results, below"
Height = 255
Left = 3240
TabIndex = 2
Top = 1920
Width = 1335
End
End
Dim pos% 'position of where an apostraphe may be in a string
Sub check_apostrophe (var1 As String)
pos% = InStr(1, var1, Chr$(39))
End Sub
Sub Command1_Click () 'searches a string without apostrophe
list1.Clear
a$ = label1.Caption 'contains the string: Brackett, Michael H."
pos% = 0
Call check_apostrophe(a$) 'check for apostrophe in a string
If pos% <> 0 Then
tmp$ = Mid$(a$, 1, pos% - 1) & "*" 'strip out apostrophe for search
criteria$ = "author like '" & tmp$ & "'" 'search with apostrophe
data1.Recordset.FindFirst criteria$
list1.AddItem data1.Recordset("author")
Else
criteria$ = "author = '" & label1.Caption & "'"
data1.Recordset.FindFirst criteria$ 'search without apostrophe
list1.AddItem data1.Recordset("author")
End If
End Sub
Sub Command2_Click () 'searches a string with apostrophe
list1.Clear
a$ = label2.Caption 'contains the string: Atre', Shaku.
pos% = 0
Call check_apostrophe(a$) 'check for apostrophe in a string
If pos% <> 0 Then
tmp$ = Mid$(a$, 1, pos% - 1) & "*" 'strip out apostrophe for search
criteria$ = "author like '" & tmp$ & "'" 'search with apostrophe
data1.Recordset.FindFirst criteria$
list1.AddItem data1.Recordset("author")
Else
criteria$ = "author = '" & label2.Caption & "'"
data1.Recordset.FindFirst criteria$ 'search without apostrophe
list1.AddItem data1.Recordset("author")
End If
End Sub
Additional query words: 3.00
Keywords: KB113955