Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the tag to hide controls 1

Status
Not open for further replies.

rmd62163

Technical User
Jan 18, 2013
16
US
Little back ground. I have a main form [Patient] and on that form is a [tabctl] and on a tab is the subform [implants]. what I have been trying to accomplish is to hide a set of controls that are tagged with the word femalegrp. I can get this to work on the main form.

Private Sub sex_Change()
Dim ctrl As control
If Me.sex = "male" Then
For Each ctrl In Me.Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = False
End If
Next ctrl

ElseIf Me.sex = "female" Then
For Each ctrl In Me.Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = True
End If
Next ctrl
End If
End Sub


But when I try and do this on the subform with

Private Sub Implants_Enter()
Dim ctrl As control
If Me.sex = "male" Then

For Each ctrl In [Implants].Form.Controls
If ctrl.Tag = "femalegrp" Then
ctrl.Visible = False
End If
Next ctrl

ElseIf Me.sex = "female" Then
For Each ctrl In [Implants].Form.Controls
If ctrl.Tag = "femalegrp" Then
ctrl.Visible = True
End If
Next ctrl

End If
End Sub


I get nothing. not even an error. Any guidance as to the error of my ways I would be grateful.
 
Thanks. will do and let you know what I find out.
 
OK, this is what I found out. Case sensitive to the word in the tag. Live and learn eh!
This is what I ended up with and works perfect. BTW still reading the book on debugging. I'm missing something on that point. I don't suppose it's to difficult but I'm learning.

Private Sub Implants_Enter()
Dim ctrl As control
If Me.sex = "male" Then

For Each ctrl In [Implants].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = False
End If
Next ctrl

ElseIf Me.sex = "female" Then

For Each ctrl In [Implants].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = True
End If
Next ctrl

End If
End Sub
 
Case sensitive to the word in the tag
To avoid such problem use something like this:
If LCase(ctrl.Tag) = "femalegrp" Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That work good also PHV, aside from checking my spelling that is. Thanks for the good tip I will be sure to use it. But now I found another bug. If my last click or focus was on one of the controls it errors with the (you can't hide a control that has focus) [mad]
 
setting focus to another control on the same subform cleared that error.[bugeyed]
 
Personal preference, but you could also do:
[tt]
If UCase(ctrl.Tag) = UCase("femaleGrp") Then
[/tt]
Spelling still counts... :)

Have fun.

---- Andy
 
some more guidance in reference to this thread. Is it possible to reference two different sub form controls on the (For Each ctrl In) line? Not even sure how to start this. Or if I need to go to a different method.

Private Sub Form_Current()
Dim ctrl As control
If Me.Gender = "Male" Then
[Implants]![Patient_ID].SetFocus

For Each ctrl In [Implants].Controls (can I reference the other controls on the 2nd subform from here?)
If ctrl.Tag = "femaleGrp" Then

ctrl.Visible = False
End If
Next ctrl

ElseIf Me.Gender = "Female" Then

For Each ctrl In [Implants].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = True
End If
Next ctrl

End If
End Sub

Thank you for any input!
 
Have you tried:

Code:
For Each ctrl In [[blue]OtherLoadedForm[/blue]].Controls
   ...

???

Have fun.

---- Andy
 
Andy,
I have. I should of put down that I want to reference the 2 different controls at the same time using the
(for each ctrl in)on the same line. like For Each ctrl In [Form1].Controls, [Form2].Controls. Not sure how to do that or if it's possible. Should I continue to pursue this approach or take a different path to accomplish the end result of hiding all controls on two different subforms that have the specific tag value?
 
Use 2 For Each loops.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, Do you mean something like below or am I missing the obvious. That does do what I want. But I was thinking that there would be a shorter way to the goal.

Private Sub Form_Current()
Dim ctrl As control
[Implants]![Patient_ID].SetFocus

If Me.Gender = "Male" Then
For Each ctrl In [Implants].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = False
End If
Next ctrl

ElseIf Me.Gender = "Female" Then

For Each ctrl In [Implants].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = True
End If
Next ctrl

End If

If Me.Gender = "Male" Then
For Each ctrl In [Medical].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = False
End If
Next ctrl

ElseIf Me.Gender = "Female" Then

For Each ctrl In [Medical].Controls
If ctrl.Tag = "femaleGrp" Then
ctrl.Visible = True
End If
Next ctrl

End If
End Sub
 
What about this ?
Code:
Private Sub Form_Current()
Dim ctrl As control
[Implants]![Patient_ID].SetFocus
For Each ctrl In [Implants].Controls
  If ctrl.Tag = "femaleGrp" Then
    ctrl.Visible = (Me.Gender = "Female")
  End If
Next ctrl
For Each ctrl In [Medical].Controls
  If ctrl.Tag = "femaleGrp" Then
    ctrl.Visible = (Me.Gender = "Female")
  End If
Next ctrl
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
To start with sorry about all the bold formatting, work computer won't run scripts. All I can say is I need to read many more books on VBA! Your code does the job perfect. But if I may, can I pick your thoughts on it? I can follow it thru to ctrl.Visible = (Me.Gender = "Female"). Is this stating that control is visible if Me.Gender = Female otherwise control is hiding or am I wrong? And HOW THE *$*^%* long have you been doing this?
Thank you! I applaud your knowledge my friend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top