if-then-else to modify property of several controls

H

Home Again

Using an if-then-else statement and want to be able to make several things
visible=false (attendance for all months past the end of the chosen term)

To change only one month, the statement follows. Is it possible to set
several?

Private Sub Report_Activate()
'hide attendance based on asofdate

If Me.asofdate <= Me.Term1end Then
Me.November.Visible = False 'I want this to be November, December,
January,February, March,April,may,June
ElseIf Me.asofdate <= Me.term2end Then
Me.February.Visible = False 'I want this to be February, March, April, may,
June
ElseIf Me.asofdate <= Me.term3end Then
Me.April.Visible = False 'I want this to be April, May, June
Else: Me.september.visible = True 'to do nothing for fourth term
End If

End Sub
 
W

Wolfgang Kais

"Home Again".

Home Again said:
Using an if-then-else statement and want to be able to make several
things visible=false (attendance for all months past the end of the
chosen term)

To change only one month, the statement follows. Is it possible to
set several?

Private Sub Report_Activate()
'hide attendance based on asofdate

If Me.asofdate <= Me.Term1end Then
Me.November.Visible = False 'I want this to be November, December,
'January,February, March,April,may,June
ElseIf Me.asofdate <= Me.term2end Then
Me.February.Visible = False 'I want this to be February, March,
'April, may, June
ElseIf Me.asofdate <= Me.term3end Then
Me.April.Visible = False 'I want this to be April, May, June
Else: Me.september.visible = True 'to do nothing for fourth term
End If

End Sub

I wonder is
f the Activate event is the correct one, I would probably have chosen
the format event of the section. Probably you are looking for this:

Dim varMonth as Variant

If Me.asofdate <= Me.Term1end Then
For Each varMonth In Array(11, 12, 1, 2, 3, 4, 5, 6)
Me.Controls(MonthName(varMonth)).Visible = False
Next
ElseIf Me.asofdate <= Me.term2end Then
For Each varMonth In Array(2, 3, 4, 5, 6)
Me.Controls(MonthName(varMonth)).Visible = False
Next
ElseIf Me.asofdate <= Me.term3end Then
For Each varMonth In Array(4, 5, 6)
Me.Controls(MonthName(varMonth)).Visible = False
Next
Else
Me.September.visible = True
End If

Check the months: There are only 2 Months between term 2 and 3.
 
G

George Nicholson

Here's one approach.

Note that if i = 1 the following 4 lines are equivalent.
Me.Controls(Format(i,"mmmm")).Visible = False
Me.Controls("January").Visible = False
Me.January.Visible = False
Me.Controls(MonthName(i,False)).Visible = False

Select Case Me.AsOfDate
Case <= Me.Term1end
For i = 1 to 12
Select case i
Case 1,2,3,4,5,6,11,12
Me.Controls(Format(i,"mmmm")).Visible = False
Case Else
' Do nothing
End Select
Next i
Case <= Me.Term2end
For i = 2 to 6
Me.Controls(Format(i,"mmmm")).Visible = False
Next i
Case <= Me.Term3end
For i = 4 to 6
Me.Controls(Format(i,"mmmm")).Visible = False
Next i
Case Else
'Do nothing
End Select
 
E

Evi

You could use the Tag property of the Control for this

Next to Tag in their Properties type 1 if the control is called November to
Feb and 2 if the control is March to June and 3 if they are July to October

Your code will look someting like this

Dim Ctl As Control

Select Case Me.AsOfDate

Case is <=Me.Term1End

For Each ctl In Me.Section(0).Controls
'replace Section(0) with the number
'of the section that has the controls
If Ctl.Tag = 1 OR Ctl.Tag = 2 Then
Ctl.Visible = False
If Ctl.Tag = 3 Then
Ctl.Visible = True
End If
End If
Next

Case is <= Me.Term2End

For Each ctl In Me.Section(0).Controls
'replace Section(0) with the number
'of the section that has the controls
If Ctl.Tag = 2 OR Ctl.Tag = 3 Then
Ctl.Visible = False
If Ctl.Tag = 2 Then
Ctl.Visible = True
End If
End If
Next

Case Is <= Me.term3end

For Each ctl In Me.Section(0).Controls
'replace Section(0) with the number
'of the section that has the controls
If Ctl.Tag = 3 OR Ctl.Tag = 2 Then
Ctl.Visible = False
If Ctl.Tag = 1 Then
Ctl.Visible = True
End If
End If
Next

Case Else
'do nothing means you want them all visible?
For Each ctl In Me.Section(0).Controls
'replace Section(0) with the number
'of the section that has the controls
Ctl.Visible = True
'or false if that is what you want
Next

End Select


Evi
 
H

Home Again

This is pretty much what I ended up doing

If Me.asofdate <= Me.Term1end Then
For i = 1 To 12
Select Case i
Case 11, 12, 1 To 6
Me(MonthName(i)).Visible = False
End Select
Next i
ElseIf Me.asofdate <= Me.term2end Then
For i = 1 To 6
Me(MonthName(i)).Visible = False
Next i
ElseIf Me.asofdate <= Me.term3end Then
For i = 4 To 6
Me(MonthName(i)).Visible = False
Next i
Else:
'do nothing
End If

Got other tricky ones though
 

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

Similar Threads

If Then Else 3
if then else 8
Ranges of dates 2
If Then Else statment Help 5
If-Then-Else Statement 2
If-Then-Else Statement 7
If Then Else looping problem 1
adding subsequent Quarters to start of year 3

Top