help with vba with if statement

krowe

Registered User.
Local time
Today, 05:53
Joined
Mar 29, 2011
Messages
159
Hi

I'm a novice when it comes to vba.

I want to put a button on a form that removes any filter applied to it.

I know the on click needs to be me.filter=""

however, the next step is i only want the button visible if there is a filter applied.

i want to put some code in the onload event of the form.

I think it is something like this, but cant figure out exactly what i need:

If Me.Filter = "" Then Me.btnUnFilter.Visible = False Else Me.btnUnFilter.Visible = True
End If

Please can you advise where im going wrong.

Thanks in advance
 
The string in me.filter holds the where part of the SQL, it has noting to do with the filter being applied. Your code should use the filteron property.

Code:
if me.filteron then
   Me.btnUnFilter.Visible = True
else 
   Me.btnUnFilter.Visible = False
end if

This is only usefull on the onload event if the form is opened with a filter applied. If filters are applied after the form is opened your button won't become visible unless you use the code in the OnApplyFilter event.
 
Because the visibility is either true or false, and the filter is either there or not, then we can say that the visibility varies directly with the presence of the filter. So we don't need an if block, we can do this ...
Code:
[COLOR="Blue"]btn.visible[/COLOR] = [COLOR="Red"]me.filter <> ""[/COLOR]
...and what's going on there is that the red expression is evaluated and the result is assigned to the blue property. So if the filter is not absent ...
Maybe it is clearer to say...
Code:
[COLOR="Blue"]btn.visible[/COLOR] = [COLOR="Red"]Len(me.filter) > 0[/COLOR]
- or -
[COLOR="Blue"]btn.visible[/COLOR] = [COLOR="Red"]NOT me.filter = ""[/COLOR]
 
Thanks for the quick replies

Peters code seemed to do the trick. I could not get lagbolt's to work though - sorry lag, probably applying it wrong.

Thanks again, much appreciated
 
Lagbolts code uses the filterstring as your original code did, you don't know if a filter is applied. I like the way lagbolts coding should work. So with the correct form property it looks like:

Code:
Me.btnUnFilter.Visible = me.FilterOn
 
thats great, got that working too.


put it on the oncurrent event and it works when i apply any filter, which is an added bonus

Thanks for all your help
 

Users who are viewing this thread

Back
Top Bottom