VBA-help needed. Field calculator. IF THEN ELSE

3253
4
02-08-2011 05:23 AM
ChristianSchreiner
New Contributor II
Hi,
I am quite new to the scripting thing an hope you guys can give me some hands on advice.

Here`s the problem:

I have a table containing a field called "BO_FUNK" filled with integer values from 1 to 4 that represent categories. Now my team leader decided to change the category values.

Old   New
4      1
3      2
3      3
1      4

I have a new integer field called "BO_FUNK_v1", that shall contain the new values.

How can I populate the new field, using IF THEN ELSE Expressions???

I tried it like this, which did not work:

If  [BO_FUNK] = 4 Then
[BO_FUNK_v1] = 1
If  [BO_FUNK] = 3 Then
[BO_FUNK_v1] = 2
If  [BO_FUNK] = 2 Then
[BO_FUNK_v1] = 3
If  [BO_FUNK] = 1 Then
[BO_FUNK_v1] = 4
End If

What am I doing wrong???

Hope you can help some newby like me.

I already searched the topics and could not find anything useful for me.

TIA

Christian
0 Kudos
4 Replies
NiklasNorrthon
Occasional Contributor III
Why complicate matters so much?

What's wrong with 5 - [BO_FUNK]?

Can't help you with VB code, but in python you'd fill in the the code block with the following:
def my_really_complex_way_of_doing_something_simple(old_value):
    if old_value == 4:
        return 1
    elif old_value == 3:
        return 2:     
    elif old_value == 2:
        return 3: 
    elif old_value == 1:
        return 4:

And then in the expression field you say:
my_really_complex_way_of_doing_something_simple(!BO_FUNK!)

It should be exactly the same in VB, except the different syntax between the languages.
0 Kudos
Senthil_KumarShanmugasundaram
New Contributor
Your syntax is wrong, but there are several ways you do this

click on advance and paste one of the 2 method below in script pre-Logic


Method 1
dim val as integer
If  [BO_FUNK] = 4 Then
val  = 1
End If

If  [BO_FUNK] = 3 Then
val  = 2
End If

If  [BO_FUNK] = 2 Then
val  = 3
End If

If  [BO_FUNK] = 1 Then
val  = 4
End If

In the second text box (fieldname), enter
val

Method 2

dim val as integer
If  [BO_FUNK] = 4 Then
val = 1
ElseIf  [BO_FUNK] = 3 Then
val = 2
ElseIf  [BO_FUNK] = 2 Then
val = 3
ElseIf  [BO_FUNK] = 1 Then
val = 4
Else
val = 0
End If


In the second text box (fieldname), enter
val
Cheers
0 Kudos
ChristianSchreiner
New Contributor II
Thanks for your replies.

The 5 - [BO_FUNK] would have been great. I forgot to say, that I also have 0 that will be 99 in the new field. But anyway, thanks for reminding me of good old maths 101. 🙂

The IF THEN ELSE worked fine. Thanks for your advice.

Great forum here.

Christian
0 Kudos