我有一个表单,用户将要在文本框中输入项目名称的部分或完整字符串值,单击表单中的按钮,然后打开仅包含按这些结果过滤的项目的报告。例如,如果我输入“ 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
答案 0 :(得分:2)
使strwhere1 =
的右侧为单个字符串
仅使用Like
而不是= Like
将单引号移到*
之后的Like
之前
strwhere1 = "project_name Like '*" & Me![findproject] & "*'"
检查传递给DoCmd.OpenReport
的 WhereCondition 可能会很有用。使用Debug.Print
将其显示在立即窗口中。您可以使用 Ctrl + g 转到那里。
Debug.Print strwhere1
DoCmd.OpenReport stDocName1, acViewReport, , strwhere1, acWindowNormal
答案 1 :(得分:0)
我想*
必须在单引号内
"Like '*" & Me![findproject] & "*'"
还必须将project_name与以下语句连接起来:
strwhere1 = project_name & " Like '*" & Me![findproject] & "*'"