• How many nest If Else can I have?

    • This topic has 13 replies, 4 voices, and was last updated 24 years ago.
    Author
    Topic
    #353391

    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

    Viewing 2 reply threads
    Author
    Replies
    • #516753

      You want the ElseIf construct:

      If item.sendname = "current user" then
         
      elseif item.sendname = "me" then
         
      elseif item.sendname = "you" then
         
      else
         'Who knows
      endif
      

      You could also use the Select Case

      • #516760

        p.s. To answer your question: you can have as many nested if statements as memory allows.

      • #516767

        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

        • #516773

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

          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
          

          That makes sense to me. What do you think?

          • #516913

            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.

            • #516914

              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.

    • #516849

      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.

    • #516867

      *** Geoff W URL reference corrected ***

      There’s an excellent article about constructing your boolean logic at http://www.devx.com/upload/free/features/v…0200/cs0200.asp

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

      If sometest() = False _
      Then
         Exit Function
      End If
      

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

      My $.02.

      Mike

      • #516879

        > 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]

        • #516884

          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

        • #516915

          How about ‘exit for’? Do you like that one?

          Tell us why.

          • #516949

            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.

            • #516951

              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.

    Viewing 2 reply threads
    Reply To: How many nest If Else can I have?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: