2

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!

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 24, 2014 at 14:19
1
  • 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. Commented Apr 24, 2014 at 14:25

2 Answers 2

7

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))
answered Apr 24, 2014 at 14:26
0

Thanks a lot! Problem solved. Also, I found other errors (on slicing and I needed to convert to string to concatenate):

def FindLabel (field):
 Var1 = field [-2:]
 Var2 = field [7:10]
 A = int(Var1)
 B = int(Var2)
 if (int(Var1) <> 1 ):
 return str(B) + "-" + str(A)
 else:
 return str(B)
answered Apr 24, 2014 at 17:35
1
  • I think you should Accept Answer from @EvilGenius but leave this one here too Commented Apr 24, 2014 at 21:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.