Hi
Is there anybody who can help me with VBA coding in ArcMap? I want to write a macros that automatically set the field length for a number of fields in a feature class. As I understand it, I can't change what I set a variable as within the same procedure, so how do I have the code hit multiple different fields in the same procedure? I was thinking of doing a loop where it says that if pField.Name = x Then set length to n for each field. Would something like that work?
Thanks
Mehmet Berker
Ramsey County GIS
Help with VBA Procedure
Started by
Mehmet Berker
, Mar 17 2010 02:28 PM
5 replies to this topic
#1
Posted 17 March 2010 - 02:28 PM
#2
Posted 17 March 2010 - 11:29 PM
Hi Mehmet,
Do you mean you are creating fields in a feature class and need to set those lengths at the point of creation, or changing the display field length? Because you cannot change an existing field's type, length, precision, scale, etc.
You can easily add fields to an existing feature class and loop till you are done. This is taken from here.
However, I think I would suggest you look at doing this with the model builder/python. VBA is no longer supported after version 9.4 so your tool is no longer useful if it is something you plan on using into the future.
Hope that helps,
David
Do you mean you are creating fields in a feature class and need to set those lengths at the point of creation, or changing the display field length? Because you cannot change an existing field's type, length, precision, scale, etc.
You can easily add fields to an existing feature class and loop till you are done. This is taken from here.
Public Sub AddField() Dim pMxDoc As IMxDocument Dim pLayer As ILayer Dim pFLayer As IFeatureLayer Dim pFeatClass As IFeatureClass Dim NewField As IFieldEdit Set pMxDoc = ThisDocument Set pLayer = pMxDoc.SelectedLayer Set pFLayer = pLayer Set pFeatClass = pFLayer.FeatureClass 'I would start the loop probably here Set NewField = New Field With NewField .Type = 3 .Name = "NewField" End With pFeatClass.AddField NewField 'End the loop here End Sub
However, I think I would suggest you look at doing this with the model builder/python. VBA is no longer supported after version 9.4 so your tool is no longer useful if it is something you plan on using into the future.
Hope that helps,
David
#3
Posted 18 March 2010 - 06:21 AM
It woln't be 9.4 I heard at a meeting the other day they are jumping to 10.
#4
Posted 18 March 2010 - 11:55 AM
jrat, thanks for the word on Arc and VBA
David
Well this process is probably going to be a one-time thing, and I have written the part of the code to change field x's length. It looks like this (editlength is a procedure I'm calling to edit the length):
'Set map document to this document
Dim pMxD As IMxDocument
Set pMxD = ThisDocument
'Set Feature Layer
Dim pFLayer As IFeatureLayer
Set pFLayer = pMxD.FocusMap.Layer(0)
'Set feature class
Dim pFClass As IFeatureClass
Set pFClass = pFLayer.FeatureClass
'Declarations
Dim pFields As IFields
Dim pField As IField
Dim pFEdit As IFieldEdit
Dim x As Integer
Dim n As Integer
'Set Fields variable
Set pFields = pFClass.Fields
If pFields.FindFieldByAliasName("Entity_Code") Then
x = pFields.FindFieldByAliasName("Entity_Code")
Set pField = pFields.Field(x)
n = 1
editlength pField, n
End If
As of now, I'm repeating the If block over and over, but I'd like to write a Do While loop so that VBA can have a pointer go to each field to do a bunch of ElseIf statements for each different field I want to edit. I feel that that would be a more robust code.
So I guess what I really want to know is how I access the list of fields in my feature class so that I can iterate through them? Looking at the VBA help in Arc, I found a ListFields method under the IGpDispatch class, but I think that that creates an object and other hoopla, and I feel there's got to be an easier way, I just can't think of it.
Thanks for replying so fast,
Mehmet
David
Well this process is probably going to be a one-time thing, and I have written the part of the code to change field x's length. It looks like this (editlength is a procedure I'm calling to edit the length):
'Set map document to this document
Dim pMxD As IMxDocument
Set pMxD = ThisDocument
'Set Feature Layer
Dim pFLayer As IFeatureLayer
Set pFLayer = pMxD.FocusMap.Layer(0)
'Set feature class
Dim pFClass As IFeatureClass
Set pFClass = pFLayer.FeatureClass
'Declarations
Dim pFields As IFields
Dim pField As IField
Dim pFEdit As IFieldEdit
Dim x As Integer
Dim n As Integer
'Set Fields variable
Set pFields = pFClass.Fields
If pFields.FindFieldByAliasName("Entity_Code") Then
x = pFields.FindFieldByAliasName("Entity_Code")
Set pField = pFields.Field(x)
n = 1
editlength pField, n
End If
As of now, I'm repeating the If block over and over, but I'd like to write a Do While loop so that VBA can have a pointer go to each field to do a bunch of ElseIf statements for each different field I want to edit. I feel that that would be a more robust code.
So I guess what I really want to know is how I access the list of fields in my feature class so that I can iterate through them? Looking at the VBA help in Arc, I found a ListFields method under the IGpDispatch class, but I think that that creates an object and other hoopla, and I feel there's got to be an easier way, I just can't think of it.
Thanks for replying so fast,
Mehmet
#5
Posted 18 March 2010 - 05:43 PM
Something like this should work:
You'll want to check my syntax for the select case, but that's usually easier than a bunch of elseif statements. The index of fields starts at zero, whereas count starts at 1 hence the subtraction by 1. You could start i at a higher number since the earlier fields are housekeeping for ArcGIS.
If you haven't been to it yet, you should start to look at the ArcObjects websites. I find the version 9.2 website easier to navigate then the most recent incarnation. If you visit the page for say IFields then you will find all the methods and properties for that interface, including the FieldCount option.
Almost a year ago I started an unfinished series of VBA tutorials for ArcGIS. I tried to include the use of the ArcObjects website as much as possible. I don't say these are fantastic tutorials, you may find them useful, but are probably more advanced already. But as JRat pointed out will be obsolete. It's a shame, because VBA has a much gentler learning curve than say VB.NET or C#.
Hope that helps,
David
Dim i as integer For i = 0 to pFields.FieldCount - 1 Set pField = pFields.Field(i) Select case pField.Name Case = "x" editlength pField, n case = "y" editlength pField, n End Select Next
You'll want to check my syntax for the select case, but that's usually easier than a bunch of elseif statements. The index of fields starts at zero, whereas count starts at 1 hence the subtraction by 1. You could start i at a higher number since the earlier fields are housekeeping for ArcGIS.
If you haven't been to it yet, you should start to look at the ArcObjects websites. I find the version 9.2 website easier to navigate then the most recent incarnation. If you visit the page for say IFields then you will find all the methods and properties for that interface, including the FieldCount option.
Almost a year ago I started an unfinished series of VBA tutorials for ArcGIS. I tried to include the use of the ArcObjects website as much as possible. I don't say these are fantastic tutorials, you may find them useful, but are probably more advanced already. But as JRat pointed out will be obsolete. It's a shame, because VBA has a much gentler learning curve than say VB.NET or C#.
Hope that helps,
David
#6
Posted 19 March 2010 - 10:43 AM
David
Just tested it out, and the Select Case method seems to be working. Also, thank you for sending me those resources on the ArcObjects and the VBA tutorials.
Thanks again for the help,
Mehmet
Just tested it out, and the Select Case method seems to be working. Also, thank you for sending me those resources on the ArcObjects and the VBA tutorials.
Thanks again for the help,
Mehmet
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users


Sign In
Create Account
United States
Back to top









