Simple question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, hopefully this is a simple question :)

When using an If Elseif Elseif Elseif....End If, are the Elseif's after the
first If only tested if the first if is true or only tested if the first if
is false as well as all the elseifs before it etc.

For instance, say hypothetically (even though this is not possible), that we
set the values of x equal to 2 and 5. What would happen with code that said
something like
If x =1 then
msgbox "1"
Elseif x = 2
msgbox "2"
Elseif x=3
msgbox "3"
Elseif x=4
msgbox "4"
Elseif x = 5
msgbox "5"
End if

Would you get messages boxes with 2, and 5? Just 2? Or none?

Thanks!
 
Michael said:
Hi, hopefully this is a simple question :)

When using an If Elseif Elseif Elseif....End If, are the Elseif's
after the first If only tested if the first if is true or only tested
if the first if is false as well as all the elseifs before it etc.

For instance, say hypothetically (even though this is not possible),
that we set the values of x equal to 2 and 5. What would happen with
code that said something like
If x =1 then
msgbox "1"
Elseif x = 2
msgbox "2"
Elseif x=3
msgbox "3"
Elseif x=4
msgbox "4"
Elseif x = 5
msgbox "5"
End if

Would you get messages boxes with 2, and 5? Just 2? Or none?

Thanks!

Just 2. Once you get a True the rest of the ElseIfs are skipped.
 
The If and ElseIfs are processed sequentially until the first true statement
is found. At that point, the statement under the true condition is executed
and then the code jumps to the End If statement. The answer to your question
is that you would get a Messagebox with 2.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
Hi Micheal

You would get none.
Unless you had If x =2 AND x =5 then......etc.etc

But you would most likely get 2 - this runs on the assumption the x can
only equall one thing at a time. So AfterUpdate of x (with 2) you would get
2.

Not so simple as you think ??
 
So could I use Elseifs to have my own custom error messages or would I need a
bunch of if end ifs. For instance, say a user does something that triggers
one error. This Elseif structure would be fine b/c it would find the error
number and give the message box I customized. But, what if a users action
triggers two errors. Would the logic be looped through twice so I would still
be fine with the elseif structure or would only the first error number be
customized and the second not customized since it exits the loop upon finding
a true statement?
 
For instance, say hypothetically (even though this is not possible), that
we set the values of x equal to 2 and 5.

The answer would be None unless you had If x =2 AND x =5 then......etc.etc
as

Isn't true
 
For custom error messages the Select Case construct would be better. Select
Case looks like:
Select Case X
Case 1
msgbox "1"
Case 2
msgbox "2"
Case 3
msgbox "3"
Case Else
msgbox "Not 1 or 2 or 3"
End Select

Again, Select Case is processed sequentially. The values after "Case" are
potential values of X. Select Case in the above example looks at each Case
statement to see if X equals the value after "Case" and if it finds a Case
statement that is true, it executes the code below the Case statement. "Case
Else" is optional. In the above example, if X does not equal 1, 2 or 3, Case
Else is true and the code under Case Else is executed. If you omitted Case
Else, you would get no message if X does not equal 1, 2 or 3. Select Case
only runs once unless you have it in a loop.

Regarding your question about two errors ---
That can't happen. If you are coding to handle errors, you have a statement
like:
On Error Go To MyErrorHandler
So as soon as the first error occurs, your error handling code executes. You
can't have two simultaneous errors!

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
Wayne,

It's not possible for X to have two different values simultaneously!
Therefore, the user would get a messagebox with 2.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
Hi Michael,

A Select Case structure is better in the situation you seem to have:

Select Case x
Case 1
MsgBox "1"
Case 2
MsgBox "2"
Case 3
MsgBox "3"
Case 4
MsgBox "4"
Case 5
MsgBox "5"
Case Else
MsgBox "X is not in 1..5"
End Select

If x = 2, only the Case 2 code is executed; if x = 5, only the Case 5
code is executed.

Elsewhere you mention the user doing something that "triggers two
errors". If you mean VBA errors, I'm pretty sure you can't have two
errors at the same time, because the first error will transfer control
to the error handler.
 
That's right!

When coding to handle errors, there is a statement like:
On Error Go To MyErrorHandler
So as soon as the first error occurs, the error handling code executes.
There can not be two simultaneous errors!

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
well what i meant is that an error occurs. You hit okay, and then another
error occurs. In that case, would the error handling code be executed twice.
The reason I am asking is because I really don't want to have to go to all of
my forms, buttons, etc and change the code I have in there for my custom
error messages. Currently, I use the elseif structure not the case structure.
I guess my question is why is the case structure better. Won't both work?
--
Michael


Steve said:
That's right!

When coding to handle errors, there is a statement like:
On Error Go To MyErrorHandler
So as soon as the first error occurs, the error handling code executes.
There can not be two simultaneous errors!

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)
 
Typically, error handling code gracefully exits the procedure after it
displays an error message. If your error handling code jumps back into the
middle of your procedure below where the first error occurred, the error
handling code would excute again if another error occurred.

Regarding the Select Case structure, it executes much faster than ElseIF.

PC Datasheet
Providing Customers A Resource For Help With Access, Excel And Word
Applications
(e-mail address removed)



Michael said:
well what i meant is that an error occurs. You hit okay, and then another
error occurs. In that case, would the error handling code be executed
twice.
The reason I am asking is because I really don't want to have to go to all
of
my forms, buttons, etc and change the code I have in there for my custom
error messages. Currently, I use the elseif structure not the case
structure.
I guess my question is why is the case structure better. Won't both work?
 
Back
Top