multi-line labels using multiple if then statements - vbs label expression

4801
9
Jump to solution
10-23-2012 09:00 AM
ChristopherBevilacqua
New Contributor III
Hi,

I am trying to label polygons using multiple attributes.  I want to combine the attribute values with descriptive text.  Each attribute should be represented on a different row of the label.  I don't want to include a row in the label if the associated attribute value = 0.  I've been able to create the labels, with the text I want, on multiple lines by using this simple expression:

"Q1 2013: " & [Q1_2013] & " acres" & vbNewLine & "Q2 2013: " & [Q2_2013] & " acres" & vbNewLine & "Q3 2013: " & [Q3_2013] & " acres" & vbNewLine & "Q4 2013: " & [Q4_2013] & " acres" & vbNewLine & "Q1 2014: " & [Q1_2014] & " acres" & vbNewLine & "Q2 2014: " & [Q2_2014] & " acres" & vbNewLine & "Q3 2014: " & [Q3_2014] & " acres" & vbNewLine & "Q4 2014: " & [Q4_2014] & " acres" & vbNewLine & "2015: " & [Exp2015] & " acres" & vbNewLine & "2016: " & [Exp2016] & " acres" & vbNewLine & "2017: " & [Exp2017] & " acres" & vbNewLine & "Extension Acreage: " & [ExtAc]


This results in a stacked label with a lot of unnecessary rows of zero values, like this:

Q1 2013: 0 acres
Q2 2013: 0 acres
Q3 2013: 0 acres
Q4 2013: 0 acres
Q1 2014: 0 acres
Q2 2014: 133.3 acres
Q3 2014: 0 acres
Q4 2014: 0 acres
2015: 0 acres
2016: 0 acres
2017: 0 acres
Extension Acreage: 0

I've figured out how to not label features when a single attribute equals zero, but I haven't been able to figure out how to do this using all of the attributes I want to include in the labels.  For example, I've used this expression to label polygons with an attribute value that does not equal zero in the [Q1_2013] field:

Function FindLabel ( [Q1_2013]  )     if ( [Q1_2013] <> 0 ) then         FindLabel = "Q1 2013: " & [Q1_2013] & " acres"     end if End Function


In essence, I want to combine the two label expressions above to that I can have a stacked label that includes all of the attributes whose values do not equal zero.  I've tried defining a label class with an SQL query that specifies [Q1_2013] <> 0 OR [Q2_2013] <> 0...but that did not give me the expected results.  Any advice or suggestions would be greatly appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You can use something like this

Function FindLabel( [Q1_2013],  [Q2_2013], [Q3_2013] )      if ( [Q1_2013] <> 0 ) then              var = "Q1 2013: " & [Q1_2013] & " acres" & vbnewline   end if    if ( [Q2_2013] <> 0 ) then              var =  var & "Q2 2013: " & [Q2_2013] & " acres" & vbnewline   end if    if ( [Q3_2013] <> 0 ) then              var =  var & "Q3 2013: " & [Q3_2013] & " acres" & vbnewline   end if       FindLabel = var  End Function

View solution in original post

0 Kudos
9 Replies
DavidPetrey1
New Contributor III
Hi,

Try adding all the fields you want to use to the first line of the VB script e.g. Function FindLabel ( [Q1_2013] ,[Q2_2013],[Q3_2013] )

Then see if your script works a bit better.

Hope this helps. Please mark as answered if it does.

David
simplyGIS Ltd. Making GIS simple.
http://www.simplyGIS.co.uk
http://www.simplyGIS.co.uk/GISTraining.html
0 Kudos
ChristopherBevilacqua
New Contributor III
Thanks for the reply David.  That would be one step to go through.  However, I'd still need to apply the if...then statement to each of those fields.
0 Kudos
KenBuja
MVP Esteemed Contributor
You can use something like this

