isladogs
MVP / VIP
- Local time
- Today, 01:33
- Joined
- Jan 14, 2017
- Messages
- 18,722
As I'm sure many of you will already know it is easy to change a selected group of form controls to be visible or hidden with one line of code using the controls' Tag property.
For those who don't know how, add code like this to the form:
In the Form_Load event, add code to set the start up properties like this:
Much quicker than writing e.g.
When you need to change something, add code like this:
However, it would be good to be able to to do a similar things to set a group of controls as enabled / disabled.
I've tried the following :
with related code
However, it doesn't work.
Anyone know a way of doing it?
For those who don't know how, add code like this to the form:
Code:
Private Sub SetControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)
On Error GoTo Err_Handler
'set controls to visible or not according to the control tag value
For Each ctrl In Me.Controls
If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.visible = State
Next ctrl
Exit_Handler:
Exit Sub
Err_Handler:
'create error message & log
strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
PopulateErrorLog
Resume Exit_Handler
End Sub
In the Form_Load event, add code to set the start up properties like this:
Code:
SetControls True, "A"
SetControls False, "E", "S", "Q", "P", "V"
SetControls False, "F", "FB", "R", "FR", "RB"
Much quicker than writing e.g.
Code:
Me.ctl1.Visible=True
Me.ctl2.Visible=False
Me.ctl3.Visible=True
Me.ctl4.Visible=False
Me.ctl5.Visible=False
Me.ctl6.Visible=False
Me.ctl7.Visible=True
When you need to change something, add code like this:
Code:
SetControls True, "E", "S"
SetControls False, "A"
However, it would be good to be able to to do a similar things to set a group of controls as enabled / disabled.
I've tried the following :
Code:
Private Sub EnableControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)
On Error GoTo Err_Handler
'set controls to visible or not according to the control tag value
'doesn't work
For Each ctrl In Me.Controls
If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.enabled = State
Next ctrl
Exit_Handler:
Exit Sub
Err_Handler:
'create error message & log
strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
PopulateErrorLog
Resume Exit_Handler
End Sub
with related code
Code:
EnableControls True, "A", "N"
EnableControls False, "B", "C"
However, it doesn't work.
Anyone know a way of doing it?