1

I have the following Main Form "Edit Services" with a Sub Form "Edit Services SubForm":

enter image description here

I want to filter the Sub Form based on the check box at the top of the Main Form that correlates to fields in the Sub Form that are on a Yes/No criteria.

I.E. If the In-Shop box is checked then it should filter anything that has In-Shop as Yes in the Sub Form. If it is unchecked it should clear the filter.

ANSWER

Private Sub chbxInShop_Click()
    If Me.chbxInShop.Value Then
        Me.[Edit Services SubForm].Form.Filter = "[SrvInShop] = True"
        Me.[Edit Services SubForm].Form.FilterOn = True
        Me.[Edit Services SubForm].Form.Requery
    End If
End Sub
11
  • 1
    But what does happen instead? Does Access complain when it encounters this? ... .Form.Filter = "In-Shop = 'Yes'" If so, perhaps you need ... .Form.Filter = "[In-Shop] = True"
    – HansUp
    Jan 9, 2014 at 18:05
  • I get the following error, no matter if it's In-Shop = Yes or [In-Shop] = True... "Run-time error '2465': Microsoft Access can't find the field '|1' referred to in your expression." Jan 9, 2014 at 18:27
  • That error message suggests the subform's record source does not include a field named In-Shop. Please double-check.
    – HansUp
    Jan 9, 2014 at 18:31
  • Note if you assigned a Caption for the field name in table design, the column header, In-Shop, is the field's Caption rather than it's name. You must use the field name in the Filter expression.
    – HansUp
    Jan 9, 2014 at 18:34
  • Is it based off the table or the subform field names? Still not having much luck with it. Jan 9, 2014 at 18:48

2 Answers 2

0

Based on your sample database, it seems the code had the True and False conditions reversed. For example, the following line means "If chbxInShop is False (un-checked) then do the following", which is to apply the subform filter. But you really want to apply the filter when the check box is checked ... meaning its .Value is True (-1).

If Me.chbxInShop.Value = 0 Then

I tested the following code for the check box's After Update event with your form, and it does what I think you're asking for.

Private Sub chbxInShop_AfterUpdate()
    Dim strFilter As String

    strFilter = vbNullString ' make it explicit
    If Me.chbxInShop.Value = True Then
        strFilter = "[SrvInShop] = True"
    End If
    With Me.[Edit Services SubForm].Form
        .Filter = strFilter
        .FilterOn = Me.chbxInShop.Value
    End With
End Sub

If you later decide you want the subform filter based on the values of all 4 of the form's check boxes, you'll need to examine each of them and create an appropriate Filter string, which could wind up as something like ...

strFilter = "[SrvInShop] = True AND [SrvSmallBusiness] = True"
0

Answer is in the original post. I had mislabeled the SubForm...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.