SteveL13
asked on
How hide fields on a form if their tag value is set to "Hide"
I have several fields on a form that have tag set to "Hide" (without the quote marks)
If the selected value in a combobox on the form is <> 2 then I want all those fields to be hidden.
Can this be done?
If the selected value in a combobox on the form is <> 2 then I want all those fields to be hidden.
Can this be done?
This is code from one of my apps that locks/unlocks controls based on security. I didn't alter it to suit your purpose because you might find it useful for its original function. Use the same concept to hide/show. To call this code use
Call LockControls (Me, True)
In your case instead of the boolean, use an integer if you prefer and instead of locking the control hide or show it.
ctl.Visible = True 'to show
or
ctl.Visible = False ' to hide
Call LockControls (Me, True)
In your case instead of the boolean, use an integer if you prefer and instead of locking the control hide or show it.
ctl.Visible = True 'to show
or
ctl.Visible = False ' to hide
Public Sub LockControls(frm As Form, bLock As Boolean)
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
Select Case ctl.Tag
Case "NoLock"
ctl.Locked = False
Case "Lock"
ctl.Locked = True
Case Else
ctl.Locked = bLock 'toggle locks
End Select
Case acOptionGroup, acOptionButton, acCheckBox ''not working
Select Case ctl.Tag
Case "NoLock"
ctl.Locked = False
Case "Lock"
ctl.Locked = True
Case Else
ctl.Locked = bLock 'toggle locks
End Select
Case acCommandButton
Select Case ctl.Tag
Case "NoLock"
ctl.Enabled = True
Case "Lock"
ctl.Enabled = False
Case Else
ctl.Enabled = Not bLock 'toggle locks
End Select
End Select
Next ctl
Set ctl = Nothing
End Sub
ASKER
Neither suggestion seem to be working. I'm sure I am doing something wrong. If I change the combobox selection to something other than 2 the fields are hidden which is what I would expect. But if I change it back to 2 they are still hidden.
Here is what I have in the after update event of the combobox:
And here is the code being called:
Here is what I have in the after update event of the combobox:
If Me.cboPrinterID2 = 2 Then
Call HideControls(Me, True)
End If
And here is the code being called:
Public Sub HideControls(frm As Form, bLock As Integer)
Dim ctl As Control
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox ', acComboBox, acListBox, acCheckBox
Select Case ctl.Tag
Case "Hide"
ctl.Visible = False
Case Else
ctl.Visible = True 'toggle locks
End Select
Case acLabel ', acComboBox, acListBox, acCheckBox
Select Case ctl.Tag
Case "Hide"
ctl.Visible = False
Case Else
ctl.Visible = True 'toggle locks
End Select
Case acCommandButton ', acComboBox, acListBox, acCheckBox
Select Case ctl.Tag
Case "Hide"
ctl.Visible = False
Case Else
ctl.Visible = True 'toggle locks
End Select
Case acLine ', acComboBox, acListBox, acCheckBox
Select Case ctl.Tag
Case "Hide"
ctl.Visible = False
Case Else
ctl.Visible = True 'toggle locks
End Select
End Select
Next ctl
Set ctl = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Pat, I'm getting an error "You can't hide a control that has the focus".
Dale: I had to change the <> to = and then it worked.
Dale: I had to change the <> to = and then it worked.
You should get the same error with Dale's code. As the first step in the procedure, move the focus to some control you know you are not going to hide.
ASKER
Pat, I will try that but am definitely not getting the error with Dale's code. Back to you soon.
ASKER
Pat. I just tried setting the focus to a control that I know should not become hidden and all controls disappeared.
It is doing the same thing. The pieces of code are equivalent. I just gave you code I had for a different use to repurpose for hiding rather than locking. Dale wrote the code from scratch. Put the cursor in the same field before running each procedure. You should get the same error.
ASKER
Pat,
I can see what you have proposed but I still get same result with your solution. All fields disappear when I select 2 from the combobox.
I can see what you have proposed but I still get same result with your solution. All fields disappear when I select 2 from the combobox.
ASKER
And here is the after update event:
And here is the code:
If Me.cboPrinterID2 = 2 Then
Call HideControls(Me, True)
Else
Call HideControls(Me, False)
End If
And here is the code:
Public Sub HideControls(frm As Form, bLock As Boolean)
Dim ctl As Control
Me.cmdCloseForm.SetFocus
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
ctl.Visible = bLock 'toggle Visible
End Select
Next ctl
Set ctl = Nothing
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Pat,
That did it. Thanks for the patience.
That did it. Thanks for the patience.
Open in new window