在 VBA Access 中用 null 替换撇号
Posted
技术标签:
【中文标题】在 VBA Access 中用 null 替换撇号【英文标题】:replace apostrophe with null in VBA Access 【发布时间】:2016-05-24 20:45:11 【问题描述】:我正在尝试更新 Access 表中将撇号替换为 null 的字段。
Dim strLimitRecordTable As String: strLimitRecordTable = Me.txtLimitRecordTable
DoCmd.RunSQL "UPDATE " & strLimitRecordTable & " SET vCompanyMapped = replace(vCompanyMapped, ''', '') WHERE vCompanyMapped like '*'*';"
但我得到这个错误:
表达式替换中的语法错误(vCompanyMapped, ''', '') WHERE vCompanyMapped like ''';"
我觉得这与用空字符串''替换'''有关
我在这里做错了什么?
我有另一个可以正常工作的查询:
DoCmd.RunSQL "UPDATE [" & strLimitRecordTable & "] SET Domain = Mid([Email],InStr([Email],'@')+1,Len([Email])-InStr([Email],'@')) WHERE Email Like '*@*.*';"
【问题讨论】:
您在两个地方有语法错误。首先将 like ' * ' * ';" 更改为 like '*';" 然后将 ''' 替换为 "'" [必须编辑,因为翻译了您的字符串而不是您所显示的] @WayneG.Dunn 我尝试将 ''' 替换为 "'" 但 VBA 将第一个引号视为语句的结尾,并将撇号视为注释的开头跨度> @WayneG.Dunn 关于 like '*';" - 我这样做是因为我想确保该字段包含撇号。现在我想到了,我甚至不需要那个 WHERE 子句 是的,嵌入的引号可能会很麻烦...这是正确的语法:“UPDATE” & strLimitRecordTable &“ SET vCompanyMapped = Replace(vCompanyMapped, ""'"", """" ) WHERE ((vCompanyMapped Like ""'""));"但是,是的,我会摆脱 WHERE 子句... 【参考方案1】:在这种情况下,我会使用 ascii 字符来查找撇号,我建议您尝试一下:
Dim strLimitRecordTable As String: strLimitRecordTable =
Me.txtLimitRecordTable DoCmd.RunSQL "UPDATE " & strLimitRecordTable & " SET
vCompanyMapped = replace(vCompanyMapped, chr(39),'')
WHERE vCompanyMapped like '*'*';"
通常当我必须以编程方式搜索标点符号时,我发现使用 Ascii 指示符很有帮助。它有助于减少错误。
【讨论】:
谢谢,这很有用!我只需要在你的 replace() 中添加一个参数:replace(vCompanyMapped,Chr(39), '')
如上所述,我什至不需要导致语法错误的 WHERE 子句。
如果你能推荐我的修改答案,那么我会得到加分:)。【参考方案2】:
在连接 SQL 时,您可以使用此功能来避免此问题和大多数其他问题:
' 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.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const vbLongLong As Integer = 20
Const SqlNull As String = " Null"
Dim Sql As String
Dim LongLong As Integer
#If Win32 Then
LongLong = vbLongLong
#End If
#If Win64 Then
LongLong = VBA.vbLongLong
#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(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 LongLong ' 20 LongLong integer (Valid 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
【讨论】:
以上是关于在 VBA Access 中用 null 替换撇号的主要内容,如果未能解决你的问题,请参考以下文章
在access中用vba如何把SQL语句查询到的一个值赋给变量?
用 .csv 文件中的 VBA 仅在 3 列中用分号替换逗号
如何使用SQL更新VBA Access中包含NULL值的列?