Solved Exercises for if elseif else ...

Agree with Pat. ElseIfs make it easy to lose your logic, hard to implement it and easy to miss cases, especially when you are working with 2+ variables like in your velocity/color example.

So often you see people pose questions about 'why won't it ever hit this line of my if/else'. And it turns out its impossible to hit because prior cases catch it. Or they set one of the variables inside a prior if/else hoping a future if/else in the same block will catch it later on. Or they implement incomplete logic and the combination of all their variables never trigger any block of code or always hits their final else and they just can't find why.

If/elses can be rewritten with plain Ifs and if some blocks are code are to have precedence over others, you can order your ifs to achieve that or set a variable to denote that action has already been taken and no further action needed in further code.
 
Yes I start to put inside elseif and then I didnt undertand nothing. Part of the code that I want was correct executed part of the code not and then I start thiking that I need more practice to undertand if- then- else statments. This is basic and is shame of me that I get into disaster.

Now I will try to implement my code too see if will work or not. For now I mamnged 80% working and then I get lost in strings...:rolleyes:
 
Hi

cheekybuddha


I try to implement your help with the code but Is not working like I want.
I will look again to see if I have covered or possible conditions and I will try again to put select cases.
Maybe later if I managed something you can check again the code to see if I am in correct way.
Thank you

 
Hi Pat
yes I will not use Elseif. I will try to combine if-then-else and select cases from now on .

With the code who show me cheekybudha
I get error run time error 94
Inavalid use of null error
Becouse the StanjeSproscanja is not having the status (3) Sproscena. It has another status in my case (6) Sending

this line

Case Me.StanjeSproscanja = "(3) Sproscena" And Me.NamenUporabe = "RednaAnaliza"


 
ohh wrong the status of the item is Sending is not relased yet..and Me.StanjeSproscanja ( mean= if the item is already released). becouse the item is not already relased Me.StanjeSproscanja is empty field... and becouse of that access is saying me invalid use of null?
becouse I told him it must be relased and in my case is not relased yet...
 
I don't think that kind of condition is legal in a CASE statement because it leaves no room for the implied comparison.

You use SELECT CASE variable followed by CASE comparison statements. You use the comparison statement as though the variable was in front of it. So you can have

Code:
SELECT CASE age
    CASE 0 to 12
...  execute if age between 0 and 12
    CASE 13 to 20
... execute if age between 13 and 20
    CASE 21
... execute if age = 21
    CASE > 21
... execute if age > 21
END SELECT

The problem with your statement is that you leave no meaningful place for the variable from the SELECT CASE statement to DO anything.

 
Ohh yes I know I am so close and so far
I will think another possibility how to prevent that user will go to regualar analysis if the item is not released...
 
StatusKolone is text field...I just put the number in front becouse it was more fancy to the users? they say ohh if i see 3 Sproscena i will know in the second that this mean that this is relased,... :oops:

I could use ony Sproscena for the status item. Only released.

I am using here StatusKolone and StanjeSproscanja ( this is dolookup field which is cheking if the item is relased?) Maybe I should only use the statusKolone becouse StatusKolone and StanjeSproscanja is telling me the same no?
 
Agree with this, but why in any professional code, you would want to do that ?
Why is a location depending of the velocity ?
(I know it is an example)
If you have have different expressions to check, split them and make them easier to read. That is my opinion of course ;)
If you read my post there is no recommendation to use an else if. The point was that a select case is not a replacement for else if in the cases where you are evaluating multiple expressions. It is shorthand for doing a nested if then else. I can do that example with nested if then else but not with a select case. I did not say that this is better or more readable then the nested if then else. I personally use elseif often, because i am very good at conditional logic, but would recommend nested ifs for the OP. Again only in cases where you are evaluating multiple expressions. Select case where you evaluate one expression with many conditions. Apples and oranges.
 
The line you're looking at is not an assignment, it's one of the cases to be compared.

@lacampeona , try adjusting to:
Code:
' ...
Case Me.StanjeSproscanja & "" = "(3) Sproscena" And Me.NamenUporabe = "RednaAnaliza"
' ...

nb untested, answering from phone
 
Ohh Pat I dont know..now I dont have errror? Access dont say nothing.
hmm I am still testing to see if the error will show again
the error was in this line

in the before update event code
Case Me.StanjeSproscanja = "(3) Sproscena" And Me.NamenUporabe = "RednaAnaliza"
 
Hi ChekyBudha
Yes I adjusted that..error dissapear but now the code is not executing correct.
If it was relased you can not go again to release testing and now access is allowing that..hmmm I will check again my conditions
 
