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
4 Answers 4
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
-
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?Stephanie– Stephanie2014年08月11日 19:44:40 +00:00Commented 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?kenbuja– kenbuja2014年08月11日 19:53:28 +00:00Commented 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.Stephanie– Stephanie2014年08月11日 19:55:39 +00:00Commented Aug 11, 2014 at 19:55
-
Check to see if the code I added above does what you wantkenbuja– kenbuja2014年08月11日 20:11:18 +00:00Commented Aug 11, 2014 at 20:11
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
-
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)Stephanie– Stephanie2014年08月11日 18:42:32 +00:00Commented Aug 11, 2014 at 18:42
-
Well, that's why I said you might have to adjust things to fit your data.recurvata– recurvata2014年08月11日 20:00:38 +00:00Commented Aug 11, 2014 at 20:00
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
-
You may want to add this updated code to your original question as an edit.GISHuman– GISHuman2014年08月11日 19:51:51 +00:00Commented 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.recurvata– recurvata2014年08月11日 20:03:45 +00:00Commented Aug 11, 2014 at 20:03
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
Explore related questions
See similar questions with these tags.