rename layer's name

1317
4
11-12-2010 07:22 PM
yosiwidiyanto
New Contributor
Hi,
Could anyone share vba code or example to rename the layer's name base on one of record's value?
Thank you for anyone who help

regards'

Yosie
0 Kudos
4 Replies
SteveFang
New Contributor III
Dim pMxdoc as imxdocument
set pMxdoc = thisdocument

'Get the feature (pFeature)
'Get the layer (pLayer)

pLayer.Name = pFeature.Value(Idx)
pMxdoc.UpdateContents

I am giving you the abbreviate untested version but something like this should work.
0 Kudos
yosiwidiyanto
New Contributor
I use userform to choose field value, I want the layer�??s name is the same as the selected list in the combo box. I get error at strLayer = pFeature.Value(pFeature.fields.FindField("Tanah"))
and the code is:

Private Sub cmdRename_Click()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap

Dim pLayer As IFeatureLayer
Dim pFeature As IFeature
Dim strLayer As String
Set strLayer = pFeature.Value(pFeature.fields.FindField("Tanah"))
pLayer.Name = pFeature.Value(strLayer)

pMxDoc.UpdateContent
End Sub

What should I do now to make this code run well? Thanks Steve
0 Kudos
AlexanderGray
Occasional Contributor III
Well based on a quick look, pFeature is nothing.  Declaring a variable without setting it to anything, will result in an 'empty' or nothing variable.  You cannot access any of the methods and properties on a nothing variable.

Also you will get an error on this line:
pLayer.Name = pFeature.Value(strLayer)
Because pLayer is nothing.  You need to set pLayer to something.  For example the first layer of the map: pLayer = pMxDoc.FocusMap.Layer(0).
Of course you are probably looking for a specific layer so you may need to loop through the layers in the map or something.

Once you have the layer you are interested in, you can Query Interface to get the IFeatureLayer (if it applies.)  IFeatureLayer has the FeatureClass property of type IFeatureClass.  The IFeatureClass has a search method that returns a featurecursor.  IFeatureCursor.NextFeature returns a feature of type IFeature.  That should give you a populated feature that has values in it.

another thing you should not use "Set" for a string.  In VBA "Set" is only used for object (quick rule of thumb if I can write the value down no set, if I can't, use set, works 99% of the time.)

Aside from that, field might not be found (findfield returns -1) or the value in that field for that feature could be null.

Good luck
0 Kudos
yosiwidiyanto
New Contributor
Thanks a lot. My code runs well now.
0 Kudos