By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,600 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,600 IT Pros & Developers. It's quick & easy.

IFElse Statement

P: 51
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.

Expand|Select|Wrap|Line Numbers
  1. If fraAge>60=No and .....
  2.  
I am not sure how to write these double condition. Can someone please help?

thanks!
Sep 8 '08 #1
Share this Question
Share on Google+
8 Replies


P: 51
here is my actual code but it seems not working properly. Please help, thanks!

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPageDown2_Click()
  2. If Me.fraDVT_SCD_ORDER = 2 And Me.DVT_RECENT_LE = "" _
  3. Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_THROBUS = "" _
  4. Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_BKA = "" _
  5. Or Me.fraDVT_SCD_ORDER = 2 And Me.DVT_SCD_OTHER = "" Then
  6. MsgBox "Must choose a field", vbOKOnly, "Required Field"
  7. Else
  8. DoCmd.GoToPage 2
  9. End If
  10. End Sub
  11.  
thanks!
Sep 8 '08 #2

P: 11
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:="frmNoCase", WindowMode:=adDialog

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.
Sep 8 '08 #3

P: 51
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!
Sep 8 '08 #4

Expert Mod 2.5K+
P: 2,545
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:

Expand|Select|Wrap|Line Numbers
  1.  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:

Expand|Select|Wrap|Line Numbers
  1. IF me!fraDVT_SCD_ORDER = 2 then
  2.   If Isnull(me!DVT_Recent_LE) OR IsNull(Me!DVT_THROBUS OR ... THEN
  3.     dostuff
  4.   end if
  5. end if
-Stewart
Sep 8 '08 #5

P: 51
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.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdPageDown2_Click()
  2. If Me!fraDVT_SCD_ORDER = 2 Then
  3.   If IsNull(Me!DVT_RECENT_LE) Or IsNull(Me!DVT_THROMBUS) Or IsNull(Me!DVT_BKA) Or IsNull(Me!DVT_SCD_OTHER) Then
  4.   MsgBox "Must choose a field", vbInformation, "Required Field"
  5.   Else
  6.   DoCmd.GoToPage 2
  7.   End If
  8. End If
  9. End sub
  10.  
Can you please help? I really have no idea how else to place my if and else statement. Thanks !
Sep 9 '08 #6

P: 51
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.
Expand|Select|Wrap|Line Numbers
  1. 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
  2. MsgBox....
  3. else
  4. docmd.gotoPage 2
  5. end if
  6.  
or
Expand|Select|Wrap|Line Numbers
  1. If (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_RECENT_LE = No) Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_THROMBUS = No) _
  2. 'Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_BKA = No) Or (Me.fraDVT_SCD_ORDER = 2 And Me.DVT_SCD_OTHER = No) Then
  3. MsgBox...
  4. else
  5. docmd.gotopage 2
  6. end if
  7.  
any thought?
many thanks!
Sep 9 '08 #7

P: 51
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_OTHER = No)) Then
MsgBox.....
else
docmd.gotopage 2
end if
[/CODE}
Sep 9 '08 #8

P: 11
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_OTHER = 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_LE = 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.
Sep 9 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.