3

I have 4 fields that hold number values. I only want a label returned if the field <>0. For instance, if col1<>0, then label = col1 AND if col2 <>0 then label = col2, etc. So, I want all four columns to show up as labels, but only if they are not equal to 0. I have tried finding info online and am having issues. Thought maybe everyone could help. The following script works, but only for one at a time. I need to combine them so that this script works with Seq2, Seq3 and Seq4. I am thinking there is a way to do multiple IF statements?

Function FindLabel ( [Seq1_TractDepthSequence], [Seq1_BegInterval] ,[Seq1_EndInterval], [Seq1_DeckCorpRITot], [Seq1_DeckCorpORRITot] , [Seq1_DeckCorpIntTot] , [Seq1_DeckCorpGWITot] )
if ( [Seq1_TractDepthSequence] = 1) AND ([Seq1_DeckCorpRITot] <> 0) then
 FindLabel = [Seq1_BegInterval] &" - "& [Seq1_EndInterval] &" : "& [Seq1_DeckCorpRITot] &" RI"
end if
End Function

The following won't work:

Function FindLabel ( [_Seq1_TractDepthSequence], [_Seq1_BegInterval] ,[_Seq1_EndInterval], [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpORRITot] , [_Seq1_DeckCorpIntTot] , [_Seq1_DeckCorpGWITot] )
if ( [_Seq1_TractDepthSequence] = 1) AND ([_Seq1_DeckCorpRITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpRITot] &" RI"
if ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpORRITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpORRITot] &" ORRI
if ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpIntTot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpIntTot] &" CorpInt"
if ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpGWITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpGWITot] &" WI"
else
end if
End Function
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 11, 2014 at 18:09

4 Answers 4

2

The reason your code was failing was you had multiple "if" statements without an "end if" for each of them. It looks like you wanted "elseif" for the second, third, and fourth checks.

Function FindLabel ( [_Seq1_TractDepthSequence], [_Seq1_BegInterval] ,[_Seq1_EndInterval], [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpORRITot] , [_Seq1_DeckCorpIntTot] , [_Seq1_DeckCorpGWITot] )
 if ( [_Seq1_TractDepthSequence] = 1) AND ([_Seq1_DeckCorpRITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpRITot] &" RI"
 elseif ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpORRITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpORRITot] &" ORRI
 elseif ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpIntTot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpIntTot] &" CorpInt"
 elseif ( [_Seq1_TractDepthSequence] = 1) AND ( [_Seq1_DeckCorpGWITot] <> 0) then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpGWITot] &" WI"
 else
 end if
End Function

Since you're always checking whether the _Seq1_TractDepthSequence field is = 1, try something like this to condense the code.

Function FindLabel ( [_Seq1_TractDepthSequence], [_Seq1_BegInterval] ,[_Seq1_EndInterval], [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpORRITot] , [_Seq1_DeckCorpIntTot] , [_Seq1_DeckCorpGWITot] )
 if [_Seq1_TractDepthSequence] = 1 then
 if [_Seq1_DeckCorpRITot] <> 0 then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpRITot] &" RI"
 elseif [_Seq1_DeckCorpORRITot] <> 0 then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpORRITot] &" ORRI
 elseif [_Seq1_DeckCorpIntTot] <> 0 then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpIntTot] &" CorpInt"
 elseif [_Seq1_DeckCorpGWITot] <> 0 then
 FindLabel = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "& [_Seq1_DeckCorpGWITot] &" WI"
 end if
 end if
End Function

Based on your comment below, try this code.

