IFElse Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elainenguyen
    New Member
    • Oct 2006
    • 51

    IFElse Statement

    I am trying to write a code in the cmdSave to give a break message when user didn't answer a question. For example. I have a option field fraAge>60 with Y and N, when user chooses N as an aswer, user has to choose 3 other Y/N checkbox fields which belong to No answer i.e Age70, Age80, Age90. If user didn't check any of those 3 fields, then I have to give a break message and won't allow user to move on unless they check either Age70, Age80 or Age90.

    Code:
    If fraAge>60=No and .....
    I am not sure how to write these double condition. Can someone please help?

    thanks!
  • elainenguyen
    New Member
    • Oct 2006
    • 51

    #2
    here is my actual code but it seems not working properly. Please help, thanks!

    Code:
    Private Sub cmdPageDown2_Click()
    If Me.fraDVT_SCD_ORDER = 2 And Me.DVT_RECENT_LE = "" _
    Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_THROBUS = "" _
    Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_BKA = "" _
    Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_SCD_OTHER = "" Then
    MsgBox "Must choose a field", vbOKOnly, "Required Field"
    Else
    DoCmd.GoToPage 2
    End If
    End Sub
    thanks!

    Comment

    • cm5th
      New Member
      • Sep 2008
      • 11

      #3
      Since you're going to pop up a message box if user selected NO but failed to make a choice amongst Age70, Age80 and Age90, why don't you created a simple form "frmNoCase" that consist of an OptionGroup with 3 Option buttons, one each for Age70, Age80 and Age90.

      When the user selected NO to your first question, you open frmNoCase as a pop-up form.

      DoCmd.OpenForm FormName:="frmN oCase", WindowMode:=adD ialog

      In this case, the user cannot proceed until he clicks one of the three options.

      You may need to put suitable a form header and control group label to help the user along.

      Comment

      • elainenguyen
        New Member
        • Oct 2006
        • 51

        #4
        Thanks for your help.
        My form has many fields, not just those 3 fields. It was jsut an example. I was trying to put the code in the ArrowDownPage, if user didn't answer, it'll show the break message and user can't go down the page until they choose either 3 or all.
        thanks!

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          Hi. Controls on a form that have not had data entered into them are generally null in value, not set to empty strings as you list in your code. The exception is a check-box control (representing a boolean value) which defaults to False unless three-state logic is enabled.

          I would also suggest being very careful about the grouping of your AND and OR operators; AND and OR are at the same level from the point of view of the VBA interpreter, and unless you group your clauses in parenthesis you may find that the VBA interpreter can validly interpret your code differently depending on the order you place things in.

          To treat it as a series of ANDs use parenthesis, like this:

          Code:
           IF ((cond1 AND cond2) OR (cond3 AND cond3) OR (cond5 etc
          There is a simpler way, which is to use two IFs, as the first condition is common to all the tests:

          Code:
          IF me!fraDVT_SCD_ORDER = 2 then
            If Isnull(me!DVT_Recent_LE) OR IsNull(Me!DVT_THROBUS OR ... THEN
              dostuff
            end if
          end if
          -Stewart

          Comment

          • elainenguyen
            New Member
            • Oct 2006
            • 51

            #6
            Hi,
            I've tried that way too but when I tested the form, it didn't show the break message, and also allowed user to move to the next page.
            I've placed the code in the On Click command. I've also placed the code in the OnCurrent command.
            Code:
            Private Sub cmdPageDown2_Click()
            If Me!fraDVT_SCD_ORDER = 2 Then
              If IsNull(Me!DVT_RECENT_LE) Or IsNull(Me!DVT_THROMBUS) Or IsNull(Me!DVT_BKA) Or IsNull(Me!DVT_SCD_OTHER) Then
              MsgBox "Must choose a field", vbInformation, "Required Field"
              Else
              DoCmd.GoToPage 2
              End If
            End If
            End sub
            Can you please help? I really have no idea how else to place my if and else statement. Thanks !

            Comment

            • elainenguyen
              New Member
              • Oct 2006
              • 51

              #7
              Hi,
              I've tried the following two ways, but then when user select one of the 4 fields, instead of allow user to go to the next page since the condition has been met (either choose all 4 or 1 among the 4, it popped up the break message again, until 4 options answered Yes.
              Code:
              If (Me.fraDVT_SCD_ORDER = 2 And (Me.DVT_RECENT_LE = NO Or Me.DVT_THROMBUS = NO Or Me.DVT_BKA = NO Or Me.DVT_SCD_ORDER = NO)) Then
              MsgBox....
              else
              docmd.gotoPage 2
              end if
              or
              Code:
              If (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_RECENT_LE = No) Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_THROMBUS = No) _
              'Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_BKA = No) Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_SCD_OTHER = No) Then
              MsgBox...
              else
              docmd.gotopage 2
              end if
              any thought?
              many thanks!

              Comment

              • elainenguyen
                New Member
                • Oct 2006
                • 51

                #8
                Thanks for everyone's help. After playing with the code, I've found the way to make it work. Belows is the correct code. I just need to change it from OR to AND.
                thanks!

                [CODE]
                If (Me.fraDVT_SCD_ ORDER = 2 And (Me.DVT_RECENT_ LE = No And Me.DVT_THROMBUS = No And Me.DVT_BKA = No And Me.DVT_SCD_OTHE R = No)) Then
                MsgBox.....
                else
                docmd.gotopage 2
                end if
                [/CODE}

                Comment

                • cm5th
                  New Member
                  • Sep 2008
                  • 11

                  #9
                  Originally posted by elainenguyen
                  Thanks for everyone's help. After playing with the code, I've found the way to make it work. Belows is the correct code. I just need to change it from OR to AND.
                  thanks!

                  [CODE]
                  If (Me.fraDVT_SCD_ ORDER = 2 And (Me.DVT_RECENT_ LE = No And Me.DVT_THROMBUS = No And Me.DVT_BKA = No And Me.DVT_SCD_OTHE R = No)) Then
                  MsgBox.....
                  else
                  docmd.gotopage 2
                  end if
                  [/CODE}
                  Do you know that the only thing that you have changed is that you ARE now treating the four Me.DVT_.. variables as BOOLEAN variables (comparing them to the boolean constant NO) in this solution whereas you WERE treating them string variables in your original (comparing them to "", i.e. vbNullString).

                  If you equate the four variables in this code segment as the variables: A, B, C, D, E, F ( meaning A = "Me.fraDVT_SCD_ ORDER=2", B = " Me.DVT_RECENT_L E = No", etc..) :

                  Your first IF CLAUSE says "A^B OR A^C OR A^D OR (A^E". The ^ means AND.
                  Your last working IF CLAUSE says "A ^ (NOT B ^ NOT C ^ NOT D ^ NOT E)"

                  In Boolean math, the two IF CLAUSES are equivalent. So, did you declare the four Me.DVT_ variables as BOOLEAN variables?

                  Sorry I got caught up in the riddle-like mystery. Hope it's helpful.

                  Comment

                  Working...