运行时错误3075(缺少语法)

时间:2016-02-16 18:54:24

标签: vba ms-access access-vba

If DCount("Business_Name", "Food Permisestbl", "[Business_Name] = '" & Me.Business_Name & "'") > 0 Then...

除非个人名字中有撇号(例如 D' Abero ),否则代码可以正常工作。然后发生运行时错误3075(缺失语法)!

如果名称包含撇号,我该怎么修改它以防止错误?

1 个答案:

答案 0 :(得分:1)

Me.Business_Name之前和之后使用双引号而不是单引号,或者在Me.Business_Name中为它包含的每个单引号替换2个单引号。

If DCount("Business_Name", "Food Permisestbl", "[Business_Name] = """ & Me.Business_Name.Value & """") > 0 Then

If DCount("Business_Name", "Food Permisestbl", "[Business_Name] = '" & Replace(Me.Business_Name.Value, "'", "''") & "'") > 0 Then

如果对第三个Dcount参数 Criteria 使用字符串变量,则可能会更容易进行故障排除。

Dim strCriteria As String
' then one of these next 2 lines ...
strCriteria = "[Business_Name] = """ & Me.Business_Name.Value & """"
'strCriteria = "[Business_Name] = '" & Replace(Me.Business_Name.Value, "'", "''") & "'"
Debug.Print strCriteria '<- inspect in Immediate window; Ctrl+g will take you there
' and finally ...
If DCount("Business_Name", "Food Permisestbl", strCriteria) > 0 Then