Solved Exercises for if elseif else ... (1 Viewer)

cheekybuddha

AWF VIP
Local time
Today, 18:11
Joined
Jul 21, 2014
Messages
2,280
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
 

lacampeona

Registered User.
Local time
Today, 19:11
Joined
Dec 28, 2015
Messages
392
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"
 

lacampeona

Registered User.
Local time
Today, 19:11
Joined
Dec 28, 2015
Messages
392
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
 

lacampeona

Registered User.
Local time
Today, 19:11
Joined
Dec 28, 2015
Messages
392
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)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:11
Joined
Feb 28, 2001
Messages
27,192
@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.
 

cheekybuddha

AWF VIP
Local time
Today, 18:11
Joined
Jul 21, 2014
Messages
2,280
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
 

lacampeona

Registered User.
Local time
Today, 19:11
Joined
Dec 28, 2015
Messages
392
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 ...
 

cheekybuddha

AWF VIP
Local time
Today, 18:11
Joined
Jul 21, 2014
Messages
2,280
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:11
Joined
Feb 28, 2001
Messages
27,192
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.)
 

cheekybuddha

AWF VIP
Local time
Today, 18:11
Joined
Jul 21, 2014
Messages
2,280
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:11
Joined
Feb 28, 2001
Messages
27,192
True enough, David, but I just didn't want to obscure the fact that it didn't change the overall concept of the CASE construct. AND you do have to be careful about the implied data types. By using a Boolean value (TRUE) you require/force the discriminator expressions to produce Boolean values as well.
 

cheekybuddha

AWF VIP
Local time
Today, 18:11
Joined
Jul 21, 2014
Messages
2,280
True enough, David, but I just didn't want to obscure the fact that it didn't change the overall concept of the CASE construct. AND you do have to be careful about the implied data types. By using a Boolean value (TRUE) you require/force the discriminator expressions to produce Boolean values as well.
Yes, I meant to say the same principle about expressions applies equally to If ... ElseIf ... End If too.

Re. the datatypes: we have seen an example of exactly that earlier in this thread where the Null had to be handled to compare with a string boolean.
 

Users who are viewing this thread

Top Bottom