Full Version: Hide label if text box = "1"
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
Jprogrammer
I would like to hide a label if a result in a text box is = "1". I thought I could do this via the conditional formatting but looks like I can't add that to a label. Any ideas or alteratives?

Thanks for any help!
R. Hicks
Use the On Format event of the section where the label resides ...

Me.LabelName.Visible = Me.TextboxName <> 1

RDH
niesz
If Me.YourTextBox = "1" Then
Me.YourLabel.Visible = False
Else
Me.YourLabel.Visible = True
End If

You may have to play with what event this is associated with on a report.
freakazeud
Hi,
use the forms on current event or whatever other event you want:

If Me.YourControl.Value = 1 Then
Me.YourLable.Visible = False
Else
Me.YourLabel.Vlisible = True
End If

HTH
Good luck

Opps...just saw that this is report related frown.gif so use either ricky's method or add an event to the details section.
R. Hicks
Toggle a Boolean vaule with another Boolean value ...
No need for the If/Else/End If ...

RDH
R. Hicks
Again .. no need for the If/Else/End If ...

RDH
freakazeud
I know that method is possible, but I just never find myself using it!
Maybe I have too much time on mind hand to write out the whole if then else frown.gif
R. Hicks
LOL ... this method is overlooked my many people ...

RDH
freakazeud
"overlooked" or ignored?
R. Hicks
Could be either .. I suspect ..

RDH
niesz
I posted it that way because there are some 'unknowns' in the OPs question. It was never stated whether the textbox is a string or a value, or what the other possibilities of the textbox could be.

But you're right. It can be done more efficiently if certain things are true. Personally, I would use a hidden checkbox to negate the need for the "<>".
freakazeud
Well...I'm glad we discussed this frown.gif
Happy Holidays!
R. Hicks
QUOTE
I would use a hidden checkbox to negate the need for the "<>"

I can't agree to that solution ..
Even if the value is a string .. it can easily be converted to a numeric value or alter the expression to handle the string value ...

Me.LabelName.Visible = Me.TextboxName <> "1"

RDH

Edited by: R. Hicks on Mon Dec 19 11:17:57 EST 2005.
R. Hicks
LOL ... grin.gif

RDH
niesz
I still think there are too many unknows to make a definitive statement as to which method is more efficient. Is the textbox on a bound or unbound form? Can the textbox value itself be rewritten to allow for more reliable code? Is the checkbox's only purpose to toggle the label?

I hate nitpicking on threads like this. My only point was we didn't have enough info to agree. Given the full circumstance, we would probably come to the same conclusion.
R. Hicks
Nothing "nitpicking" about this ... and I find your reply somewhat hostile ...
You should not post a reply if you don't want anyone to post a more efficient and shorter solution.
There is no need to use an If/Else/End If that requires a boolean output when evaluating 2 boolean values ...

RDH
niesz
Ricky, I didn't mean to come across as hostile, and do apologize for that. Evaluating boolean to boolean is definately more efficient. We can both agree to that. If I sounded like this was the intent of my reply, I did not mean it to be.

What it was meant to say was there may be other ways to further improve the efficiency if more details were provided.

Hope I didn't overstep anything, here. It certainly wasn't my intent. I also hope the original poster was not offended by our conversation. Nor was it my intent to infer that they did anything wrong.

Again, I apologize.
R. Hicks
Had me worried there for a second ...

Of couse the If/Else/End If solution will return the same result .. my point was only that it is not needed in this case.
We are here to provide correct solutions to problems being posted .. and in this case .. both solutions are correct and acceptable.
I just think that the shorter, less coding solution would be better.

RDH
niesz
I agree.
freakazeud
QUOTE
Is the textbox on a bound or unbound form?

I thought this was concerning a report issue?
niesz
It was never stated where the textbox was.
freakazeud
Well..but the thread is in the reports forum so I would assume it is on a report!
niesz
It may be...
Jprogrammer
Thanks everyone for the great info & interested discussion! To answer one question, the text box is located on a report not a form.

I went with this option:

CODE
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

If Me.txtType.Value = "OSHA" Then
Me.lblDOC_CPR.Visible = False
Else
Me.lblDOC_CPR.Visible = True
End If

End Sub


This works great! Now I'm trying to hide two labels if the same text box in question is = "2". I'm trying to figure out how to add two labels to the same "If" line? So basically to add to what I already have, would I do something like this??:

CODE
If Me.txtType.Value = 1 Then
Me.lblCPR.Visible = False
If
txtType.Value = 2 Then
Me.lbl10 and lbl30.Visible = False
End If

End Sub
R. Hicks
Try using a Case Select statement ..
You can then use as many variables as ytou want by add a "Case" ...
CODE
Select Case Me.txtType

  Case 1

    Me.lblCPR.Visible = False

  Case 2

    Me.lbl10.Visible False

    Me.lbl30.Visible = False

