Results 1 to 9 of 9
  1. #1
    hithere is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    63

    Nested If Then Statement

    I have a nested if then statmement I am tryining to implement but it is quite difficult as it contains an inner if then statement and an outter if then statment - or an if then statment within an if then statment.

    I have three objects - ball, sqaure, triangle - each in three different colors - red, blue, green. Two objects are selected at a time and there is a message boxes that indicates the colors of the objects selected.

    I know how to implement then inner if then else statment as..
    If ball is red and square is red then msg box "Ball and square are red"


    Elseif ball is red and sqaure is blue then msg box "Ball is red, square is blue"
    Else if ballis red and square is green then....
    ........

    What I am having difficulty with is how to wrap it inside an outer if then statement consisting of what objects were selected
    If ball and square selected then
    If ball is red and square is red then msg box "Ball and square are red"
    Elseif ball is red and sqaure is blue then msg box "Ball is red, square is blue"
    Else if ball is red and square is green then....
    ........
    If ball and triangle selected then
    If ball is red and triangle is red then msg box "Ball and triangle are red"
    Elseif ball is red and triangle is blue then msg box "Ball is red, triangle is blue"
    Else if ball is red and triangle is green then....
    ........
    If square and triangle selected then....
    ................................

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    This looks for all the world like a classroom exercise on Nested If's

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,795
    Yes, do your homework. Write code, test, debug, repeat. Refer to link at bottom of my post for guidance on debug techniques. When you encounter specific issue with your code that seems to defy debug efforts, post question.

    Programming is, for a big part, an exercise in logical thinking. Application of logical thought processes needs to be practiced for improvement.
    Last edited by June7; 01-29-2013 at 06:05 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    We're not trying to be mean but you will learn infinately more if you work it out yourself than if we supply an answer.

  5. #5
    hithere is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    63
    After some debugging I came up with this which executes correctly....

    If ball and square selected then
    If ball is red and square is red then msg box "Ball and square are red"
    Elseif ball is red and sqaure is blue then msg box "Ball is red, square is blue"
    Else if ball is red and square is green then....
    End if
    End if

    If ball and triangle selected then
    If ball is red and triangle is red then msg box "Ball and trianlge are red"
    Elseif ball is red and triangle is blue then msg box "Ball is red, triangle is blue"
    Else if ball is red and triangle is green then....
    End if
    End if

    If square and triangle selected then
    If square is red and triangle is red then msg box "square and trianlge are red"
    Elseif square is red and triangle is blue then msg box "Square is red, triangle is blue"
    Else if ball is red and triangle is green then....
    End if
    End if

    I'm not sure if this is the 'cleanest' way to do it but it works.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    There is a lot to be said for "It works!".

  7. #7
    hithere is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    63
    Initially i tried to make one big if then else statment by trying to bracket the 'inner' portions with brackets which simply didn't work so I seperated them. I took a vba class about 10 years ago so I am trying to re-learn it all.

  8. #8
    qa12dx is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    india
    Posts
    112
    hmm... i wouldn't use a if statement here.

    u really have two results, both the shapes are of the same color, each shape is diffrent color.

    i'd just refference the stored fields;
    condition - color1=color2 - msgbox - [object1] & " and " & [object2]&" are " [color1]
    color1<>color2 - msgbox - [object1] & " is" & [color1]&", "&[object2]&" is " [color1]

    u'll need vb code for this. if u dont know script just search msgbox vb script.

    now, msgbox and textbox are two different things. msgbox is a pop up window and textbox if a field on ur form. if you want to use text box to show this message, use;

    iif(color1=color2, [object1] & " and " & [object2] &" are " [color1], [object1] & " is" & [color1] &", "&[object2]&" is " [color2])

    the second part doesn't need a criteria. cause if the colours or not same it has to be different... (is it a trick q for homework?)

  9. #9
    hithere is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Sep 2011
    Posts
    63
    Actually, this is a real simulation in the database I am working on. I substituted my own pararameters with colors and shapes for ease of explanation and clarity but the different combinations and iterations are real.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Nested If/Or Statement
    By kristyspdx in forum Queries
    Replies: 2
    Last Post: 10-15-2012, 04:49 PM
  2. Nested If statement
    By Bellablue in forum Access
    Replies: 7
    Last Post: 10-09-2011, 12:00 PM
  3. Nested IIf Statement
    By traquino98 in forum Queries
    Replies: 5
    Last Post: 06-11-2011, 10:56 AM
  4. Nested Iif statement help
    By Goodge12 in forum Queries
    Replies: 6
    Last Post: 09-21-2010, 11:45 AM
  5. nested if statement with two criteria
    By kendra in forum Queries
    Replies: 5
    Last Post: 06-16-2009, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums