Simple Nest IF Statement

  • Thread starter Thread starter Krzysztof via AccessMonster.com
  • Start date Start date
K

Krzysztof via AccessMonster.com

Good Day!!

I currently have working form with a number of IF's and I want to change into
nested, for better programming, and neater code, but it is not allowing me to
do what I want.

I have now:

If LocationPicker = "001" Then
LocationDisplay.Value = "Location Name 1"
Else
End If

If LocationPicker = "002" Then
LocationDisplay.Value = "Location Name 2"
Else
End If

and so on...

I have tried:

If LocationPicker = "001" Then
LocationDisplay.Value = "Location Name 1"
Else
If LocationPicker = "002" Then
LocationDisplay.Value = "Location Name 2"
Else
....
Endif

this tells me it is missing end for block if

i have also tried:

If LocationPicker = "001" Then
LocationDisplay.Value = "Location Name 1"
Else If LocationPicker = "002" Then
LocationDisplay = "Location Name 2"
....
End if

It does not like me to combine 'Else' and 'If' on the same line.

i don't know what I a doing wrong, it seems so simple

Very Much TIA

~K
 
Hi Krzysztof

You're missing the form reference in your if statement - should assuming
that LocationPicker is the control on the form, it should be
Me.LocationPicker (I'm assuming this is not a variable already populated
previously in your code). Also, you have Else with no actions:

If LocationPicker = "001" Then
LocationDisplay.Value = "Location Name 1"
Else
End If

Try
If Me.LocationPicker = "001" Then
Me.LocationDisplay.Value = "Location Name 1"
End If

However, with so many options on one control being used to set a single
control value, this is a good place to use a SELECT CASE statement:

SELECT CASE Me.LocationPicker
CASE "001"
Me.LocationDisplay.Value = "Location Name 1"

CASE "002"
Me.LocationDisplay.Value = "Location Name 2"

CASE "003"
Me.LocationDisplay.Value = "Location Name 3"
'Add any additional CASE options here
END SELECT

This makes adding or editing options much simpler in the future, and reduces
processing time, as the code exits the SELECT CASE when it finds a match,
rather than continuing to test against every possible option.

Oh, one other thing - if LocationPicker is a numeric value, drop the quotes
on "001" etc - make them simply 001, 002 and so on.
 
Oh, and drop the Value - you don't need that:
SELECT CASE Me.LocationPicker
CASE "001"
Me.LocationDisplay = "Location Name 1"

CASE "002"
Me.LocationDisplay = "Location Name 2"

CASE "003"
Me.LocationDisplay = "Location Name 3"
'Add any additional CASE options here
END SELECT
 
Krzysztof,
Check out Select Case in Help.
Example from Help...

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
 
Krzysztof,

There are 2 quick ways to do what you are doing... You can use a Select Case
statement or an ElseIF block.

Select Case LocationPicker
Case "001"
[your code here]
Case "002"
[your code here]
Case ....
Case Else
End Select

The Select case makes it easier to read (in my opinion).

Alternately, where you had:
If LocationPicker = "001" Then
...
Else If LocationPicker = "002" Then
...
End If

Combine the Else and the If on the line, and they will do fine: ElseIf (one
word) instead of Else (space) If

-Nick
 
Thank You Both for Your help!

I have forgot about the CASE option: that will work mush better!

i need to go back to my programming books!

thanks again!
Oh, and drop the Value - you don't need that:
SELECT CASE Me.LocationPicker
CASE "001"
Me.LocationDisplay = "Location Name 1"

CASE "002"
Me.LocationDisplay = "Location Name 2"

CASE "003"
Me.LocationDisplay = "Location Name 3"
'Add any additional CASE options here
END SELECT
Hi Krzysztof
[quoted text clipped - 85 lines]
 
Glad to help any time

;-)

SusanV

Krzysztof via AccessMonster.com said:
Thank You Both for Your help!

I have forgot about the CASE option: that will work mush better!

i need to go back to my programming books!

thanks again!
Oh, and drop the Value - you don't need that:
SELECT CASE Me.LocationPicker
CASE "001"
Me.LocationDisplay = "Location Name 1"

CASE "002"
Me.LocationDisplay = "Location Name 2"

CASE "003"
Me.LocationDisplay = "Location Name 3"
'Add any additional CASE options here
END SELECT
Hi Krzysztof
[quoted text clipped - 85 lines]
 
I wouldn't code this type of function in a form. It would be more robust to
create a small lookup table of locations. You could then use a combo box on
the form. If that doesn't work for you, create a small user-defined function
that accepts the code and returns the name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top