Function FindLabel ( [_Seq1_TractDepthSequence], [_Seq1_BegInterval] ,[_Seq1_EndInterval], [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpORRITot] , [_Seq1_DeckCorpIntTot] , [_Seq1_DeckCorpGWITot] )
 if [_Seq1_TractDepthSequence] = 1 then
 label = [_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "
 if [_Seq1_DeckCorpRITot] <> 0 then label = label & [_Seq1_DeckCorpRITot] &" RI "
 if [_Seq1_DeckCorpORRITot] <> 0 then label = label & [_Seq1_DeckCorpORRITot] &" ORRI "
 if [_Seq1_DeckCorpIntTot] <> 0 then label = label & [_Seq1_DeckCorpIntTot] &" CorpInt "
 if [_Seq1_DeckCorpGWITot] <> 0 then label = label & [_Seq1_DeckCorpGWITot] &" WI"
 FindLabel = label
 end if
End Function
answered Aug 11, 2014 at 19:01
4
  • If using the above code, will it return a value for all 4 FindLabel results, if there is a case where none of them = 0? Commented Aug 11, 2014 at 19:44
  • Do you mean that if more than one field is greater than zero, you would want multiple fields in the label (such as Beginning Interval - End Interval : 45 RI, 12 ORRI, 2 CorpInt, 32 WI)? Could you modify your original question to show what types of output you're expecting? Commented Aug 11, 2014 at 19:53
  • Yes, that is what I mean. I was getting "0 RI 0 ORRI 25 CorpInt 12 WI". I wanted to remove the "0" and text portions to get "25 CorpInt 12 WI" only. Commented Aug 11, 2014 at 19:55
  • Check to see if the code I added above does what you want Commented Aug 11, 2014 at 20:11
0

I can think of several ways to do this, but maybe the simplest is to just build the label with all values, 0 included, then replace the 0's with empty spaces. Although honestly your function doesn't look like it's doing what you state. It looks like you're passing in seven fields, not four.

Not as familiar with VBScript as I used to be, but you could try something like the following in Python. You might have to adjust things a bit to fit your data.

def FindLabel(Field1, Field2, Field3, Field4):
 lbl = ' '.join(Field1, Field2, Field3, Field4)
 lbl.replace('0', '')
 return lbl
answered Aug 11, 2014 at 18:29
2
  • If I replace the 0 with a blank, that does not solve my problem, because then my label would still have the string of "RI" (or other) floating out in the label by itself. I want it to only put in the label string if the value is not 0 and I want it to put in four label strings based on the value of 4 fields. (yes, more than 4 fields are being used, but that does not interfere with my question) Commented Aug 11, 2014 at 18:42
  • Well, that's why I said you might have to adjust things to fit your data. Commented Aug 11, 2014 at 20:00
0

I found a way to get it to work! It only provides the label string if the query is true.

Function FindLabel ( [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpIntTot] )
 strExp1 = [_Seq1_DeckCorpRITot] &" RI"
 strExp2 = [_Seq1_DeckCorpIntTot] &" CorpInt"
if ([_Seq1_DeckCorpRITot] <> 0) then strExp = strExp1
if ([_Seq1_DeckCorpIntTot] <> 0) then strExp = strExp2
 FindLabel = strExp 
End Function

enter image description here

answered Aug 11, 2014 at 19:25
2
  • You may want to add this updated code to your original question as an edit. Commented Aug 11, 2014 at 19:51
  • What if both if statements are true? You'll default to the second value. You may be relying on only one being true in your data, but unless there's some constraint to enforce that, it's not a reliable method. Then again, maybe you want to default to the second value if both are true, in which case you're ok. Commented Aug 11, 2014 at 20:03
0

Two things I would try:

Simply export the selection for your query and turn on labeling. So your SQL query would be something like

(_Seq1_TractDepthSequence = 1 AND [_Seq1_DeckCorpRITot] <> 0) OR
(_Seq1_TractDepthSequence = 1 AND [_Seq1_DeckCorpORRITot] <> 0) 
...etc

Then export the selection. Your new layer will only contain those that are not = 0. You could then specify what needs to be labelled or create a separate "labelfield" with what you want labelled inside... OR...

I would go the lazy route and add a "LabelField" since your code seems redundant. In the calculate field:

[_Seq1_BegInterval] &" - "& [_Seq1_EndInterval] &" : "

And for your label modified from kenbuja:

 Function FindLabel ( [LabelField],[_Seq1_TractDepthSequence], [_Seq1_DeckCorpRITot], [_Seq1_DeckCorpORRITot] , [_Seq1_DeckCorpIntTot] , [_Seq1_DeckCorpGWITot] )
 if [_Seq1_TractDepthSequence] = 1 then
 if [_Seq1_DeckCorpRITot] <> 0 then Findlabel = [LabelField] & [_Seq1_DeckCorpRITot] &" RI "
 if [_Seq1_DeckCorpORRITot] <> 0 then Findlabel = [LabelField] & [_Seq1_DeckCorpORRITot] &" ORRI "
 if [_Seq1_DeckCorpIntTot] <> 0 then Findlabel = [LabelField] & [_Seq1_DeckCorpIntTot] &" CorpInt "
 if [_Seq1_DeckCorpGWITot] <> 0 then Findlabel = [LabelField] & [_Seq1_DeckCorpGWITot] &" WI"
 FindLabel = [LabelField]
 end if
End Function
answered Aug 11, 2014 at 20:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.