VB Script Labeling for Attributes in One Field - how to stack and truncate labels?

574
10
08-18-2013 02:33 PM
TiaMorita
New Contributor III
I have a shapefile with a field I am using for labels. The attributes in this field are long company names. I will be publishing the contents of this map to ArcGIS Portal as a map service too, so I want to clean up my labels.

First, I'd like to stack the labels.
Second, I would like to truncate them by replacing common words in all these company names (eg. replace Company with "Co.", Beverage with "Bev.", Distributing with "Dist.", and so on...)

Is there a single script where I can accomplish both?

Thanks!
Tags (2)
0 Kudos
10 Replies
RichardFairhurst
MVP Honored Contributor
I have a shapefile with a field I am using for labels. The attributes in this field are long company names. I will be publishing the contents of this map to ArcGIS Portal as a map service too, so I want to clean up my labels.

First, I'd like to stack the labels.
Second, I would like to truncate them by replacing common words in all these company names (eg. replace Company with "Co.", Beverage with "Bev.", Distributing with "Dist.", and so on...)

Is there a single script where I can accomplish both?

Thanks!


Here is my suggested code assuming you have multiple company names separated by commas that you want to stack:

Function FindLabel([COMPANY_NAMES])

  Dim MyArray, i, names
  MyArray = Split([COMPANY_NAMES], ",")

  For i = 0 to UBound(MyArray)
    If i = 0 Then
      names = MyArray(i) ' Begin a single company.
    Else
      names = names & vbCrLf & MyArray(i) ' Insert a new line after every company name.
    End If
  Next
  Replace(names, "Limited Liability Company", "LLC")
  Replace(names, "Company", "Co")
  Replace(names, "Beverage", "Bev")
  Replace(names, "Distributed", "Dist")
  FindLabel = names
End Function
0 Kudos
TiaMorita
New Contributor III
Hi rfairhur24 - thanks for the post reply.

I tried to copy and paste the code into the expression window under the Labels tab, but I get an error message:
"The expression contains an error.... Error 8 on line 35. Cannot use parentheses when calling a Sub."

Also, just to be clear, the field I'm using for the labels looks something like this:

[ATTACH=CONFIG]26782[/ATTACH]


Thanks so much for your input!
0 Kudos
RichardFairhurst
MVP Honored Contributor
Hi rfairhur24 - thanks for the post reply.

I tried to copy and paste the code into the expression window under the Labels tab, but I get an error message:
"The expression contains an error.... Error 8 on line 35. Cannot use parentheses when calling a Sub."

Also, just to be clear, the field I'm using for the labels looks something like this:

[ATTACH=CONFIG]26782[/ATTACH]


Thanks so much for your input!


I edited the expression a little, but the error makes little sense, since there is no line 35 in my code and none of the functions I am using are Subs.  So the error is probably coming from an actual field value processing thought the internal code of the Replace expression, the Split or the array.  Please put a definition query on the layer to just label a single record (the one you showed) and repaste the expression.

Also, what is the actual name of the field that contains the company names?  How are the names stored in the field?  Does it already contain the newlines like your example showed, or did you add those?  What type is the field?  Is it a text field and less than 256 characters or is it another field type?  I am operating in the dark at this point, since I really don't know what your set up is.  I really need to know the field configuration in its raw form.
0 Kudos
TiaMorita
New Contributor III
Hi there,

In my polygon shapefile, the field name is "dist_name" (for Distributor Name). It is a text field with a limit of 50. Null values are allowed in this field (although there are none). Each row is a unique distributor name.

Do you think the issue could be if there are any other punctuation marks in the company names? For instance, a company name in this field might look like this: B & K Distributing, or "Northwest Colorado Beverage - PM".

Sorry I did not provide more info before... I did try using the expression on just one record, but I got the same error message.

Thanks again!
0 Kudos
TiaMorita
New Contributor III
Just thinking of the easiest way to tackle this... Would it be easier to just loop through all the names in this one "dist_name" field and every time a particular word comes up, replace it with a truncated version to shorten my labels? Eg: everytime "Distributing" comes up, replace it with "Dist."; everytime "Beverage" comes up, replace it with "Bev."; everytime "Company" comes up, replace it with "Co."? Would that be possible?

