multiple if statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • slenish
    Contributor
    • Feb 2010
    • 283

    multiple if statements

    Hello,

    Im trying to figure out if you can create many if statments with in each other? So far i can only get 2 to work. I need about 8 to run one after the other if the initial criteria is not meet.

    So for example if Text1 is "" then run code else run another code else run another one and so on. But for some reason it will only let me run two.

    Any help would be great.

    Here is what i have so far

    Code:
    If Me.Text12 = "" Then
    
    Me.Text12 = DLookup("[Box1]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box1]=" & Chr(34) & Text3 & Chr(34))
        
    Me.Text17 = DLookup("[Date]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box1]=" & Chr(34) & Text3 & Chr(34))
    
    Me.Text10 = DLookup("[BoxNumber]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box1]=" & Chr(34) & Text3 & Chr(34))
     
    Else
    
    Me.Text12 = DLookup("[Box2]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box2]=" & Chr(34) & Text3 & Chr(34))
        
    Me.Text17 = DLookup("[Date]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box2]=" & Chr(34) & Text3 & Chr(34))
    
    Me.Text10 = DLookup("[BoxNumber]", "MainTable", _
        "[Date]=#" & Format(Text1, "mm/dd/yy") & "# " & _
        "And [Box2]=" & Chr(34) & Text3 & Chr(34))
    
    End If
    What i need to do is get this code to keep going some how where i can add in a check for box 4, 5 and etc.

    Thank for the help in advance.
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    I would use in this case a SELECT CASE statement.

    This will allow to act on every different value of the field.

    Nested IF's are possible, but often the "higher" levels are making the understanding of the lowest level very hard.

    Nic;o)

    Comment

    • slenish
      Contributor
      • Feb 2010
      • 283

      #3
      Could post a link to some good examples?? I tried looking up the select case but im not seeing the right kind of example that I need.

      thanks for the help :D

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        From the Access helpfile:

        Code:
        Dim Number
        Number = 8    ' Initialize variable.
        Select Case Number    ' Evaluate Number.
        Case 1 To 5    ' Number between 1 and 5, inclusive.
            Debug.Print "Between 1 and 5"
        ' The following is the only Case clause that evaluates to True.
        Case 6, 7, 8    ' Number between 6 and 8.
            Debug.Print "Between 6 and 8"
        Case 9 To 10    ' Number is 9 or 10.
        Debug.Print "Greater than 8"
        Case Else    ' Other values.
            Debug.Print "Not between 1 and 10"
        End Select
        Nic;o)

        Comment

        • colintis
          Contributor
          • Mar 2010
          • 255

          #5
          Nic, what's the difference between using "6,7,8" and "6 To 8" from the sample code you've put above?

          Comment

          • nico5038
            Recognized Expert Specialist
            • Nov 2006
            • 3080

            #6
            When working with integers, there's no difference.
            When having a range 5 to 25, the difference is a lot of typing :-).

            Nic;o)

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Originally posted by Nico
              Nico:
              When having a range 5 to 25, the difference is a lot of typing :-).
              Good answer :D

              When not using integers the former will miss many potential values while the latter will not. The latter form is generally preferred where possible.

              Comment

              Working...