2

Table and Field Calculator

I want to calculate the field "area_type" with text depending on numbers from the "Value" field. I have tried with the following VB-script in "Field Calculator" but am getting this error message:

"The calculated value is invalid for the row with ObjectID = 1. For example, the calculated value may be too large for the field or you may be trying to add a string to a number field. This row will not be updated".

My "area_type" field type is string and "Value" is type long.

Dim area_type

Select Case [value]

Case "1"
  area_type = "aapent omraade"
Case "11"
  area_type = "bebygd"
Case "12"
  area_type = "samferdsel"
Case "21"
  area_type = "fulldyrka jord"
Case "22"
  area_type = "overflatedyrka jord"
Case "23"
  area_type = "innmarksbeite"
Case "30"
  area_type = "skog"
Case "50"
  area_type = "aapen fastmark"
Case "60"
  area_type = "myr"
Case "70"
 area_type = "isbre"
Case "80"
 area_type = "vann"
Case "99"
 area_type = "ruralt (ikke kartlagt)"
End Select
3
  • What is the length of the field area_type? It might be too short to calculate certain long values such as "overflatedyrka jord" Commented Nov 25, 2015 at 8:56
  • The length is 255. I think that is default for string.
    – Alexander
    Commented Nov 25, 2015 at 9:08
  • 50 is default in 10.3.1. However, the field can be specified with another length which is why it's important to verify that :) Commented Nov 25, 2015 at 9:25

1 Answer 1

4

You have mixed the VB Script approach to Field Calculations with the Python approach. With VB Script the Expression should not say the input field name, it should say the output variable. Python has to use the Expression to feed field values to the Pre-Logic Script Code section, but VB Script does not. Therefore your expression should not be Value and should instead be area_type.

You also have to use integer values for the case values, not strings. You should also have a Case Else to use the existing field value for the situation where you left out a code, since something still needs to be written to the area_type field. Therefore the Pre-Logic Script Code should instead be:

Select Case [value]

Case 1
  area_type = "aapent omraade"
Case 11
  area_type = "bebygd"
Case 12
  area_type = "samferdsel"
Case 21
  area_type = "fulldyrka jord"
Case 22
  area_type = "overflatedyrka jord"
Case 23
  area_type = "innmarksbeite"
Case 30
  area_type = "skog"
Case 50
  area_type = "aapen fastmark"
Case 60
  area_type = "myr"
Case 70
 area_type = "isbre"
Case 80
 area_type = "vann"
Case 99
 area_type = "ruralt (ikke kartlagt)"
Case Else
 area_type = [area_type]
End Select

The area_type field is big enough with the default 50 characters for these values. The error is occurring because the calculation never sets a variable called value, so nothing is ever returned. By changing value to area_type a variable value will be returned.

1
  • It worked! Appreciate it Mr. Fairhurst.
    – Alexander
    Commented Nov 27, 2015 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.