Results 1 to 14 of 14
  1. #1
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Houston, Texas, USA
    Posts
    432
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How many nest If Else can I have?

    When I first start this project I want doing:
    If Item.Sendname = "current user"
    do this.....
    Else
    do that....
    End if

    Now I want to know can I add a third "Else" ????

    If Item.Sendname = "current user"
    do this.....
    Else
    do that....
    Else
    Do it again....
    End if

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    You want the ElseIf construct:

    <pre>If item.sendname = "current user" then
    [img]/w3timages/icons/mad.gif[/img]
    elseif item.sendname = "me" then
    [img]/w3timages/icons/laugh.gif[/img]
    elseif item.sendname = "you" then
    [img]/w3timages/icons/nuts.gif[/img]
    else
    'Who knows
    endif
    </pre>


    You could also use the Select Case
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    p.s. To answer your question: you can have as many nested if statements as memory allows.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  4. #4
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Houston, Texas, USA
    Posts
    432
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    Here is what I am trying to do. THe first IF checks to see if the jobnumber is 0. If it is 0 then this is the start of a new job request. Now the ElseIf, If the job is resubmitted it should have a "Sendername" so I compare the Sendername against the Current username. If they match then procedd with resubmitting the form. It will change the jobnumber by 1 count. The last option ELSE, if the first option is great then 0 and the Username is different I want it to just open the form and procedd.

    This has worked with the jobnum = 0 and the last Else statement. I am now trying to add the ElseIF statements to it.

    set nms=application.getnamespace("mapi")
    vrUser=nms.currentuser
    msgbox "User " & vrUser

    If UserProperties.Find("jobnum").Value = 0 Then
    responce = MsgBox("Do you need a request number assigned?", vbYesNo, "Initializing")
    If responce = 6 Then

    CounterStart = 1
    Counter = Rst.RecordCount + CounterStart
    set nms= application.getnamespace("mapi")
    vrUser=nms.currentuser

    UserProperties.Find("jobnum").Value = Counter
    Rst.AddNew
    set nms2= application.CreateItem(0)
    Rst.fields(11).value = nms2.Sent
    Rst.fields(4).value=vrUser

    Rst.Update
    Rst.Close
    MyDB.Close
    Else
    Rst.close
    Mydb.close
    Exit Function
    End If

    ElseIf Item.Sendername = "vrUser" then
    If UserProperties.Find("jobnum").Value > 0 then
    responce = MsgBox("Is this a resubmit job?", vbYesNo, "Initializing")
    If responce = 6 Then
    CounterStart = 1
    Counter = Rst.RecordCount + CounterStart
    set nms= application.getnamespace("mapi")
    vrUser=nms.currentuser

    UserProperties.Find("jobnum").Value = Counter
    UserProperties.Find("Reqby").Value = vrUser

    Rst.AddNew
    set nms2= application.CreateItem(0)
    Rst.fields(11).value = nms2.Sent
    Rst.fields(4).value=vrUser

    Rst.Update
    Rst.Close
    MyDB.Close
    Else
    Rst.close
    Mydb.close
    Exit Function
    End If

    Else
    RequestNum =UserProperties.Find("jobnum").Value
    Set Rst = MyDB.OpenRecordset("select * from dbtask where tasknum = " & RequestNum)
    UserProperties.Find("txtstatus").Value = rst.Fields(8).Value
    UserProperties.Find("coper1").Value = rst.Fields(5).Value
    'msgbox "Inside Update"
    If UserProperties.Find("txtstatus").Value = "Work in progress" then
    msgbox"This job has been started already"
    End if

    If UserProperties.Find("txtstatus").Value = "Finished" then
    msgbox"This job has been completed already"
    End if
    rst.close
    mydb.close
    End If
    End if
    End Function


    Daniel J. Reyes
    Operations PC SPecialist
    1002 Texas Parkway
    Stafford, Tx. 77477
    Daniel.Reyes@tabsdirect.com
    281-403-7259

    Si, Hablo Ingles

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    Here are the changes I'd make (I hope I get the drift here)

    <pre>set nms=application.getnamespace("mapi")
    vrUser=nms.currentuser
    msgbox "User " & vrUser

    If UserProperties.Find("jobnum").Value = 0 Then
    if MsgBox("Do you need a request number assigned?", vbYesNo, "Initializing") = vbYes then
    'Note the test and the message box on same line
    CounterStart = 1
    Counter = Rst.RecordCount + CounterStart
    set nms= application.getnamespace("mapi")
    vrUser=nms.currentuser
    UserProperties.Find("jobnum").Value = Counter
    Rst.AddNew
    set nms2= application.CreateItem(0)
    Rst.fields(11).value = nms2.Sent
    Rst.fields(4).value=vrUser

    Rst.Update
    Rst.Close
    MyDB.Close
    Else
    Rst.close
    Mydb.close
    Exit Function
    End If

    ElseIf Item.Sendername = "vrUser" then
    'You already know the jobnum <> 0 so why re-test??
    If MsgBox("Is this a resubmit job?", vbYesNo, "Initializing") = vbYes then
    CounterStart = 1
    Counter = Rst.RecordCount + CounterStart
    set nms= application.getnamespace("mapi")
    vrUser=nms.currentuser
    UserProperties.Find("jobnum").Value = Counter
    UserProperties.Find("Reqby").Value = vrUser
    Rst.AddNew
    set nms2= application.CreateItem(0)
    Rst.fields(11).value = nms2.Sent
    Rst.fields(4).value=vrUser
    Rst.Update
    Rst.Close
    MyDB.Close
    Else
    Rst.close
    Mydb.close
    Exit Function
    End If

    Else
    RequestNum =UserProperties.Find("jobnum").Value
    Set Rst = MyDB.OpenRecordset("select * from dbtask where tasknum = " & RequestNum)
    UserProperties.Find("txtstatus").Value = rst.Fields(8).Value
    UserProperties.Find("coper1").Value = rst.Fields(5).Value
    'msgbox "Inside Update"
    If UserProperties.Find("txtstatus").Value = "Work in progress" then
    msgbox"This job has been started already"
    End if

    If UserProperties.Find("txtstatus").Value = "Finished" then
    msgbox"This job has been completed already"
    End if
    rst.close
    mydb.close
    End If
    End Function
    </pre>


    That makes sense to me. What do you think?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    You can have as many as you want. Whether anyone else will appreciate that is another matter.

    I have found a few guidelines that serve ME well, they may serve you well too, they may not. And they are guidelines, not rigorous rules:

    1. Make the basic construct an IF-ELSE-ENDIF; always include the ELSE statement, even when you think there is no alternative. I've found that by actually typing ELSE with nothing (except the ENDIF) following it, my mind is sometimes jogged enough to realize that there IS an alternative.

    2. Limit nesting to three levels. If the conditions become more than three levels deep, it's probably time to turn the innermost levels into a function, if at all possible.

    These guidelines seem to allow me to stick within another guideline - that of keeping a sub's or function's code visible all on one screen.

    I don't always stick to these guidelines, but when I can, my programs seem to have less problems.

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    > Exit Function


    [soapbox]
    I hate this with a passion. It's as bad as a GOTO.

    When I'm scanning down code, I use the nesting level to blocvk off chunks/ Discovering (usually much later) that the darn code exited right out of there instead of dropping through is a pain.

    I can't think of a valid reason for using "Exit Function". Maybe someone else can.

    I'm still waiting for someone to convince me that GOTO is necessary (excepting as a result of the ridiculous On Error mistake).
    [/soapbox]

  8. #8
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    I believe you can improve the readability of long routines by using the Exit statement. It can save having to wrap the entire routine with an If-Then statement.

    I guess we can agree to disagree on the use of the Exit statement.

    The only time you should use a GOTO statement is in an error handler. i.e. On Error Goto Super_Duper_Error. Not every error can be handled using in-line error handling.

    Mike

  9. #9
    4 Star Lounger
    Join Date
    Jan 2001
    Location
    Houston, Texas, USA
    Posts
    432
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    Kevin,
    That got it, In fact the other changes you mentioned worked out even better.

    Thank you to very one out there that helped me work this out. VBNerd & Crisgreaves, I got a lot of usefull information from you suggestions also.

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    Daniel,

    Good to hear it all worked out.

    Yeah, when I see an easy improvement to someone else's code, I just go for it. I welcome the same interjections from others -- that's how we get better.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    How about 'exit for'? Do you like that one?

    Tell us why.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    2 Star Lounger
    Join Date
    Feb 2001
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    *** Geoff W URL reference corrected ***

    There's an excellent article about constructing your boolean logic at <A target="_blank" HREF=http://www.devx.com/upload/free/features/vbpj/2000/02feb00/cs0200/cs0200.asp>http://www.devx.com/upload/free/features/v...0200/cs0200.asp</A>

    Also, I find myself doing a lot of testing for the negative, then exiting the sub or function if it passes.

    <pre>If sometest() = False _
    Then
    Exit Function
    End If
    </pre>


    I find this easier than putting huge blocks of code within an if-then statement.

    My $.02.

    Mike

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    ExitFor I don't like either, but if I was choosing sleeping partners, I'd take ExitFor over ExitFunction any day.

    At least with ExitFor you are probably popping out quite locally. I eyeball a FOR-loop and drop down the the Next statement. That's where the ExitFor will arrive anyway, even if I've not spotted the Exit insode the loop.

    But ExitFunction is just too sneaky; it scoots out through another dimension, it seems to me.

    Rot your socks, all of you!!!! Now I have to go search all my VBA code for any ExitFor statements before you catch one.

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How many nest If Else can I have?

    Chris,

    EXIT FORs are your friends. Learn to embrace them. Peace.

    And if you write short, tight functions, exit function is very much a good thing too. It's only spagetti if you break other rules.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •