VBA编译错误:预期的语句结束

时间:2015-01-03 23:49:43

标签: vba ms-access access-vba

我正在尝试对表单中的组合框进行编码,以便在用户键入时自动过滤选项。我有一个代码但我不断收到编译错误。这是我的代码:

Private Sub UTIL_FCLTY_ID_Change()
    If Nz(Me.UTIL_FCLTY_ID.Text) = “” Then
        Me.Form.Filter = “”
        Me.FilterOn = False
    ElseIf Me.UTIL_FCLTY_ID.ListIndex <> -1 Then
        Me.Form.Filter = “[UTILITY FACILITY TYPE NAME:] = ‘” & _
                 Replace(Me.UTIL_FCLTY_ID.Text, “‘”, “””) & “‘”
        Me.FilterOn = True
    Else
        Me.Form.Filter = “[UTILITY FACILITY TYPE NAME:] Like ‘*” & _
                 Replace(Me.UTIL_FCLTY_ID.Text, “‘”, “””) & “*'”
        Me.FilterOn = True

    End If

    Me.UTIL_FCLTY_ID.SetFocus
    Me.UTIL_FCLTY_ID.SelStart = Len(Me.UTIL_FCLTY_ID.Text)

End Sub

1 个答案:

答案 0 :(得分:1)

正如@HansUp所提到的,您使用的是非标准引号和非标准撇号。用正确的替换它们。 第二个问题是这句话: &#34;&#34;&#34; 。 VBA编译器将其视为 &#34;&#34; &#34; (空字符串和未结算的引号)。你需要在报价内使用双引号来解决它: &#34;&#34;&#34;&#34;&#34;

最后你的程序应该是这样的:

Private Sub UTIL_FCLTY_ID_Change()
    If Nz(Me.UTIL_FCLTY_ID.Text) = "" Then
        Me.Form.Filter = ""
        Me.FilterOn = False
        Exit Sub 'there's nothing to proceed ;)
    End If

    If Me.UTIL_FCLTY_ID.ListIndex <> -1 Then
        Me.Form.Filter = "[UTILITY FACILITY TYPE NAME:] = '" & _
                 Replace(Me.UTIL_FCLTY_ID.Text, "'", """") & "'"
        Me.FilterOn = True
    Else
        Me.Form.Filter = "[UTILITY FACILITY TYPE NAME:] Like '*" & _
                 Replace(Me.UTIL_FCLTY_ID.Text, "'", """") & "*'"
        Me.FilterOn = True
    End If

    Me.UTIL_FCLTY_ID.SetFocus
    Me.UTIL_FCLTY_ID.SelStart = Len(Me.UTIL_FCLTY_ID.Text)

End Sub