Thanks!
0 Kudos
RichardFairhurst
MVP Honored Contributor
Null values do matter and would cause an error, so I would have had to add a test for that.

Also, the expression does not stack multiple rows.  There is no label expression that will do that without using Python and a cursor, and I not totally certain even that will work.  I would have to look for some code to convert to Python, since I am not a user of that language normally.  So no point working out the VB script issues anymore.

Something for another day and possibly another Python user, but multi-row labels have come up repeatedly on the forums and I do not think anyone has come up with a solution since the VB6 label stacker routine stopped working off the user script page (and that was with a related table, not multiple rows in a single table).

Are the records that would stack related by any attributes or would they only stack due to some kind of proximity analysis?  No one has done any proximity based labeling code that I have seen.

Your question now goes way beyond Cartography and should be reposted on the Python forum with a much clearer description of your set up and requirements.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Edit:  I found the error in my code.  I have to assign the replace output back to the name variable as shown below.  Normally I use this in the Field Calculator and don't have to do that step, but it is required in a label expression.

Just for the sake of trying it, use this code.  It will do nothing to stack your labels, but it should do the word replacements without error:

Function FindLabel([dist_name])
  Dim name
  If IsNull([dist_name]) Then
    name = ""
  Else
    ' Add as many replace expressions as you need.
    ' The first string in the pair must match your input exactly to be replaced.
    ' The order of replace statements matters.
    ' Words that can be part of other replaced words must come last.
    name = [dist_name]
    name = Replace(name, "Limited Liability Company", "LLC")
    name = Replace(name, "Company", "Co")
    name = Replace(name, "Beverage", "Bev")
    name = Replace(name, "Distributed", "Dist")
  End if
  FindLabel = name
End Function


If you did the edits to the fields with the field calculator you would use the Replace expression anyway, i.e.:

Replace([dist_name], "Company", "Co")

So that statement will work unless there is something very strange in your field that I have never encountered before (none of the characters you mentioned would be special or trigger an error).

The only character that would throw an error in your company names that I know of is a double quote (").  Do you have that character in any actual company name?
0 Kudos
TiaMorita
New Contributor III
rfairhur24 - THANKS! This works just great and helps me to simplify/truncate my labels.
0 Kudos
TiaMorita
New Contributor III
Edit:  I found the error in my code.  I have to assign the replace output back to the name variable as shown below.  Normally I use this in the Field Calculator and don't have to do that step, but it is required in a label expression.

Just for the sake of trying it, use this code.  It will do nothing to stack your labels, but it should do the word replacements without error:

Function FindLabel([dist_name])
  Dim name
  If IsNull([dist_name]) Then
    name = ""
  Else
    ' Add as many replace expressions as you need.
    ' The first string in the pair must match your input exactly to be replaced.
    ' The order of replace statements matters.
    ' Words that can be part of other replaced words must come last.
    name = [dist_name]
    name = Replace(name, "Limited Liability Company", "LLC")
    name = Replace(name, "Company", "Co")
    name = Replace(name, "Beverage", "Bev")
    name = Replace(name, "Distributed", "Dist")
  End if
  FindLabel = name
End Function


If you did the edits to the fields with the field calculator you would use the Replace expression anyway, i.e.:

Replace([dist_name], "Company", "Co")

So that statement will work unless there is something very strange in your field that I have never encountered before (none of the characters you mentioned would be special or trigger an error).

The only character that would throw an error in your company names that I know of is a double quote (").  Do you have that character in any actual company name?


--------------
rfairhur24,

Thanks so much for your earlier help. This script has been working great! Just curious if there's a way to update it so it stacks my lllabels after the first word. For instance, if the company name is "Pacific Beverage Company", the script would still replace "Company" with "Co.", but stack the label where Beverage Company comes as a single line under Pacific.

Thanks for your help,
tiamo
0 Kudos