I think i finally managed what I want. I will do a lot of testing these days and I will see if all is ok or not.
I use Select Case like you experts suggested me.
I also change some conditions in my code-becouse of that one part of the code was not correct. :confused:

Thanks to all of you to give me correct instrustions.(y)
 
@Pat Hartman

The line in question is part of a SELECT CASE statement. The error is that a CASE statement IMPLIES a comparison to the variable named by the SELECT CASE, but if you look at the line:

Code:
Case Me.StanjeSproscanja & "" = "(3) Sproscena" And Me.NamenUporabe = "RednaAnaliza"

it doesn't begin with a relational operator AND it introduces an extra field. When you do SELECT CASE X followed by a CASE expression statement, you can have a limited number of expressions. The implication of the statement is that EACH of the CASE elements could be put into a comparison statement as though you wrote " X " & " expression " (and VBA supplies an "=" if and there is no leading relational operator).

If you look at the case statement, its argument would evaluate as a Boolean expression: (Me.field = "some string") AND (me.someotherfield = "another string") - which will produce either TRUE or FALSE. I added parentheses to emphasize the way it would most likely be evaluated. So the implied CASE evaluation would be for {string value =} some Boolean expression - and that just ain't gonna fly.

Not to mention that there is a ZLS after the first ampersand, followed by a naked equals-sign - but that won't produce a compile error on that basis, because all it does is tack the ZLS on the end of the value in the first control. I.e. protects against a null, whether intentionally or accidentally. So nothing to complain about.
 
I don't think that kind of condition is legal in a CASE statement because it leaves no room for the implied comparison.

You use SELECT CASE variable followed by CASE comparison statements.
The important bit to notice is the initial:
Code:
Select Case True
Case ...

There is no requirement for the variable being compared to be in the part after Select Case

This allows multiple (unrelated if desired) variables to be tested in the case statements.

So, instead of the traditional:
Code:
Select Case X   ' <-- Only this variable is being tested
Case 1
  ' ...
Case 2
  ' ...
Case 3
  ' ...
End Select
You can have:
Code:
Select Case True   ' <-- First case that evaluates to True wins
Case X = 1         ' <-- variable X is being tested
  ' ...
Case Y = 1         ' <-- variable Y is being tested
  ' ...
Case Z = 42        ' <-- variable Z is being tested
  ' ...
End Select
 
Hi CheekyBuddha

your solutution work now good for me. Thank you very much. But can I ask you something?

I never read about select case that you can write like you did it. How do you know that? I know you are expert but without that my code wouldnt work.
When first reading your code..I thought maybe this is mistake? becouse I never see putting Select Case True
Then I start testing and I was very happy. :love:

Select Case True
Case ...
 
I don't know where I saw it first (it was a long time ago!)

But once you see it, it makes perfect sense!

The condition (Case) which first evaluates the comparison expression to True will be executed. You can actually have variables in both parts (sides of the equation) if you wanted. It might get confusing though!

Another shortcut is to use the expression to return a result for assignment.

So, instead of something like:
Code:
If Not IsDate(Me.DateEntered) Then
  Me.cmdSubmit.Enabled = True
Else
  Me.cmdSubmit.Enabled = False
End IF
you can simply say:
Code:
Me.cmdSubmit.Enabled = Not IsDate(Me.DateEntered)

I know that Pat dislikes this form of expression, but it makes perfect sense to me.
 
Clever approach, David. Of course, it still DOES make the comparison I mentioned.

What you are doing is the LOGICAL equivalent of an IF expression THEN action ladder and putting the action as the action element of the CASE statement and the expression element as the discriminator of the CASE statement. But technically you still are using the element following SELECT CASE in a comparison - in this case, a Boolean comparison. That IS a neat approach but I want to avoid confusing others who read this. The SELECT CASE ladder is still doing EXACTLY what SELECT CASE statements do. It is just an unorthodox placement of elements. Also, if you do this, the first discriminator that is TRUE will stop the ladder even if other discriminators are also TRUE. Which means that ordering of the discriminators is crucial.

I may be using an older term that I learned a long time ago so I will clarify my meaning:

Code:
SELECT CASE expression
    CASE discriminator1 action1
    CASE discriminator2 action2
    CASE discriminator3 action3
    CASE ELSE action4
END SELECT

Logic-wise, this is identical to

Code:
IF expression = discriminator1 THEN action1
ELSEIF expression = discriminator2 THEN action2
ELSEIF expression = discriminator3 THEN action3
ELSE action4
END IF

(where the "=" is supplied by VBA if the discriminator does not start with a relational operator like < or > or = or etc.)
 
The thing a lot of folk miss is that all of expression, discriminator1, discriminator2, and discriminator3 can be expressions in themselves and not just constants.
 

Users who are viewing this thread

Back
Top Bottom