nested if/elseif statements 
Author Message
 nested if/elseif statements

I have a set of controls that I want to show or hide
depending on several conditions. Another newsgroup member
was kind enough to get me started with the dim/for/next
statements, but I can't seem to get the nested if/elseif
statements to work properly..

Essentially, I need to check if ShowHPAggregateHeader is =
or > 1. If it's =1 then it should show all controls with
tag='hideagg' when detail counter JustOne=1, and hide
those controls for all other values of JustOne. If
ShowHPAggregateHeader is >1, the the controls should only
be visible if JustOne=3.

At present, if ShowHPAggregateHeader>1, it shows the
controls for all values of JustOne.  If
ShowHPAggregateHeader=1, it hides the controls for all
values of JustOne..

Am I nesting the if/elseif statements incorrectly? Is
there a better way to do this?

<begin code snip>
Dim ctl As Control
For Each ctl In Me.Controls
    If (Me.ShowHPAggregateHeader > 1) Then
        If ctl.Tag = "Hideagg" And (Me.JustOne = 3) Then
ctl.Visible = True
        ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 3)
Then ctl.Visible = False
    ElseIf (Me.ShowHPAggregateHeader = 1) Then
        If ctl.Tag = "Hideagg" And (Me.JustOne = 1) Then
ctl.Visible = True
        ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 1)
Then ctl.Visible = False
    End If
Next ctl



Tue, 08 Feb 2005 03:09:57 GMT  
 nested if/elseif statements
Hi Carter,

You where missing two Endif statements. When nesting make sure you follow
the indentation conventions. This doesn't affect the correctness of the code
but it makes it a lot easier to understand what you are doing. Remember that
each If ...Elseif block must end with an Endif. Keep each block at the same
indentation level. I corrected your code and have included it below.
Indenting it enabled me to find the missing Endifs:

For Each ctl In Me.Controls
    If (Me.ShowHPAggregateHeader > 1) Then
        If ctl.Tag = "Hideagg" And (Me.JustOne = 3) Then
            ctl.Visible = True
        ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 3)  Then
            ctl.Visible = False
        ElseIf (Me.ShowHPAggregateHeader = 1) Then
            If ctl.Tag = "Hideagg" And (Me.JustOne = 1) Then
                ctl.Visible = True
            ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 1) Then
                ctl.Visible = False
            End If
        Endif
    Endif
Next ctl

I hope this helps.

Cheers,
Jerome Smith



Quote:
> I have a set of controls that I want to show or hide
> depending on several conditions. Another newsgroup member
> was kind enough to get me started with the dim/for/next
> statements, but I can't seem to get the nested if/elseif
> statements to work properly..

> Essentially, I need to check if ShowHPAggregateHeader is =
> or > 1. If it's =1 then it should show all controls with
> tag='hideagg' when detail counter JustOne=1, and hide
> those controls for all other values of JustOne. If
> ShowHPAggregateHeader is >1, the the controls should only
> be visible if JustOne=3.

> At present, if ShowHPAggregateHeader>1, it shows the
> controls for all values of JustOne.  If
> ShowHPAggregateHeader=1, it hides the controls for all
> values of JustOne..

> Am I nesting the if/elseif statements incorrectly? Is
> there a better way to do this?

> <begin code snip>
> Dim ctl As Control
> For Each ctl In Me.Controls
>     If (Me.ShowHPAggregateHeader > 1) Then
>         If ctl.Tag = "Hideagg" And (Me.JustOne = 3) Then
> ctl.Visible = True
>         ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 3)
> Then ctl.Visible = False
>     ElseIf (Me.ShowHPAggregateHeader = 1) Then
>         If ctl.Tag = "Hideagg" And (Me.JustOne = 1) Then
> ctl.Visible = True
>         ElseIf ctl.Tag = "hideagg" And (Me.JustOne <> 1)
> Then ctl.Visible = False
>     End If
> Next ctl

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.384 / Virus Database: 216 - Release Date: 21-08-02


Tue, 08 Feb 2005 06:41:09 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. If Then ElseIf Statements

2. Elseif Statements

3. Need confirmation about ElseIf statement

4. IF ELSEIF statements in VBScript?

5. Solution to Nested Statements?

6. Embedded or nested Select Case statements

7. ExitSelect & Nested Select Statements

8. Q: GWBasic and nested if statements.

9. Nesting if...else statements

10. Nested SQL statements

11. Can you nest Select Case statements?

12. Nested With Statement

 

 
Powered by phpBB® Forum Software