If Else

SaraMegan

Starving Artist
Local time
Today, 09:51
Joined
Jun 20, 2002
Messages
185
If Else (*Wicked* easy question!!)

Hi. I'm totally new at VBA so please bear with me...

I'm working on an IF Else statement that has three different options. I've tried a few different things...

I'm not sure how I would do this.

If Certified = 1 then
Do this
Else
If Certified = 2 then
Do this
Else
Do this
End if

(This example doesn't seem to work...)

Again, I've tried this a number of different ways, some of which I can't even remember. Some of them the debugger picks up on and I can fix, but others don't work, though it doesn't tell me where there's a problem.

thanks.

--Sara
 
Last edited:
Where are you trying to use the code, on a form, and if so what does certified represent?
 
I'm trying to use it on a form.

Certified is the name of an option group. I want to disable/enable parts of the form based on the selection in the option group. There are three options in the group.

Is that enough info? I can tell you as much as you need to know.

Thanks, Rich

--Sara
 
Use a Select Case statement. It's way easier than an If ...Then... for multiple things.

Example:

Select Case optCertified

Case 1
Your Code if option 1 is selected Here

Case 2
Your Code if option 2 is selected Here

Case 3
Your Code if option 3 is selected Here

End Select

Works great, easier to read, understand and maintain.
 
thanks boblarson!

I'm not clear, however, on how to create a select case statement. How do I write it exactly? Do you think you could give me an example?

Thanks. :)

--Sara
 
how do they work...

Use the Select Case statement as an alternative to using ElseIf in If...Then...Else statements when comparing one expression to several different values. While If...Then...Else statements can evaluate a different expression for each ElseIf statement, the Select Case statement evaluates an expression only once, at the top of the control structure.
In the following example, the Select Case statement evaluates the performance argument that is passed to the procedure. Note that each Case statement can contain more than one value, a range of values, or a combination of values and comparison operators. The optional Case Else statement runs if the Select Case statement doesn't match a value in any of the Case statements.

Code:
Function Bonus(performance, salary)
	Select Case performance
		Case 1
			Bonus = salary * 0.1
		Case 2, 3
			Bonus = salary * 0.09
		Case 4 To 6
			Bonus = salary * 0.07
		Case Is > 8
			Bonus = 100
		Case Else
			Bonus = 0
	End Select
End Function
 
all right, i think i see that... but how does it determine the difference between case 1, case 2, etc.

I mean, i have three options in this group... 1, 2, and 3. How do I tell it that Case 1 is equal to option 1? Does it just know?

Thanks again. This is really helpful. (Woo hoo! I'm learning!)
 
When you set up an option group, you specify what label you want and what the value of that option within that group. So, you can specify which things are what value. When you click on the option, it sets the value of the option group to the value you've chosen as the value for that option.

Make sure you use an option group, however, as just putting radio buttons (the ones that look like holes, with a dot in the one you select) will not be as easily manipulated within your code.
 

Users who are viewing this thread

Back
Top Bottom