简体   繁体   English

MS Access Form - 来自文本框的字符串,用于 LIKE 查询以过滤报告结果

[英]MS Access Form - string from textbox used for LIKE query to filter report results

I have a form where the user will want to enter a partial or full string value of a project name in a textbox, hit a button in the form, and open a report that only includes projects filtered by these results.我有一个表单,用户希望在文本框中输入项目名称的部分或完整字符串值,点击表单中的按钮,然后打开一个仅包含按这些结果过滤的项目的报告。 For example, if I entered "Acc", all results in my project_name field would only include those with "Acc."例如,如果我输入“Acc”,我的 project_name 字段中的所有结果将只包括那些带有“Acc”的结果。

I can get this to work from a query just fine ( Like "*" & [Enter keyword] & "*" ), but I want to do this from a form.我可以很好地从查询中得到它( Like "*" & [Enter keyword] & "*" ),但我想从表单中做到这一点。 I also have a few other fields I would like to do this with too, and I don't want to have to create a new report and query for each field.我还有一些其他字段我也想这样做,我不想为每个字段创建一个新的报告和查询。 I'd rather be able to do this with VBA and a form.我宁愿能够用 VBA 和一个表单来做到这一点。

My code opens the report, but there are no results.我的代码打开报告,但没有结果。 I have even tried entering individual letters (ie, "a") or other obviously strings with no luck.我什至尝试输入单个字母(即“a”)或其他明显的字符串,但没有运气。 Here is my code:这是我的代码:

  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

Make the right side of strwhere1 = one single string使strwhere1 =的右侧strwhere1 =一个字符串

Use just Like instead of = Like使用 Just Like而不是= Like

Move the single quote before the * following Like将单引号移动到*后面的Like之前

strwhere1 = "project_name Like '*" & Me![findproject] & "*'"

It would probably be useful to inspect the WhereCondition you're passing to DoCmd.OpenReport .检查您传递给DoCmd.OpenReportWhereCondition可能会很有用。 Use Debug.Print to display it in the Immediate window.使用Debug.Print在立即窗口中显示它。 You can use Ctrl + g to go there.你可以使用Ctrl + g去那里。

Debug.Print strwhere1
DoCmd.OpenReport stDocName1, acViewReport, , strwhere1, acWindowNormal

I guess the * must be inside the single quotes:我猜*必须在单引号内:

"Like '*" & Me![findproject] & "*'"

also project_name must be concatenated with the statement:还必须将 project_name 与语句连接起来:

strwhere1 = project_name & " Like '*" & Me![findproject] & "*'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM