Avatar of Shawn
Shawn
Flag for Canada asked on

cannot clear filter in subform

I can set a filter from a main form to the subform fine with this:

Me.sbfrmClientSearchCompany.Form.Filter = "[CompanyName] LIKE '*" & Me.search0 & "*' "
Me.sbfrmClientSearchCompany.Form.FilterOn = True

Now on another button I am trying to clear the filter and this isn't clearing it:
Me.sbfrmClientSearchCompany.Form.FilterOn = False

how can I clear the subform?
Microsoft Access

Avatar of undefined
Last Comment
Shawn

8/22/2022 - Mon
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Try this first:
Me.sbfrmClientSearchCompany.Form.Filter = ""
Me.sbfrmClientSearchCompany.Form.FilterOn = False
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

If that doesn't work, you can resort to this, although I really don't like it that well:

Dim sRs As String
sRs = Me.sbfrmClientSearchCompany.Form.Recordsource
Me.sbfrmClientSearchCompany.Form.Recordsource = ""
Me.sbfrmClientSearchCompany.Form.Recordsource = sRs

mx
Shawn

ASKER
your first one worked in a standalone button but it doesn't seem to work with my surrounding code...sorry, I should have posted all the code.

I get the error "can't go to the specified record"

See below:
Private Sub search0_Change()

Me.sbfrmClientSearchContact.Form.Filter = ""
Me.sbfrmClientSearchContact.Form.FilterOn = False

Me.sbfrmClientSearchCompany.Form.Filter = ""
Me.sbfrmClientSearchCompany.Form.FilterOn = False

DoCmd.GoToControl "sbfrmClientSearchCompany"
DoCmd.GoToControl "CompanyName"

If Nz([Forms]![frmClientSearch].search0, "") <> "" Then

Me.sbfrmClientSearchCompany.Form.Filter = "[CompanyName] LIKE '*" & Me.search0 & "*' OR [ClientID] LIKE '*" & Me.search0 & "*' OR [CompanyName] LIKE '*" & Me.search0 & "*'"
Me.sbfrmClientSearchCompany.Form.FilterOn = True

DoCmd.GoToControl "sbfrmClientSearchContact"
DoCmd.GoToControl "ClientID"

Me.sbfrmClientSearchContact.Form.Filter = "[LastName] LIKE '*" & Me.search0 & "*' OR [FirstName] LIKE '*" & Me.search0 & "*'OR [ClientID] LIKE '*" & Me.search0 & "*' "
Me.sbfrmClientSearchContact.Form.FilterOn = True

End If

    
DoCmd.GoToControl "search0"
SendKeys "{F2}"
End Sub

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

What line doe the error occur on?

Also, don't see how this would be related to the filter setting ...

mx
Shawn

ASKER
it happens on this line:
DoCmd.GoToControl "CompanyName"

In the subform window when there are no records, it tries to go to this control and it is not present when the the filter is on. I was trying to shut the filter off before this so records would be present.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

"In the subform window when there are no records, it tries to go to this control and it is not present "
Well, that would cause this error. You can't really go to a control if no records are present on a subform.

Can you rethink the logic ?

mx
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Actually, let try this:

Private Sub search0_Change()

Me.sbfrmClientSearchContact.Form.Filter = ""
Me.sbfrmClientSearchContact.Form.FilterOn = False

Me.sbfrmClientSearchCompany.Form.Filter = ""
Me.sbfrmClientSearchCompany.Form.FilterOn = False

Me.sbfrmClientSearchCompany.SetFocus  ' focus to subform control first
Me.sbfrmClientSearchCompany.Form.CompanyName.SetFocus ' then actual control.



If Nz([Forms]![frmClientSearch].search0, "") <> "" Then

Me.sbfrmClientSearchCompany.Form.Filter = "[CompanyName] LIKE '*" & Me.search0 & "*' OR [ClientID] LIKE '*" & Me.search0 & "*' OR [CompanyName] LIKE '*" & Me.search0 & "*'"
Me.sbfrmClientSearchCompany.Form.FilterOn = True

DoCmd.GoToControl "sbfrmClientSearchContact"
DoCmd.GoToControl "ClientID"

Me.sbfrmClientSearchContact.Form.Filter = "[LastName] LIKE '*" & Me.search0 & "*' OR [FirstName] LIKE '*" & Me.search0 & "*'OR [ClientID] LIKE '*" & Me.search0 & "*' "
Me.sbfrmClientSearchContact.Form.FilterOn = True

End If

   
DoCmd.GoToControl "search0"
SendKeys "{F2}"
End Sub
Shawn

ASKER
still getting the same error here:
Me.sbfrmClientSearchCompany.Form.CompanyName.SetFocus ' then actual control.

maybe a refresh or requery?
Shawn

ASKER
here it is in action
Shawnaraxi-487767.flv
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Shawn

ASKER
that did the trick. thanks MX
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Humm .... kind of odd.  That really should not be necessary, but I've had trouble doing it the other way also.

mx
Shawn

ASKER
yeah, I 've used something similar before without any hitches. strange.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.