【问题标题】:MS Access 2010 VBA runtime error 3075 (searching with apostrophes)MS Access 2010 VBA 运行时错误 3075(使用撇号搜索)
【发布时间】:2014-09-17 15:20:19
【问题描述】:

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

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

这是运行时错误对话框:

【问题讨论】:

  • 您的函数adhHandleQuotes 在代码中使用strDelimiter,但可选参数的名称是Delimiter。你检查过adhHandleQuotes(Me.EmpSurname) 返回的内容吗?

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


【解决方案1】:

为什么你甚至需要一个函数?只需简单地包含一个双引号,我的技巧就是使用 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

【讨论】:

  • 谢谢!昨天我试图做危险的 Chr(34) 大约一个小时;我想我一定是在其中放错了一个撇号,因为您的行的复制/粘贴效果很好。我的职员会很高兴现在不必滚动查看姓名以 O 开头的整个员工名单。 :)
  • 很高兴能帮上忙!祝你好运@Sarah :)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多