2016-06-10 26 views
0

我正在使用Access 2013中的數據庫,並且我想使用一種表單將篩選器應用於其他表單。有問題的表單稱爲高級搜索,我希望它使用輸入到文本框中的關鍵字將過濾器應用到名爲FormA的拆分表單上。每當我點擊搜索按鈕,我收到「運行時錯誤424:所需的對象」。VBA for Access以單獨的形式應用篩選器

Private Sub Search_Click() 'Applies a filter based on search Criteria 
    Forms!FormA.Filter = _ 
    "Field1 Like '*" & SearchBox.Value & "*' And Field2 Like '*" & SearchBox.Value & "*'" 
    Forms!FormA.FilterOn = True 
End Sub 

我感覺好像我在搞FormA的參考。有任何想法嗎?

+0

嘗試打開過濾器,在下面添加以下行:'Forms!FormA.FilterOn = True' – Parfait

+0

忘記複製該行,儘管您確實是正確的,因爲我需要使其最終工作。當它出現時仍然會出現相同的運行時錯誤。 – TheEngineEar

回答

1

「object required」錯誤是由SearchBox.Value引起的。 Access沒有找到名爲SearchBox的控件,其格式爲(高級搜索),其中包含您的Search命令按鈕。

在找到正確的名稱,我建議你建立你的Filter串這樣的...

Dim strFilter As String 
strFilter = "Field1 Like '*" & Me![YourSearchBoxNameHere].Value & _ 
    "*' And Field2 Like '*" & Me![YourSearchBoxNameHere].Value & "*'" 
Debug.Print strFilter '<- inspect this in Immediate window; Ctrl+g will take you there 

然後應用Filter ......

Forms!FormA.Filter = strFilter 
Forms!FormA.FilterOn = True 

也包括在Option Explicit所有代碼模塊的聲明部分。然後從VB編輯器的主菜單運行Debug-> Compile。修復編譯器所抱怨的任何內容,然後重新編譯。重複直到沒有更多的編譯錯誤。

+0

謝謝!我疲憊的眼睛想念錯過了明顯的錯誤。我甚至在其他地方發現了一些命名錯誤。 – TheEngineEar

+0

不客氣。在VB編輯器中,選擇工具 - >選項。然後在該選項對話框的編輯器選項卡上,勾選需要變量聲明旁邊的複選標記。該設置將導致「Option Explicit」被包含在您創建的任何* new *模塊中。但它不適用於現有的模塊。 – HansUp

+0

另外,引用對象時使用感嘆號而不是句號的原因是什麼? – TheEngineEar