【发布时间】: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