End Select

Note .. The above example is assuming that "Me.txtType" is returning a numeric value

RDH
Jprogrammer
Thanks! What if I have a text value? I have 2 reports that I want to use this with, one returns a numeric & the other returns text.

Thanks again!!!
R. Hicks
If the value being evaluated is a string (text) .. them enclose the values to evaluate within quotes.

Here is the same example as a text variable:
CODE
Select Case Me.txtType
  Case "ABC"
    Me.lblCPR.Visible = False
  Case "XYZ"
    Me.lbl10.Visible False
    Me.lbl30.Visible = False
End Select

This should show you the difference ...

RDH
Jprogrammer
Thanks so much!!! This works great. notworthy.gif
R. Hicks
No problem .. you are welcome .. wink.gif

RDH
Jprogrammer
After playing with this more I am wondering if it is possible to do this- When txtType is = 1 hide lblCPR but SHOW ALL OTHER LABELS (lbl10 and lbl30) and when txtType is = 2 hide lblCPR but SHOW lbl10 and lbl30. Is this possible? confused.gif
R. Hicks
Try ....
CODE
Select Case Me.txtType
  Case 1
    Me.lblCPR.Visible = False
    Me.lbl10.Visible =True
    Me.lbl30.Visible = True
  Case 2
    Me.lblCPR.Visible = True
    Me.lbl10.Visible = False
    Me.lbl30.Visible = False
End Select

RDH
Jprogrammer
Once again my friend... notworthy.gif

Works great! Thanks so much & Happy Holidays!!!
R. Hicks
And ... Happy Holidays to you .. wink.gif

RDH
Jprogrammer
I am loving this feature where I can hide a label if the text box doesn't meet a certain criteria!

Right now I have a report that the text box is sometimes empty or Null. I wrote this:

CODE

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.PMPager.Value = ""
Then Me.Label13.Visible = False
Else: Me.Label13.Visible = True
End If

End Sub


But I'm getting an error with the "". I thought this meant the same as Is Null ???
freakazeud
No it means a zero length string. There is a difference between that and isnull!
Have a look at this earlier discussion!
There are many ways to ensure a control is actual NULL.
HTH
Good luck
Jprogrammer
Thanks! I'm not getting an error now but it's not working... crazy.gif ????

CODE
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Nz(Me.PMPager.Value, "") = "" Then Me.Label13.Visible = False
Me.Label13.Visible = True
End Sub
freakazeud
Aren't you missing the else part?
HTH
Good luck
Jprogrammer
Yeah you are right! SHEESH!!!

But now I'm getting a compile error saying:
QUOTE
Else without If


CODE
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Nz(Me.txtPMPager.Value, "") = "" Then Me.lblPager.Visible = False
Else
Me.lblPager.Visible = True
End If
End Sub


ARG!!! crazy.gif (by the way, I renamed my labels and text boxes.)

Thanks for your help man!!!!!!
freakazeud
Try to move Me.lblPager.Visible = False on a new line:

If Nz(Me.txtPMPager.Value, "") = "" Then
Me.lblPager.Visible = False
Else
Me.lblPager.Visible = True
End If

Also make sure you have no other if statements anymore anywhere.
HTH
Good luck
Jprogrammer
I also want to add another label and text box to this. Would I write it like this?

CODE

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Nz(Me.txtPMPager.Value [color="red"] and txtSuperPager [/color] , "") = "" Then Me.lblPager [color="red"] and lblPagerSupers[/color].Visible = False
Else
Me.lblPager [color="red"]and lblPagerSupers[/color].Visible = True
End If
End Sub
freakazeud
No you need to evaluate this seperatly:

If Nz(Me.txtPMPager, "") = "" OR Nz(Me.txtSuperPager, "") Then
Me.lblPager.Visible = False
Me.lblPagerSupers.Visible = False
Else
Me.lblPager.Visible = True
Me.lblPagerSupers.Visible = True
End If

HTH
Good luck
Jprogrammer
I'm not receiving an error now but it's not working. cryhard.gif
I'm sorry to bother you so much with this!!!

There is no more if statements at all in this report. Here is EVERYTHING that is in my report:

CODE
  

Option Compare Database
Option Explicit
_________________________________________________________
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Nz(Me.txtPMPager.Value, "") = "" Then
Me.lblPager.Visible = False
Else
Me.lblPager.Visible = True
End If
End Sub
freakazeud
Is txtPMPager really Null or empty?
It should work as the code seems to be correct if your controls are the names you assigned there.
HTH
Good luck
Jprogrammer
Now I'm getting an error when I try to view the report saying: Run-time error '13': Type MisMatch

Basically not all of our PM's have pager's so sometimes the textbox will have the pager number in it and other times it will not. Does that make sense??
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.