Hide Controls w/ VBA!

xxx

Registered User.
Local time
Yesterday, 21:16
Joined
Apr 29, 2011
Messages
41
So I have several main forms which contain the same subform. This one subform contains ~4 Controls / Labels that are only necessary for one of the main forms. I'd like to hide them in all instances except when the main form is named "x". I think hiding them is most efficient. Database needs to be done by Friday.

I've explored several variations of the below code with no effect what so ever. I've tried calling on the subform more specifically. (i.e. [Forms]![Main Form]![Subform]![Control Name].Visible = False)

Also, where is the best place to execute this code. Should it work in this general format or do I need to specifically "call" on the Sub? For example, OnOpen, etc. Please, forgive me! have been experimenting and teaching myself as I go, but it's crunch time now :( Thank you!

Private Sub HideHeight()
On Error Resume Next
For Each frm In Forms
If frm.Name <>"Baseline" Then
Height.Visible = False
Height_Label.Visible = False
Label41.Visible = False
Weight.Visible = False
Weight_Label.Visible = False
Label42.Visible = False
Label39.Visible = False
BMI.Visible = False
Else
Height.Visible = True
Height_Label.Visible = True
Label41.Visible = True
Weight.Visible = True
Weight_Label.Visible = True
Label42.Visible = True
Label39.Visible = True
BMI.Visible = True
Next
End Sub
 
Last edited:
Maybe an additional observation to note:
In other instances in this database when my "action items" are placed below the If statement, following a For Statement, nothing I want to happen happens.

WORKS:
"For Each frm In Forms
If frm.Name <>"meh" Then blah blah blah"

DOESN'T WORK:
"For Each frm In Forms
If frm.Name <>"meh" Then
blah blah blah"


Side note: :banghead: Bahahahah! So fitting!
 
Regarding your first post, it should be frm.YourControlName.Propertyname=Value.

Regarding your second post, have you tried to compact or make a new shell?
 
Regarding your first post, it should be frm.YourControlName.Propertyname=Value.

Regarding your second post, have you tried to compact or make a new shell?


I tried your suggestion. Still no effect. Does it matter that the control is on a subform?

Regarding your second comment, I am not familiar with either. Would have to look into it.
 
Thank you though! Your suggestion did help. I put it with the first Tab Index Event, and just repeated the condition for each control.


Private Sub Text51_GotFocus()
On Error Resume Next
For Each frm In Forms
If frm.Name = "Daily" And Len([Forms]![Daily]![Combo139]) > 6 Then [Forms]![Daily]![Combo139].SetFocus
If frm.Name <> "x" Then Height.Visible = False
If frm.Name <> "x" Then Height_Label.Visible = False
If frm.Name <> "x" Then Label41.Visible = False
If frm.Name <> "x" Then Weight.Visible = False
If frm.Name <> "x" Then Weight_Label.Visible = False
If frm.Name <> "x" Then Label42.Visible = False
If frm.Name <> "x" Then Label39.Visible = False
If frm.Name <> "x" Then BMI.Visible = False
Next
End Sub
 
Why can't you have one IF... END IF block with all your property code change within?
 
My understanding is that you have a subform that you use in a variety of parent forms, and you want to customize the behavior of the subform for each parent.

- If that's correct then apply customizations to the subform in its open or load events, as each of these will fire once when the subform first opens. Customizations you make at that time will persist for the lifetime of that instance of the subform.
- In that process, check the name of the parent of the subform (Me.Parent.Name), and take action as required, and it will never be required to traverse the whole Forms collection, which is global to the application. You are only concerned about one form: the parent of the subform.
- If you hide a textbox with an attached label the label hides automatically, so it's unlikely that you need to show/hide all the controls AND their labels. Start with just the parent control and see if that works.
- Finally, visible is a boolean property so to set its value you can assign to it the value of an expression.

Bringing this all together, consider the following code, which would be native to the subform,....
Code:
Private Sub Form_Open(Cancel As Integer)
On Error Goto handler
  dim tmp as string
[COLOR="Green"]  'get the name of the parent of this instance of the subform, fails if opened stand-alone[/COLOR]
  tmp = me.parent.name
[COLOR="Green"]  'show the height control when parent form is Form1 OR Form2, and hide of not[/COLOR]
  me.height.visible = (tmp = "Form1") or (tmp = "Form2")
[COLOR="Green"]  'only show the weight control when parent is Form3, otherwise hide it[/COLOR]
  me.weight.visible = tmp = "Form3"
  exit sub
handler:
  cancel = true
  msgbox "This form is designed as a subform only.", vbCritical
End Sub

Does that makes sense?
Mark
 
I appreciate this is an old (VERY old) post, but it popped up whilst I was searching.

Eventually I found this solution to set any attribute of all controls of the same type...

Code:
Dim cCont As Control
  For Each cCont In Me.Controls
   Select Case TypeName(cCont)
      Case Is = "Page"
           cCont.Visible = False
      Case Is = "TextBox"
           cCont.Visible = False
           cCont.Value = ""
      Case Is = "Label"
           cCont.Visible = False
      End Select
  Next cCont

and then if you want to make any particular exception to the rule, after this For/Next, you re-specify

Code:
txtSearch.Visible = True
lblTitle.Visible = True

.. etc :)
 

Users who are viewing this thread

Back
Top Bottom