简体   繁体   中英

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."

I can get this to work from a query just fine ( Like "*" & [Enter keyword] & "*" ), but I want to do this from a form. 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.

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. 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

Use just Like instead of = Like

Move the single quote before the * following Like

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

It would probably be useful to inspect the WhereCondition you're passing to DoCmd.OpenReport . Use Debug.Print to display it in the Immediate window. You can use Ctrl + g to go there.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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