Function FindLabel( [Q1_2013],  [Q2_2013], [Q3_2013] )      if ( [Q1_2013] <> 0 ) then              var = "Q1 2013: " & [Q1_2013] & " acres" & vbnewline   end if    if ( [Q2_2013] <> 0 ) then              var =  var & "Q2 2013: " & [Q2_2013] & " acres" & vbnewline   end if    if ( [Q3_2013] <> 0 ) then              var =  var & "Q3 2013: " & [Q3_2013] & " acres" & vbnewline   end if       FindLabel = var  End Function
0 Kudos
ChristopherBevilacqua
New Contributor III
Thanks Ken.  I'll give that a try and will post results.
0 Kudos
ChristopherBlinn1
Occasional Contributor III
This has not been tested, but it should be close to what you need.

Function FindLabel ([Q1_2013],[Q2_2013],[Q3_2013],[Q4_2013],[Q1_2014],[Q2_2014],[Q3_2014],[Q4_2014],[Exp2015],[Exp2016],[Exp2017],[ExtAc])
 y = ""
 
 if ( [Q1_2013] <> 0 ) then
        x = "Q1 2013: " & [Q1_2013] & " acres"
  y = x
 end if
 
 if ( [Q2_2013] <> 0 ) then
        x = "Q2 2013: " & [Q2_2013] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q3_2013] <> 0 ) then
        x = "Q3 2013: " & [Q3_2013] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q4_2013] <> 0 ) then
        x = "Q4 2013: " & [Q4_2013] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q1_2014] <> 0 ) then
        x = "Q1 2014: " & [Q1_2014] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q2_2014] <> 0 ) then
        x = "Q2 2014: " & [Q2_2014] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q3_2014] <> 0 ) then
        x = "Q3 2014: " & [Q3_2014] & " acres"
  y = y & vbnewline & x
 end if
 
 if ( [Q4_2014] <> 0 ) then
        x = "Q4 2014: " & [Q4_2014] & " acres"
  y = y & vbnewline & x
 end if
 
 if ([Exp2015] <> 0 ) then
        x = "2015: " & [Exp2015] & " acres"
  y = y & vbnewline & x
 end if
 
 if ([Exp2016] <> 0 ) then
        x = "2016: " & [Exp2016] & " acres"
  y = y & vbnewline & x
 end if
 
 if ([Exp2017] <> 0 ) then
        x = "2017: " & [Exp2017] & " acres"
  y = y & vbnewline & x
 end if
 
 if ([ExtAc] <> 0 ) then
        x = "Extension Acreage: " & [ExtAc]
  y = y & vbnewline & x
 end if
 
FindLabel = y

End Function


You may need to tweak it to resemble the correct filed names.

Hope this helps!
Chris B.
0 Kudos
JasonSmith3
New Contributor
Is there a way to create a label expression with "if" and "and" statements

I would like to label particular sections within certain townships. The AND part is what I can not figure out.


Function FindLabel ([SECTION], [TOWNSHIP])
if (cLng([SECTION]) = 15) AND (TOWNSHIP] = 034N) then
FindLabel = "<CLR red='255'><FNT size = '14'>" + [SECTION] + "</FNT></CLR>"
else
FindLabel = [SECTION]
end if
End Function

Thank you, Jason
0 Kudos
KenBuja
MVP Esteemed Contributor
You should get in the practice of starting a new thread when your question isn't directly related to the original question.

Your Township field is a string field and needs quotes.

if (cLng([SECTION]) = 15) AND (TOWNSHIP] = "034N") then
0 Kudos
ChristopherBevilacqua
New Contributor III
Ken,

This worked great.  Thanks for the help!

Chris Bev.

You can use something like this

Function FindLabel( [Q1_2013],  [Q2_2013], [Q3_2013] )
  
  if ( [Q1_2013] <> 0 ) then         
    var = "Q1 2013: " & [Q1_2013] & " acres" & vbnewline
  end if 
  if ( [Q2_2013] <> 0 ) then         
    var =  var & "Q2 2013: " & [Q2_2013] & " acres" & vbnewline
  end if 
  if ( [Q3_2013] <> 0 ) then         
    var =  var & "Q3 2013: " & [Q3_2013] & " acres" & vbnewline
  end if 
  
  FindLabel = var

End Function
0 Kudos
KenBuja
MVP Esteemed Contributor
I'm glad to help.

Please remember to click the check on the post that best answered your question.
0 Kudos