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

Ms access MS Access 2010 VBA运行时错误3075(使用撇号搜索),ms-access,vba,ms-access-2010,Ms Access,Vba,Ms Access 2010,答案可能很简单——我想我就是想不出正确的搜索词 我有一个表单,可以打开另一个表单,显示与输入的搜索匹配的任何员工记录。您可以按姓氏、姓名或员工ID(使用单独的按钮)进行搜索;如果搜索结果为空,它会给你一个小消息框 代码运行良好,除了处理名称中的撇号(“O'Neill”、“O'Brien”等)的常见问题外,我发现了一个非常简单的撇号处理函数,但当我尝试在搜索查询中使用该函数时,它仍然会抛出3075运行时错误,我不确定原因。它只会抛出包含搜索的撇号的运行时错误,所以我觉得该函数可能没有完成我认为应该

答案可能很简单——我想我就是想不出正确的搜索词

我有一个表单,可以打开另一个表单,显示与输入的搜索匹配的任何员工记录。您可以按姓氏、姓名或员工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"
我正在尝试使用:

我修改了搜索代码以使用该功能,在第一行“strearch=”中插入了三行:

这是运行时错误对话框:


您可能想尝试以下方法:


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

为什么需要一个函数?只要简单地合并一个双引号,我的技巧就是使用Chr(34)


您的函数
adhHandleQuotes
正在代码中使用
stredLimiter
,但可选参数的名称是
分隔符
。您是否检查了
adhHandleQuotes(Me.empnusname)
返回的内容?谢谢!昨天我试着做了一个小时的危险的Chr(34);我想我一定是把一个撇号放错地方了,因为你这句话的复制/粘贴效果很好。我的职员将非常高兴不必翻阅以O now开头的全部员工名单。:)很高兴能帮上忙!祝你好运@Sarah:)
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
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
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