我有一个表单,用户希望在文本框中输入项目名称的部分或完整字符串值,点击表单中的按钮,然后打开仅包含按这些结果筛选的项目的报告。 例如,如果我输入"Acc",则project_name字段中的所有结果将仅包含带有"Acc"的结果。
我可以让它从查询中正常工作(Like "*" & [Enter keyword] & "*"
(,但我想从表单中执行此操作。我还想使用其他一些字段,并且我不想为每个字段创建新报表和查询。 我宁愿能够使用 VBA 和表单来做到这一点。
我的代码打开报表,但没有结果。 我甚至尝试输入单个字母(即"a"(或其他明显的字符串,但没有运气。这是我的代码:
Private Sub Command0_Click()
Dim stDocName1 As String, strwhere1 As String
Dim stLinkCriteria1 As String
stDocName1 = "Grantlist"
strwhere1 = project_name = "Like *'" & Me![findproject] & "*'"
DoCmd.OpenReport stDocName1, acViewReport, , strwhere1, acWindowNormal
End Sub
将strwhere1 =
的右侧设为一个字符串
只使用Like
而不是= Like
将单引号移到*
后面的Like
strwhere1 = "project_name Like '*" & Me![findproject] & "*'"
检查您要传递给DoCmd.OpenReport
的位置条件可能会很有用。 使用Debug.Print
将其显示在"即时"窗口中。 你可以使用 Ctrl+g 去那里。
Debug.Print strwhere1
DoCmd.OpenReport stDocName1, acViewReport, , strwhere1, acWindowNormal
我想*
必须在单引号内:
"Like '*" & Me![findproject] & "*'"
此外,project_name必须与以下语句连接:
strwhere1 = project_name & " Like '*" & Me![findproject] & "*'"