VB6 在过滤器上输入撇号时出错,使用 ADODB
VB6 Getting Error when inputting apostrophe on Filter, using ADODB
我正在尝试从文本框中过滤数据网格,但如果在文本框中键入撇号或 ' 则不行,我使用的是 ADODB 和 VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
您需要 "escape" 您的引号或单引号。简单的方法是在 vi 中将所有 ' 替换为 '' 并将所有 " 替换为 "".
正如 ADO 文档中所说(人们什么时候养成了调用 ADO "ADODB" 的奇怪习惯???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on O'Malley
, the criteria string should be "col1 = 'O''Malley'"
. To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on '1'
, the criteria string should be "col1 = #'1'#"
.
这里还必须考虑通配符规则:
If Operator is LIKE, Value can use wildcards. Only the asterisk (*
) and percent sign (%
) wild cards are allowed, and they must be the last character in the string. Value cannot be null
.
但有点令人困惑:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example, LastName Like '*mit*'
), or only at the end of the pattern (for example, LastName Like 'Smit*'
).
我正在尝试从文本框中过滤数据网格,但如果在文本框中键入撇号或 ' 则不行,我使用的是 ADODB 和 VB6
Public Sub pGetCustomer(Optional vSearch As String)
If vSearch = "'" Then
xRSTree.Filter = adFilterNone
xRSTree.Requery
Else
xRSTree.Filter = "description like '%" & vSearch & "%' or customercode like '%" & vSearch & "%'"
End If
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
您需要 "escape" 您的引号或单引号。简单的方法是在 vi 中将所有 ' 替换为 '' 并将所有 " 替换为 "".
正如 ADO 文档中所说(人们什么时候养成了调用 ADO "ADODB" 的奇怪习惯???):
Note To include single quotation marks (') in the filter Value, use two single quotation marks to represent one. For example, to filter on
O'Malley
, the criteria string should be"col1 = 'O''Malley'"
. To include single quotation marks at both the beginning and the end of the filter value, enclose the string with pound signs (#). For example, to filter on'1'
, the criteria string should be"col1 = #'1'#"
.
这里还必须考虑通配符规则:
If Operator is LIKE, Value can use wildcards. Only the asterisk (
*
) and percent sign (%
) wild cards are allowed, and they must be the last character in the string. Value cannot benull
.
但有点令人困惑:
In a LIKE clause, you can use a wildcard at the beginning and end of the pattern (for example,
LastName Like '*mit*'
), or only at the end of the pattern (for example,LastName Like 'Smit*'
).