MS Access 2010 VBA运行时错误3075(使用撇号搜索)

时间:2014-09-17 15:20:19

标签: vba ms-access access-vba ms-access-2010

答案可能很简单 - 我怀疑,我无法提出正确的搜索字词。

我有一个打开另一个表单的表单,显示与输入的搜索匹配的任何员工记录您可以按姓氏,姓名或员工ID搜索(使用单独的按钮);如果您的搜索没有任何结果,它会为您提供一个消息框。

代码工作正常,除了在名称中处理撇号的常见问题(" O' Neill,"" O' Brien,"等等)我发现了一个非常简单的撇号处理函数,但是当我尝试在搜索查询中使用该函数时,它仍然会引发3075运行时错误,我不确定原因。它只会在包含撇号的搜索中抛出运行时错误,所以我觉得这个函数可能不是我认为的那样。

我很高兴接受涉及"使用此功能但添加更多引号(或其他)的解决方案"以及全新的想法。我喜欢使用之类的这个功能,因为它非常小,因此它可以更快更清洁地取代按名称搜索对每个出现的地方进行编码。

这是可行的代码:

Private Sub btnSearchSurname_Click()

Dim frm As Form
Dim strSearch As String

strSearch = "[List_Employees.Surname] like '" & Me.EmpSurname & "*'"
strSearch = strSearch & " AND [CurrentEmployee] = " & True
DoCmd.OpenForm "Employee_Entry_Extended_Certs", , , strSearch, , acHidden
Set frm = Forms("Employee_Entry_Extended_Certs")
If frm.Recordset.RecordCount > 0 Then
    frm.Visible = True
    Else
    MsgBox ("Employee not found.  Try the 'all' button to see if they're inactive.  If that doesn't work, please check for typos and try again.")
    DoCmd.Close acForm, "Employee_Entry_Extended_Certs"
    Call OpenPayrollCloseRest
End If

DoCmd.Close acForm, "Find_An_Employee"

我试图使用this simple public function to handle apostrophes

Public Function adhHandleQuotes(ByVal varValue As Variant, Optional Delimiter As String = "'") As Variant
    ' Replace all instances of a string delimiter with TWO instances,
    ' thereby handling the darned quote issue once and for all. Also,
    ' surround the string with the delimiter, as well.

    ' Returns Null if the String was Null, otherwise
    ' returns the String with all instances of strDelimiter
    ' replaced with two of each.

    adhHandleQuotes = strDelimiter & Replace(varValue, strDelimiter, strDelimiter & strDelimiter) & strDelimiter
End Function

我通过插入三行代替第一个" strSearch ="来修改搜索代码以使用该功能。行:

Dim strSearch As String
Dim strTerm As String

strTerm = adhHandleQuotes(Me.EmpSurname)
strSearch = "[List_Employees.Surname] like '" & strTerm & "*'"
strSearch = strSearch & " AND [CurrentEmployee] = " & True
DoCmd.OpenForm "Employee_Entry_Extended_Certs", , , strSearch, , acHidden

这是运行时错误对话框:

Runtime Error '3075':  Syntax error (missing operator) in query expression '[List_Employees.Surname] like 'o'cal*' AND [CurrentEmployee]=True'

2 个答案:

答案 0 :(得分:1)

为什么你甚至需要一个功能?只需简单地加入Double Quotes,我的黑客就是使用Chr(34)。

Private Sub btnSearchSurname_Click()

    Dim frm As Form
    Dim strSearch As String

    strSearch = "[List_Employees.Surname] Like " & Chr(34) & Me.EmpSurname & "*" & Chr(34)
    strSearch = strSearch & " AND [CurrentEmployee] = True"

    DoCmd.OpenForm "Employee_Entry_Extended_Certs", , , strSearch, , acHidden

    Set frm = Forms("Employee_Entry_Extended_Certs")
    If frm.Recordset.RecordCount > 0 Then
        frm.Visible = True
    Else
        MsgBox ("Employee not found.  Try the 'all' button to see if they're inactive.  If that doesn't work, please check for typos and try again.")
        DoCmd.Close acForm, "Employee_Entry_Extended_Certs"
        Call OpenPayrollCloseRest
    End If

    DoCmd.Close acForm, "Find_An_Employee"
End Sub

答案 1 :(得分:0)

你可能想试试这个: Access VBA, unescaped single quotes, Replace(), and null

不是加倍撇号,而是用双引号括起来。