Label Expression (VBS)

673
13
Jump to solution
01-06-2021 12:12 PM
Labels (1)
Lauren-H
New Contributor II

I am trying to add a line to an existing script. I have created a new attribute field [PlantDate], and need it to reflect in the label expression.

This is the current script: 

 

 

Function FindLabel ( [FieldName], [FarmAcres], [FutureCrop1], [FutureCrop2], [FutureCrop3], [IrrigationType] )

      If (IsNull ([FutureCrop2])  AND IsNull ([FutureCrop3])) Then

              FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1]  & vbCrLf& [IrrigationType]

   ElseIf IsNull ([FutureCrop3]) Then

               FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1] + "/ " + [FutureCrop2]  & vbCrLf& [IrrigationType]

   Else

               FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1] + "/ " + [FutureCrop2] + "/ " + [FutureCrop3]  & vbCrLf& [IrrigationType]

   End If

End Function

 

 

 

I need it to include the new "plant date" field [PlantDate] on the next line. I did the following script but it is only showing the features of the original script so I am unsure what I am doing wrong (note: the field is not <null>, I have a value in the field for this polygon). This is the script I tried: 

 

 

Function FindLabel ( [FieldName], [FarmAcres], [FutureCrop1], [FutureCrop2], [FutureCrop3], [IrrigationType], [PlantDate] )
      If (IsNull ([FutureCrop2])  AND IsNull ([FutureCrop3])) Then
              FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1]  & vbCrLf& [IrrigationType] & vbCrLf& [PlantDate]
   ElseIf IsNull ([FutureCrop3]) Then
               FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1] + "/ " + [FutureCrop2]  & vbCrLf& [IrrigationType] & vbCrLf& [PlantDate] 
   Else
               FindLabel = "Field #" + [FieldName]  & vbCrLf & [FarmAcres]  + " Acres"   & vbCrLf& [FutureCrop1] + "/ " + [FutureCrop2] + "/ " + [FutureCrop3]  & vbCrLf& [IrrigationType] & vbCrLf& [PlantDate]
   End If
End Function

 

 

 

The system says it is valid, but it is not showing that added [PlantDate] information on the polygon. Refreshing and reloading is not correcting it either.  Ideally, I would like it to have the text "Plant Date" following the value (like I have ' + " Acres" ' for field acres) but it says there is an error when I attempt, and it blues out the word "date." 

Any help/advice would be great appreciated! Thank you. 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Sound like giving up talk.

Just a typo on the 3rd Line, also forgot to include irrigationtype.

 

def FindLabel([FieldName], [FarmAcres], [FutureCrop1], [FutureCrop2], [FutureCrop3], [IrrigationType], [PlantDate]):
    
    if [FieldName] is not None and [FarmAcres] is not None and [PlantDate] is not None and [IrrigationType] is not None:


        if [FutureCrop2] is None and [FutureCrop3] is None:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

        elif [FutureCrop3] is None:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

        else:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "/" + [FutureCrop3] + "\n" + [IrrigationType] + "\n" + str([PlantDate])


        return result

    else:

        return None

View solution in original post

0 Kudos
13 Replies
DavidPike
MVP Frequent Contributor

Are you happy doing it in Python? My VBA knowledge is non existent! (although not sure that + is being used in the right way instead of &?, also is that the right fieldname and not an alias?)

If so, can you just give some pseudo code or example of the intended output.

Hopefully a VBA ninja can help you out though.

JoeBorgione
MVP Esteemed Contributor

@DavidPike , my vb script skills are about the same as yours.  I think the plus sign is being used to join the strings in quotes with the value of the field in square brackets.  The ampersand concatenates expressions and Vbcrlf is a carriage return. Vbnewline is what I used to use.

I agree though, Python is going to be around longer than any of the vb flavours and  suggest taking that route. Label expressions are a great way to get started.

 

That should just about do it....
0 Kudos
Lauren-H
New Contributor II

So as far as I'm aware, the + is to add the text to the end, otherwise it would just show the value. i.e. if the value for [FarmAcres] is 84, it would show only 84. But with the " + Acres" it adds the text "Acres" to show "84 Acres" instead.

So the intended output is this (with the line breaks):

Field# [FieldName]
[Farm Acres] Acres
[FutureCrop1]
[Irrigation Type]
[PlantDate]

So if values on a particular polygon feature is completed it would look like this: 

Field #2
84 Acres
Walnuts
Micro Irrigation Sprinkler
2018


(and then obviously, we have the conditions in there that if there are values in [FutureCrop2] or [FutureCrop3] that it will display those if applicable.)

I'm not against changing to Python as I am not versed in VBA either, but I'm also not versed in Python... I am very much a novice in all things ArcGIS. But if Python is easier to use/learn, I could look into it. 

The alias and field name are the same, so I'm not sure if I did that correctly, but that has worked for me in the past. 

Thanks so much for your response! I hope I answered all your questions adequately. 

DavidPike
MVP Frequent Contributor

this should do it:

def FindLabel([FieldName], [FarmAcres], [FutureCrop1], [FutureCrop2], [FutureCrop3], [IrrigationType], [PlantDate]):
    

    if [FutureCrop2] is None and [FutureCrop3] is None:
        result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

    elif [FutureCrop3] is None:
        result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

    else:
        result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "/" + [FutureCrop3] + "\n" + [IrrigationType] + "\n" + str([PlantDate])


    return result
        
Lauren-H
New Contributor II

Thanks so much for that, @DavidPike . Unfortunately, I think I am doing something wrong. I changed the language to Python, but it is giving me this message: 

Invalid Expression
Traceback (most recent call last):
   File "<expression>", line 1, in <module>
   File "<string>", line 5, in FindLabel
TypeError: must be str, not NoneType

0 Kudos
DavidPike
MVP Frequent Contributor

It's, likely the case that any of FieldName, FarmAcres and PlantDate are NULL/None type values, which are trying to be concatenated and failing. 

There's many ways to work around this, below is an example but it really depends if you want to catch the error or adjust the input data. (if this is the issue).

 

def FindLabel([FieldName], [FarmAcres], [FutureCrop1], [FutureCrop2], [FutureCrop3], [IrrigationType], [PlantDate]):
    
    if [FieldName]is not None and [FarmAcres] is not None and [PlantDate] is not None:


        if [FutureCrop2] is None and [FutureCrop3] is None:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

        elif [FutureCrop3] is None:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "\n" + [IrrigationType] + "\n" + str([PlantDate])

        else:
            result = "Field #" + [FieldName] + "\n" + str([FarmAcres]) + " Acres" + "\n" + [FutureCrop1] + "/" + [FutureCrop2] + "/" + [FutureCrop3] + "\n" + [IrrigationType] + "\n" + str([PlantDate])


        return result

    else:

        return "Error - Null Value in FarmAcres, FieldName or PlantDate"
        
0 Kudos
Lauren-H
New Contributor II

Thanks so much! We only want it to show whatever values are in there. If a value is null or none, we want it to just exclude that value. I really appreciate you spending time on this. We rarely have to change our labels (hence why we are still using VB) but this helps me understand this process better. I'm the only one for my company working in this program so I really value this input, thank you!

0 Kudos
DavidPike
MVP Frequent Contributor

Did it work?

If so, in the final else block, just return None, or delete the last else entirely.

0 Kudos
Lauren-H
New Contributor II

Invalid Expression
 File "<string>",line 3
  if esri__0is not None and esri__1 is not None and
esri__6 is not None:
                          ^
SyntaxError: invalid syntax



Don't feel like you have to continue working on this if you cannot, I will continue digging or reach out to support if needed. Thanks for everything so far.

0 Kudos