0

在 Access 2010 中,我正在尝试为表单上的联系人表创建一个搜索框。

这是我的代码:

Private Sub Command119_Click()
On Error GoTo Command119_Click_Err

If (Eval("[Forms]![frmTitlePage]![SearchBox] Is Null")) Then
    ' Clear Filter when search box empty
    DoCmd.RunCommand acCmdRemoveFilterSort
End If
' Handle "'s in search
TempVars.Add "strSearch", Replace(Forms!frmTitlePage!SearchBox, """", """""")
' Build the Filter
TempVars.Add "strFilter", "([Last Name] Like "" * " & [TempVars]![strSearch] & " * "" )"
TempVars.Add "strFilter", TempVars!strFilter & " OR ([First Name] Like "" * " & [TempVars]![strSearch] & " * "" )"
DoCmd.ApplyFilter "", TempVars!strFilter, ""
TempVars.Remove "strFilter"
TempVars.Remove "strSearch"


Command119_Click_Exit:
Exit Sub

Command119_Click_Err:
MsgBox Error$
Resume Command119_Click_Exit

End Sub

我显然不擅长这种编码,所以任何清理这个搜索工具的额外帮助都会有很大帮助。

4

1 回答 1

0

您的搜索中有一些额外的空格

您生成的原始代码Like " * Smith * "

删除您添加的位置周围的额外空格[TempVars]![strSearch]以产生此:

TempVars.Add "strFilter", "([Last Name] Like ""*" & [TempVars]![strSearch] & "*"" )"
TempVars.Add "strFilter", TempVars!strFilter & " OR ([First Name] Like ""*" & [TempVars]![strSearch] & "*"" )"

现在应该产生Like "*Smith*"

于 2012-07-02T15:58:02.473 回答