2

I'm attempting to write a code to show 6 fields that all have some Null values. The fields will have accompanying text labels that are not a field, and each one needs to be on a different line.

The 6 fields are [TF] [INV] [INV_N] [INV_S] [INV_E] [INV_W]
The desired output would be
"TF [TF]"
"INV [INV]"
etc.

I have tried to do research and can't seem to find anything with this many values. The vb-script code I attempted to write is:

    Function FindLabel ( [TF], [INV], [INV_N] , [INV_S] , [INV_E] , [INV_W]  )
     FindLabel = "TF " & [TF] & vbCrLf  
      If IsNull( [INV] ) Then
     FindLabel = ( [INV_N] )
      If isNull ( [INV_N] ) Then
     FindLabel = ( [INV_S] )
      If isNull ( [INV_S] ) Then
    FindLabel =( [INV_E] )
     If isNull ( [INV_W] ) Then
    Else
     FindLabel = "INV " & [INV] &vbCrLf
    Else 
     FindLabel = "INV N " & [INV_N] & vbCrLf
    Else
     FindLabel = "INV S " & [INV_S] & vbCrLf
    Else
     FindLabel = "INV E " & [INV_E] & vbCrLf
    Else
     FindLabel = "INV W " & [INV_W] 
    End If
    End If
    End If
    End If
    End If
    End If
   End Function

My coding experience is 1 Java class my freshman year of college so I'm pretty lost when it comes to this.

If the field has a Null I would not want the text to be shown nor for there to be an empty space

  • 4
    If you're starting mostly from scratch as far as coding goes, I'd suggest using Python instead of VB, VB has no future in ArcGIS. – Dan C Feb 22 '17 at 14:29
2

Just keep your if and end if together, checking for Null each time. If it's Null then do nothing (move to the next field), and if it's not Null then add the relevant text and value.

Function FindLabel ([TF], [INV], [INV_N], [INV_S], [INV_E], [INV_W])

FindLabel = "TF = " & ([TF])

if not isNull([INV]) Then
 FindLabel = FindLabel & vbnewline & "INV = " & ([INV])
end if
if not isNull([INV_N]) Then
 FindLabel = FindLabel & vbnewline & "INV_N = " & ([INV_N])
end if
if not isNull([INV_S]) Then
 FindLabel = FindLabel & vbnewline & "INV_S = " & ([INV_S])
end if
if not isNull([INV_E]) Then
 FindLabel = FindLabel & vbnewline & "INV_E = " & ([INV_E])
end if
if not isNull([INV_W]) Then
 FindLabel = FindLabel & vbnewline & "INV_W = " & ([INV_W])
end if

End Function

All this does is take the FindLabel string and adds to it when there's a value to add, constructing a longer string to output as the label. If all of them are Null it will only output the TF value.


As recommended in @DanC's comment, you should look into using Python for your labelling as VBScript is being phased out.

Change the Label Parser to Python (at the bottom of the Label Expression window), switch to Advanced and try this code:

def FindLabel ( [TF], [INV], [INV_N] , [INV_S] , [INV_E] , [INV_W]   ):
  myLabel = "TF = {0}".format([TF])
  if not [INV] is None:
    myLabel += "\nINV = {0}".format([INV])
  if not [INV_N] is None:
    myLabel += "\nINV_N = {0}".format([INV_N])
  if not [INV_S] is None:
    myLabel += "\nINV_S = {0}".format([INV_S])
  if not [INV_E] is None:
    myLabel += "\nINV_E = {0}".format([INV_E])
  if not [INV_W] is None:
    myLabel += "\nINV_W = {0}".format([INV_W])
  return myLabel

NOTE: Python requires consistent indents, so be careful that everything is indented correctly.

  • Smashing stuff. This will come in very useful. – Clubdebambos Feb 22 '17 at 18:11
-1

Use Python

enter image description here

Here's the expression

def FindLabel ( [TF], [INV], [INV_N] , [INV_S] , [INV_E] , [INV_W] ):
  return "TF: " + [TF] + "\nINV: " + [INV] + "\nINV_N: " + [INV_N] + "\nINV_S: " + [INV_S] + "\nINV_E: " + [INV_E] + "\nINV_W: " + [INV_W]

This will stake your labels. The + is concatenation and the \n means place what follows on a newline.

If any of these are numeric values you need to cast to string with str() example

str(TF)

Turn on Maplex Engine if you want to left justify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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