Jump to content

 
Photo

Help with VBA Procedure

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Mehmet Berker

Mehmet Berker

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

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

#2
dsl

dsl

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 225 posts
  • Gender:Male
  • Location:Denver
  • United States

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.

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
jrat

jrat

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 143 posts
  • Gender:Male
  • Location:Hagerstown, MD
  • United States

It woln't be 9.4 I heard at a meeting the other day they are jumping to 10.

#4
Mehmet Berker

Mehmet Berker

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

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

#5
dsl

dsl

    Master Contributor

  • Validated Member
  • PipPipPipPip
  • 225 posts
  • Gender:Male
  • Location:Denver
  • United States

Something like this should work:

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
Mehmet Berker

Mehmet Berker

    Newbie

  • New Member
  • Pip
  • 3 posts
  • United States

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->