ArcGIS is just plain lousy at dealing with any relationship that isn't one-to-one.
We all have a slew of hacks just to deal with this limitation. I for one regularly am creating temporary cross-tab queries so I can represent multiple sample results at a collection point, fish surveyed at a cross-section and a host of other relationships.
The classic example for mapping comes from the cadastral community and condominium lots. It's an odd situation where more than one person has title to the same piece of ground. How do you represent this?
I've got a new trick thanks to Mohammed Hoque's article in ArcUser Magazine.
We're going to do a database query inside a label expression, loop through the results and output the entire list to label.
For our example we'll use Outfitting Areas in Idaho and we'll label them with the Outfitters and Guide License Numbers and Outfitter Names.
1.) Open ArcGIS and add your spatial layer with the unique identifier shared with your database.
2.) In the label expression, click Advanced
3.) Replace the labeling expression with the following:
This example also uses SQL Server, different databases require different database connection strings:
The final product:
Link
We all have a slew of hacks just to deal with this limitation. I for one regularly am creating temporary cross-tab queries so I can represent multiple sample results at a collection point, fish surveyed at a cross-section and a host of other relationships.
The classic example for mapping comes from the cadastral community and condominium lots. It's an odd situation where more than one person has title to the same piece of ground. How do you represent this?
I've got a new trick thanks to Mohammed Hoque's article in ArcUser Magazine.
We're going to do a database query inside a label expression, loop through the results and output the entire list to label.
For our example we'll use Outfitting Areas in Idaho and we'll label them with the Outfitters and Guide License Numbers and Outfitter Names.
1.) Open ArcGIS and add your spatial layer with the unique identifier shared with your database.
2.) In the label expression, click Advanced
3.) Replace the labeling expression with the following:
Function FindLabel ([ID])You'll need to replace the bold values with those appropriate for your situation.
Dim strQry, strInfo, i
i = 1
strQry = "SELECT Outfitter FROM VU_GIS_Labeling WHERE ID = " & [ID]
Dim Conn
set Conn = createobject("ADODB.Connection")
Dim rs
set rs = createObject("ADODB.Recordset")
Conn.Open "PROVIDER=SQLOLEDB;Integrated Security=SSPI; Persist Security Info=False; Initial Catalog=aDatabaseName; Data Source=aServerName"
Conn.CursorLocation = 3
rs.Open strQry, Conn, 3, 1, 1
'if more than one records are found, append to the existing string.
Select Case rs.RecordCount
Case -1, 0
'If no record is found, return empty string
strInfo = ""
Case 1
'reading only the first record
strInfo = rs.Fields("Outfitter")
Case Else
Do While Not rs.eof
'if multiple records indicate how many using count
strInfo = strInfo & vbNewLine & rs.Fields("Outfitter") & " (" & i & ")"
i = i + 1
rs.movenext
Loop
End Select
'closing connections this is a must
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
'returning string for labeling
FindLabel = strInfo
End Function
This example also uses SQL Server, different databases require different database connection strings:
Oracle4.) Finally, test your label expression for typos using the Verify button and if successful, OK your way out.
“PROVIDER=OraOLEDB.Oracle; Data Source=aDatabaseName; User ID=aUserName; Password=aPassword”
MySQL
“driver={MySQL ODBC 3.51 Driver}; Server=aServerName; Database=aDatabaseName; uid=aUserName; PWD=aPassword”
Microsoft Access
“PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=c:\myDatabase.mdb;”
Microsoft SQL Server (using Windows NT Integrated security)
“Provider=SQLOLEDB; Integrated Security=SSPI; Persist Security Info=False; Initial Catalog=aDatabaseName; DataSource=aServerName”
The final product:
Link
Comments
1.) You don't have labels turned on (check box on labels tab in layer properties)
or
2.) You are not using the right connection. Try the MS Jet connection listed above.
If this fails paste your layer expression below.
Function FindLabel ( [PIN] )
Dim strQry, strInfo, i
i = 1
strQry = "SELECT Permit_Number FROM 2008_Zoning_Permits WHERE PIN = " & [PIN]
Dim Conn
set Conn = createobject("ADODB.Connection")
Dim rs
set rs = createObject("ADODB.Recordset")
Conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=f:\Documents\Planning\Townships\Evangeline\Zoning\Evangeline Zoning Permits.mdb;"
Conn.CursorLocation = 3
rs.Open strQry, Conn, 3, 1, 1
'if more than one records are found, append to the existing string.
Select Case rs.RecordCount
Case -1, 0
'If no record is found, return empty string
strInfo = ""
Case 1
'reading only the first record
strInfo = rs.Fields("Permit_Number")
Case Else
Do While Not rs.eof
'if multiple records indicate how many using count
strInfo = strInfo & vbNewLine & rs.Fields("Permit_Number") & " (" & i & ")"
i = i + 1
rs.movenext
Loop
End Select
'closing connections this is a must
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
'returning string for labeling
FindLabel = strInfo
End Function
I just took your label expression and crafted an MS Access database and shapefile to match. Changing only part of the path of your label expression everything worked perfectly.
You said the expression verified correctly, however when you hit verify do you see a sample label?
I have a couple ideas of what might still be causing you issues. One possibility is that the data types do not match up. In the SQL Expression strQry I am comparing an integer to an integer (strQry = "SELECT Permit_Number FROM 2008_Zoning_Permits WHERE PIN = " & [ID]). Integers are by far the easiest to compare. If PIN is a string in your database you will have to change the syntax to correctly quotate the [ID] field.
Another possibility is that there is either a lock on the database from another user/application (is there an .ldb file with the same name in the directory) or a security issue. The best way to test this is to make a copy of the database in a directory where you know you have full read/write privileges. And although I was able to manipulate the database in MS Access while labeling, having both open at the same time may cause issues as well.
Finally, you really want to be working with an MS Access table and not a query. Queries will theoretically work but are horridly slow in ArcGIS and may timeout.
Because your PIN field is a string you need to change your SQL expression. For MS Access JET this strings are delimited with single quotes. So you change:
strQry = "SELECT Permit_Number FROM 2008_Zoning_Permits WHERE PIN = " & [ID]
to
strQry = "SELECT Permit_Number FROM 2008_Zoning_Permits WHERE PIN = '" & [ID] & "'"
And you should be good. I don't think the lock file (.ldb) will get in your way. You should be golden.
Thank you again.
Take care,
Shannon
I used this vbscript and it worked great. However I would like to replace the count (1) with an additional field from the database. I tried several things that I thought might work, but with no success. I also could not figure out how to take the space off the top line. You can see this when you add a text box behind the label. I thought that removing &vbnewline& would take it out, but it put everything on one line.
Thanks for posting this.
If rs.Fields("Code") = "W" Then strInfo = rs.Fields("State_Rank")
but I am not sure where to place this in the code.
If rs.Fields("Code") = "W" Then
strInfo = rs.Fields("State_Rank")
Else
strInfo = ""
End If
Of course if you are using a database other than SQL Server the quote syntax may be different, for example, MS Access uses single quotes (see comment above).
I am especially new to writing VB for labeling. My problem is that I am trying to relate a .dbf file to a layer in ArcView, and I am trying to make labels from the related fields.
I am trying to label the multiple owners associated with a single property, and I can not get your code to work. Like I said, I am very new to the coding world, so please be gentle!
Thanks in advance for your help!
Each datasource has it's own connection string. My example uses SQL Server and I provided a number of different connections for other databases.
dbf files have their own connection string as well. You need to use the following connection string for dbase:
Conn.Open "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=C:\path\to\folder\in\which\dbf\resides"
In the SQL Query replace the table name (VU_GIS_Labeling in my example) with the name of the dbf file with no extension e.g. myfile.dbf type:
strQry = "SELECT Outfitter FROM myfile WHERE ID = " & [ID]
Where ID is the column name and [ID] is the column in GIS. Note that if it is a shapefile or odd GIS datasource the brackets may be quotes. You can see the correct "bracketing" by using the standard dropdowns to make your label and then switching to advanced.
Thanks for the quick response. I changed your code as shown below. Please check and see if you can see where I am messing up. "TID" is the field in my Shapefile the corresponds to "TRACTID" in the .dbf file.
I think there may be an issue. I have my .dbf related to my shapefile, but I can not see the fields in my .bdf as choices by which to label. Is that a problem?
I am trying to get it to label the field from my .dbf called LSE_NAME. Please let me know if you need any other information.
Feel free to e-mail me directly at potter.josh@hotmail.com
Function FindLabel ( [TID] )
FindLabel = Function FindLabel ([TID])
Dim strQry, strInfo, i
i = 1
strQry = "SELECT LSE_NAME FROM SandLakeSS WHERE TRACTID = " & [TID]
Dim Conn
set Conn = createobject("ADODB.Connection")
Dim rs
set rs = createObject("ADODB.Recordset")
Conn.Open "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=M:\Projects\Celero_Energy\Sand_Lake\Tables\SandLakeSS.dbf"
Conn.CursorLocation = 3
rs.Open strQry, Conn, 3, 1, 1
'if more than one records are found, append to the existing string.
Select Case rs.RecordCount
Case -1, 0
'If no record is found, return empty string
strInfo = ""
Case 1
'reading only the first record
strInfo = rs.Fields("LSE_NAME")
Case Else
Do While Not rs.eof
'if multiple records indicate how many using count
strInfo = strInfo & vbNewLine & rs.Fields("LSE_NAME") & " (" & i & ")"
i = i + 1
rs.movenext
Loop
End Select
'closing connections this is a must
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
'returning string for labeling
FindLabel = strInfo
End Function
End Function
You have a couple issues going on here:
1.) dbf files in every software on earth except ESRI software (grrr) are limited to 8 character filenames. Since we're using MS Jet to connect we have the same limitation, so change the name to "SandLake.dbf". http://allfaq.org/forums/t/143048.aspx
2.) You want to remove the filename from the path so that it just reads:
Conn.Open "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=M:\Projects\Celero_Energy\Sand_Lake\Tables"
3.) You're closing your function twice so remove the second END FUNCTION.
Good luck! and I'll email you the full function. Note, be sure you are in Advanced mode when using this Label Expression (checkbox above large textarea).