[英]Unable to access Form.Textbox.Text property when the source query returns no results in MS Access
[英]MS Access Form - string from textbox used for LIKE query to filter report results
我有一個表單,用戶希望在文本框中輸入項目名稱的部分或完整字符串值,點擊表單中的按鈕,然后打開一個僅包含按這些結果過濾的項目的報告。 例如,如果我輸入“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 =
的右側strwhere1 =
一個字符串
使用 Just 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
我猜*
必須在單引號內:
"Like '*" & Me![findproject] & "*'"
還必須將 project_name 與語句連接起來:
strwhere1 = project_name & " Like '*" & Me![findproject] & "*'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.