Jump to content

 
Photo

Label Expression for duplicate data in fields

- - - - -

  • Please log in to reply
5 replies to this topic

#1
petgis

petgis

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

I am a self tought gis fool...and I have some history in vb but no scripting knowledge at all :o

I have 2 fields that need to be shown. Some of the records show the same data in both fields. I want to write an expression that will show both fields only if they don't have the same data.

Here is the direction I was going with it...but i'm an idiot and have no idea what I am doing:

Function FindLabel ( [field1] , [field2] )

Dim lbl

lbl = [field1]



If ( [field1] = [field2] )

lbl = lbl

Else

lbl = lbl & [field2] 

End If

  FindLabel = lbl 

End Function

Any simple solutions for this? Any help will be much appreciated.

#2
SaultDon

SaultDon

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 153 posts
  • Gender:Male
  • Location:BC
  • Interests:Environmental/Wildlife GIS, Aboriginal Land Use and Occupancy
  • Canada

OK, I got this to work...

Below is a grid, labelled with two fields using the following expression:
[F1] & " - " & [F2]

Attached File  grid.png   11.59KB   22 downloads

This is the attribute table showing the selected records (ones that do not match, they will be labelled):

Attached File  attrib.png   2.9KB   13 downloads

Using the following label expression:
Function FindLabel ( [F1], [F2]  )    intComp = StrComp( [F1], [F2], vbTextCompare )      if intComp = -1 Then FindLabel = [F1]End Function
The above assumes that both fields are stored as Strings already, otherwise they will need to be stored as strings and converted to UCase or LCase to avoid case-sensitive issues:
Function FindLabel ( [F1], [F2]  )  str1 = UCase([F1])  str2 = UCase([F2])    intComp = StrComp( str1, str2, vbTextCompare )      if intComp = -1 Then FindLabel = [F1]End Function

Results in the following:

Attached File  grid_l.png   15.18KB   22 downloads



#3
heath b

heath b

    Key Contributor

  • Validated Member
  • PipPipPip
  • 71 posts
  • Gender:Male
  • Location:Portland, OR
  • Interests:the great outdoors, bouldering, martinis, unusual graphical representation
  • United States

You're missing a "Then" after the "If x = y". It should read like "If x = y then"

Also, you'll want to add a space or vbNewLine between the two labels: "lbl = x & " " & y" or "lbl = x & vbNewLine & y"

Should work like that.

Heath

#4
petgis

petgis

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

OK, I got this to work...

Below is a grid, labelled with two fields using the following expression:

[F1] & " - " & [F2]

Attached File  grid.png   11.59KB   22 downloads

This is the attribute table showing the selected records (ones that do not match, they will be labelled):

Attached File  attrib.png   2.9KB   13 downloads

Using the following label expression:
Function FindLabel ( [F1], [F2]  )    intComp = StrComp( [F1], [F2], vbTextCompare )      if intComp = -1 Then FindLabel = [F1]End Function
The above assumes that both fields are stored as Strings already, otherwise they will need to be stored as strings and converted to UCase or LCase to avoid case-sensitive issues:
Function FindLabel ( [F1], [F2]  )  str1 = UCase([F1])  str2 = UCase([F2])    intComp = StrComp( str1, str2, vbTextCompare )      if intComp = -1 Then FindLabel = [F1]End Function
Results in the following:

Attached File  grid_l.png   15.18KB   22 downloads

Thanks for the help, I will play around with this! I should have clarified that I always want to show a label..just don't want to show duplicates for the ones that have the same data.

#5
SaultDon

SaultDon

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 153 posts
  • Gender:Male
  • Location:BC
  • Interests:Environmental/Wildlife GIS, Aboriginal Land Use and Occupancy
  • Canada

Thanks for the help, I will play around with this! I should have clarified that I always want to show a label..just don't want to show duplicates for the ones that have the same data.


I thought that might be the case and someone asked the same question this morning on the ESRI forums! So to elaborate even more:

Function FindLabel ( [F1], [F2]  )    intComp = StrComp( [F1], [F2], vbTextCompare )      if intComp = -1 Then           FindLabel = [F1] & " - " & [F2]         Else          FindLabel = [F1]    End ifEnd Function

Attached File  label_2fields.png   15.8KB   16 downloads

The key to this script is the intComp value, where I have chosen to use -1 and it could possible be: 0, 1, or -1

See: strComp VB help to see what those possible values actually mean.

Like you: VB stuff I learn most of it myself.

#6
petgis

petgis

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

Thanks for the help, I will play around with this! I should have clarified that I always want to show a label..just don't want to show duplicates for the ones that have the same data.


I thought that might be the case and someone asked the same question this morning on the ESRI forums! So to elaborate even more:

Function FindLabel ( [F1], [F2]  )    intComp = StrComp( [F1], [F2], vbTextCompare )      if intComp = -1 Then           FindLabel = [F1] & " - " & [F2]         Else          FindLabel = [F1]    End ifEnd Function

Attached File  label_2fields.png   15.8KB   16 downloads

The key to this script is the intComp value, where I have chosen to use -1 and it could possible be: 0, 1, or -1

See: strComp VB help to see what those possible values actually mean.

Like you: VB stuff I learn most of it myself.


This works great! I really appreciate the help. :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->