问题描述
我在 Microsoft Access 表中有记录。我正在尝试使用 Excel VBA 过滤它们。
我可以很好地查询表并创建记录集。在表中有一个字段“Row_Label”。我想查看记录集在该列中是否包含值“Tim's Roofing”。我知道我需要以某种方式逃避它,但每一种方法都失败了。
这是 .Filter 表达式
dim str as string
str = "[Row_Label] = """ & "Tim's Roofing" & """"
accessRSExistLabels.Filter = str
运行时错误“3001”:
参数类型错误、超出可接受范围或 相互矛盾。
当我将代码更改为
str = "[Row_Label]='Test'"
accessRSExistLabels.Filter = str
它工作正常,所以我(认为)这是一个逃避问题,我做错了,但我无法让 .Filter 工作。
感谢您的帮助
解决方法
您可以使用我的 CSql
函数:
str = "[Row_Label] = " & CSql("Tim's Roofing")
' xstr -> [Row_Label] = 'Tim''s Roofing'
' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
' SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
' SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
' SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
' SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2021-06-05. Gustav Brock,Cactus Data ApS,CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const TypeLongLong As Long = 20
Const SqlNull As String = " Null"
Dim Sql As String
#If Win64 Then
' VBA.VarType.vbLongLong exists.
#Else
Dim vbLongLong As Integer
vbLongLong = TypeLongLong
#End If
Select Case VarType(Value)
Case vbEmpty ' 0 Empty (uninitialized).
Sql = SqlNull
Case vbNull ' 1 Null (no valid data).
Sql = SqlNull
Case vbInteger ' 2 Integer.
Sql = Str(Value)
Case vbLong ' 3 Long integer.
Sql = Str(Value)
Case vbSingle ' 4 Single-precision floating-point number.
Sql = Str(Value)
Case vbDouble ' 5 Double-precision floating-point number.
Sql = Str(Value)
Case vbCurrency ' 6 Currency.
Sql = Str(Value)
Case vbDate ' 7 Date.
Sql = Format(Value," \#yyyy\/mm\/dd hh\:nn\:ss\#")
Case vbString ' 8 String.
Sql = Replace(Replace(Trim(Value),"'","''"),"""","""""")
If Sql = "" Then
Sql = SqlNull
Else
Sql = " '" & Sql & "'"
End If
Case vbObject ' 9 Object.
Sql = SqlNull
Case vbError ' 10 Error.
Sql = SqlNull
Case vbBoolean ' 11 Boolean.
Sql = Str(Abs(Value))
Case vbVariant ' 12 Variant (used only with arrays of variants).
Sql = SqlNull
Case vbDataObject ' 13 A data access object.
Sql = SqlNull
Case vbDecimal ' 14 Decimal.
Sql = Str(Value)
Case vbByte ' 17 Byte.
Sql = Str(Value)
Case vbLongLong ' 20 LongLong integer (Relevant on 64-bit platforms only).
Sql = Str(Value)
Case vbUserDefinedType ' 36 Variants that contain user-defined types.
Sql = SqlNull
Case vbArray ' 8192 Array.
Sql = SqlNull
Case Else ' Should not happen.
Sql = SqlNull
End Select
CSql = Sql & " "
End Function
,
试试:
str = "[Row_Label] = '" & "Tim''s Roofing" & "'"
单个'
需要加倍