'Listing for Using VBScript to BUild Complex Labels in ArcGIS article by Chip Hankley

'Figure 1: Sample expression code for simple example

<ACP><FNT name='Arial' size='12'>" &  [STATE_NAME]  & _
</FNT>" & vbnewline & _
<FNT name='Lucida Console' size='10'>Year 2000 Population: " & _
 formatnumber([POP2000],0,-1,-1,-1) & "</FNT></ACP>"




'Figure 2: Syntax for the FormatNumber function

FormatNumber(Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])




'Figure 3: Performing more advanced labeling

Function FindLabel([field1],[field2],,[fieldx])
  some code
  FindLabel = something
End Function



'Figure 4:  Sample expression code for advanced example

Function FindLabel([STATE_NAME], [POP2000], [MALES], [FEMALES])
  iMxLblSz = 0 'The MAX width of the descriptive label
  iMxValSz = 0 'The MAX width of the values
  iSpace = 5   'The minimum number of spaces (periods) 
               'between a label and a value

  'Build a nested array where each sub-array contains the descriptive name of
  'the field and the field value
  m = Array(Array("Year 2000 Population", [POP2000]), _
      Array("Male Population", [MALES]), _
      Array("Female Population", [FEMALES]))

  'Calculate the values for iMxLblSz and iMxValSz by looping through
  'the array and calculating the length of each element (tracking
  'the descriptive labels and values separately). The calculated size
  'is recorded, then checked against previous values, ultimately
  'identifying the longest (number of characters) value. Note that
  'when calculating lengths, we are calculating the length of the
  'formatted number (i.e. with commas) for the field value.
  For i = 0 To UBound(m)
    j = m(i)
    If (Len(j(0)) > iMxLblSz) Then 
      iMxLblSz = Len(j(0))
    End If
    If (Len(FormatNumber(j(1), 0, 0, 0, -1)) > iMxValSz) Then 
      iMxValSz = Len(FormatNumber(j(1), 0, 0, 0, -1))
    End If
  Next
  
  'START BUILDING THE LABEL
  'Make tags to format the label so that it's red if the population is
  'greater than 10 Million, otherwise make the label black.
  j = m(0) 'Re-set j to be the first sub-array in the array m
  If (j(1) > 10000000) Then
    strGT10M1 = "<CLR red='255' green ='0' blue='0'>"
  Else
    strGT10M1 = "<CLR red='0' green ='0' blue='0'>"
  End If
  
  'Make a closing tag
  strGT10M2 = "</CLR>"
  

  
  'Make the first line with the state name and begin generating the text
  'formatting tags.
  FindLabel = strGT10M1 + "<ACP><BOL><FNT name='Arial' size='10'>" & _
             [STATE_NAME] & "</FNT></BOL>" & vbNewLine & _
             "<FNT name='Lucida Console' size='8'>" & vbNewLine
  
  'Make a solid line by repeating the underscore character the 
  'correct number of times.
  FindLabel = FindLabel & String(iMxLblSz + iMxValSz + iSpace, "_") & _
              vbNewLine

  'Make the subsequent lines by looping through the array. For each
  'line we need to calculate the number of periods that we need to 
  'add between the descriptive label and the field value so that
  'each line has the same number of characters. Note that when we
  'do calculations on the field value, we are calculating against
  'the formatted (i.e. with commas) value.
  For i = 0 To UBound(m)
    j = m(i)

    'Calculate the number of periods that need to go between 
    'the descriptive label and the value by 1) subtracting the 
    'size of each from the maximum size calculated above, 2)
    'adding the minimum number of periods (iSpace).
    k = (iMxLblSz - Len(j(0))) + iSpace + _
        (iMxValSz - Len(FormatNumber(j(1), 0, 0, 0, -1)))

    'Build the label line by adding the field label, the right 
    'number of periods, then the number
    FindLabel = FindLabel + j(0) + String(k, ".") + _
                FormatNumber(j(1), 0, 0, 0, -1) + vbNewLine
  Next

  'Close the text formatting tags.
  FindLabel = FindLabel + "</FNT></ACP>" + strGT10M2

End Function











