A Better Way

D

DS

Is There a better way to write this......

For Each ctl In Me.Controls
If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI" Then
ctl.Visible = False
ElseIf ctl.Tag = "MG" Then
ctl.Visible = False
ElseIf ctl.Tag = "IM" Then
ctl.Visible = False
End If
Next ctl

instead of the else if's

This didn't work.

If ctl.Tag = "MS" Then
ctl.Visible = True
Else:
ctl.Visible = False
End If

Neither did this.

If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI","MG","IM" Then
ctl.Visible = False
End If

Thanks
DS
 
A

Arno R

DS said:
Is There a better way to write this......

For Each ctl In Me.Controls
If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI" Then
ctl.Visible = False
ElseIf ctl.Tag = "MG" Then
ctl.Visible = False
ElseIf ctl.Tag = "IM" Then
ctl.Visible = False
End If
Next ctl

instead of the else if's

This didn't work.

If ctl.Tag = "MS" Then
ctl.Visible = True
Else:
ctl.Visible = False
End If

Neither did this.

If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI","MG","IM" Then
ctl.Visible = False
End If

Thanks
DS

Have you tried?
For Each ctl In Me.Controls
Select Case ctl.Tag
Case "MS"
ctl.Visible = True
Case else
ctl.Visible = False
End Select
Next ctl

Also how did you write down the tag value in the properties?
Only write MS (without quotes), not "MS".

Hth
Arno R
 
D

DS

Arno said:
Have you tried?
For Each ctl In Me.Controls
Select Case ctl.Tag
Case "MS"
ctl.Visible = True
Case else
ctl.Visible = False
End Select
Next ctl

Also how did you write down the tag value in the properties?
Only write MS (without quotes), not "MS".

Hth
Arno R
No I haven't I'll give it a try!
Thanks
DS
 
J

John Vinson

Is There a better way to write this......

For Each ctl In Me.Controls
If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI" Then
ctl.Visible = False
ElseIf ctl.Tag = "MG" Then
ctl.Visible = False
ElseIf ctl.Tag = "IM" Then
ctl.Visible = False
End If
Next ctl

You were close: try

If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI" OR ctl.Tag ="MG" OR ctl.Tag "IM" Then
ctl.Visible = False
End If

Alternatively, a Select Case might be useful:

Select Case ctl.Tag
Case "MS"
ctl.Visible = True
Case "MI", "MG", "IM"
ctl.Visible = False
Case Else
' do nothing
End Select

John W. Vinson[MVP]
 
D

DS

John said:
You were close: try

If ctl.Tag = "MS" Then
ctl.Visible = True
ElseIf ctl.Tag = "MI" OR ctl.Tag ="MG" OR ctl.Tag "IM" Then
ctl.Visible = False
End If

Alternatively, a Select Case might be useful:

Select Case ctl.Tag
Case "MS"
ctl.Visible = True
Case "MI", "MG", "IM"
ctl.Visible = False
Case Else
' do nothing
End Select

John W. Vinson[MVP]
The first one worked well. Meaning it was the least amount of code!
Thanks John.
And Arno
 
R

Rob Oldfield

DS said:
The first one worked well. Meaning it was the least amount of code!
Thanks John.
And Arno

....even less code....

for each ctl in me.controls
ctl.visible=(ctl.tag="MS")
next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top