Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have an expression in VBscript that works well for labeling:

Function FindLabel (field)
 if (INT(RIGHT(field, 2)) <> 1) then
   FindLabel = INT(MID(field, 8, 3)) &"-"& INT(RIGHT(field,2))
else
    FindLabel = INT(MID(field, 8, 3))
end if
End Function

This expression labels a text in this format: VCC065-00102 to: 1-2 or from VCC065-00501 to: 5 (using just the last 5 carachters, if the last one is > 1, it shows the number after a "-").

But, when I tried to use the same script for calculating a field, I've got a syntax error error:

Code block: the same as above

Expression:

FindLabel ([DATA])

Also, I tried to convert this script to Python:

Code Block:

def FindLabel (Field):
  Var1 = Field[-2:]
  Var2 = Field[8:-3]
  if (int(Var1)  <> 1 ):
    FindLabel = int(Var2) &"-"&  int(Var1)
  else:
    FindLabel = int(Var2)

Expression:

FindLabel (!DATA!)

But it returns an error: The field is not nullable. (I don't know why, but the expression is returning a null value)

Thanks!

share|improve this question
    
Have you tried selecting just a single record and running your script above to confirm it works? If you run the script on the entire dataset, and have even one record where !DATA! is blank or null, it could throw this error if the output field is not nullable. –  RyanDalton Apr 24 '14 at 14:25

1 Answer 1

Your Python code isn't actually returning a value, so that's why you are getting the not nullable error.

You need to use the return statement instead. Additionally, use + to concatenate strings instead of &:

def FindLabel (Field):
  Var1 = Field[-2:]
  Var2 = Field[8:-3]
  if (int(Var1)  <> 1 ):
    return str(int(Var2)) + "-" + str(int(Var1))
  else:
    return str(int(Var2))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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