Change of field order of shapefile attribute table in ArcMap 9.2

2525
8
02-04-2011 06:20 AM
KyungKim
New Contributor
Withe the following macro, I could open an attribute table of pFeatureLayer that was just added but could not change the field order as I intended.  Actually, the field order did not change at all.  The macro did not show an error message, though.  Probably the macro could not access the opened attribute table?  Or, what could be wrong in the following VBA macro?  Please, can anybody help me out?

Kyung

   ----
   ----

   ' Open the attribute table window.

    Dim pTableWindow As ITableWindow
    Set pTableWindow = New TableWindow
    Set pTableWindow.FeatureLayer = pFeatureLayer
    Set pTableWindow.Application = Application
    pTableWindow.TableSelectionAction = esriSelectFeatures
    pTableWindow.ShowAliasNamesInColumnHeadings = True
    pTableWindow.Show True
   
    ' Get the properties for this table window.

    Dim pTableProperties As ITableProperties
    Dim pEnumTableProperties As IEnumTableProperties
    Dim pTableProperty As ITableProperty
    Set pTableProperties = pMxDoc.TableProperties
    Set pEnumTableProperties = pTableProperties.IEnumTableProperties
    pEnumTableProperties.Reset
    Set pTableProperty = pEnumTableProperties.Next

    ' Re-order the fields

    pTableProperty.FieldOrder = "OBJECT_ID, UFI, MF, NT, UNI, LAT, LONG"
 
    ' Freeze the first 4 fields on the left side of the attribute table

    pTableProperty.FrozenFields = 4
    Dim pTableControl As ITableControl
    Set pTableControl = pTableWindow.TableControl
    pTableControl.Redraw
 
    ' Refresh the active view.

    pActiveView.Refresh

    ----
    ----
0 Kudos
8 Replies
KyungKim
New Contributor
Hi all,

As nobody commenting on this topic, I may ask a related question that may invoke some idea.  The same macro did work previously on the table based on personal geodatabase in ArcMap 9.1.  Now when I use the macro on the table based on shapefile in ArcMap 9.2, it does not work.  Does that fact ring a bell to you?  Did ESRI change ArcObject change some classes and properties and methods without showing them in documentation?  What can the problem be?

Thanks.

Kyung
0 Kudos
JonHall
Occasional Contributor II
Kyung-
After 
[INDENT]pTableProperty.FieldOrder = "OBJECT_ID, UFI, MF, NT, UNI, LAT, LONG" [/INDENT]

you need to do this...

[INDENT]pTableWindow.Refresh()  'necessary to show the new FieldOrder [/INDENT]

Also, your comma-delimited list has spaces after each comma; mine does not; not sure if that's an issue.
Good luck!
0 Kudos
KyungKim
New Contributor
Kyung-
After 
[INDENT]pTableProperty.FieldOrder = "OBJECT_ID, UFI, MF, NT, UNI, LAT, LONG" [/INDENT]

you need to do this...

[INDENT]pTableWindow.Refresh()  'necessary to show the new FieldOrder [/INDENT]

Also, your comma-delimited list has spaces after each comma; mine does not; not sure if that's an issue.
Good luck!


Thank you for your interest in this problem.  As your saying, I added pTableWindow.Refresh but it didn't work.  Then, I removed all spaces from the comma-delimited list, but it didn't work either....
Could you suggest some other things?

Thanks.

Kyung
0 Kudos
JonHall
Occasional Contributor II
Kyung-
When you say "it didn't work", is your code finishing with no errors, and when you open the table, the field order is unchanged?
Do you have any errorhandling in your VBA code to catch errors???

For example:

Public Sub ChangeFieldOrder()

On Error GoTo Errorhandler
<your code here>
    Exit Sub    'exit before errorhandler
Errorhandler:
            MsgBox "An error occurred in ChangeFieldOrder:" & Err.Description, vbCritical, "ChangeFieldOrder"
End Sub
0 Kudos
JonHall
Occasional Contributor II
Kyung-
That 'freeze fields' sample only works with ONE table, if you have more than one Table open in ArcMap, you need to step thru he enum of TableProperties until you find the one for the shapefile name you are trying to change, for example "Streets" :

pEnumTableProperties.Reset
Set pTableProperty = pEnumTableProperties.Next

Do While Not pTableProperty Is Nothing
'Test if it is a FeatureLayer or Standalonetable
pLayer = pTableProperty.Layer
If Not pLayer Is Nothing Then
   If UCase(pLayer.Name) = UCase("Streets") Then Exit Do 'Found the right pTableProperty!
Else
   pStandaloneTable = pTableProperty.StandaloneTable
   If UCase(pStandaloneTable.Name) = UCase("Streets") Then Exit Do 'Found the right pTableProperty!
End If
   pTableProperty = pEnumTableProperties.Next           
Loop
0 Kudos
KyungKim
New Contributor
Kyung-
When you say "it didn't work", is your code finishing with no errors, and when you open the table, the field order is unchanged?
Do you have any errorhandling in your VBA code to catch errors???

For example:

Public Sub ChangeFieldOrder()

On Error GoTo Errorhandler
<your code here>
    Exit Sub    'exit before errorhandler
Errorhandler:
            MsgBox "An error occurred in ChangeFieldOrder:" & Err.Description, vbCritical, "ChangeFieldOrder"
End Sub


I had no error handler, and running the macro after adding pTableWindow.Refresh caused no runtime error message, and there was no change of field order.
Now, I added an error handler, but the result was still no error message and no change of field order.
I always made sure that there is only one attribute table is open and there be no other table whether opened or not.

Kyung
0 Kudos
JonHall
Occasional Contributor II
Kyung-
I don't know what could be wrong. If you re-post the latest version of your code, maybe something will hit me. I tried this in VBA a couple years ago and gave up, so I share your frustration. Just got it working in ArcGIS 10, .NET, VS2010.


2 more things you could try:

1. set the .Application property before anything else:
[INDENT]Set pTableWindow.Application = Application
Set pTableWindow.FeatureLayer = pFeatureLayer
[/INDENT]

2. verify that you have the TableProperty for the correct Featurelayer, check it's FieldOrder before, and again after you reset it:  

[INDENT]
' Re-order the fields
Debug.Print("TableProperty FeatureLayer: " & pTableProperty.FeatureLayer.Name)
Debug.Print("TableProperty Original FieldOrder: " & pTableProperty.FieldOrder)
pTableProperty.FieldOrder = "OBJECT_ID, UFI, MF, NT, UNI, LAT, LONG"
Debug.Print("TableProperty Revised FieldOrder: " & pTableProperty.FieldOrder)
[/INDENT]

The newer interfaces, ITableWindow2 and ITableWindow3, take an iLayer instead if iFeatureLayer; I think those were added in 9.3 and 10.0.
0 Kudos
KyungKim
New Contributor
Kyung-
I don't know what could be wrong. If you re-post the latest version of your code, maybe something will hit me. I tried this in VBA a couple years ago and gave up, so I share your frustration. Just got it working in ArcGIS 10, .NET, VS2010.


2 more things you could try:

1. set the .Application property before anything else:
[INDENT]Set pTableWindow.Application = Application
Set pTableWindow.FeatureLayer = pFeatureLayer
[/INDENT]

2. verify that you have the TableProperty for the correct Featurelayer, check it's FieldOrder before, and again after you reset it:  

[INDENT]
' Re-order the fields
Debug.Print("TableProperty FeatureLayer: " & pTableProperty.FeatureLayer.Name)
Debug.Print("TableProperty Original FieldOrder: " & pTableProperty.FieldOrder)
pTableProperty.FieldOrder = "OBJECT_ID, UFI, MF, NT, UNI, LAT, LONG"
Debug.Print("TableProperty Revised FieldOrder: " & pTableProperty.FieldOrder)
[/INDENT]

The newer interfaces, ITableWindow2 and ITableWindow3, take an iLayer instead if iFeatureLayer; I think those were added in 9.3 and 10.0.


Thank you, Jon!  How can I thank you enough!  Your line saved me!  The line-- Debug.Print("TableProperty FeatureLayer: " & pTableProperty.FeatureLayer.Name)-- gave me a shapefile name different than what I expected!  The shapefile was there on top inside the main data frame together with many other shapefiles, and the macro has been dufifully working on the shapefile, not on the new file that I intended to create on top of them and manipulate its attribute table.  I had thought that even with all existing shapefiles, if any of their attribute table is not open, it's like there are no attribute tables to enumerate, and the new shapefile that I create and open up its attribute table with my macro will be the only attribute table to deal with.  The truth is, it seems that the macro sees all shapefiles as attribute table to enumerate even though their attribute table is not opened in ArcMap.  Another thing that I found was that the macro's view scope is not limited to just one data frame in which I am working.  Actually, I tried the macro inside a new data frame where I have no existing shapefile or anything, but your line caught the top shapefile's name in the other data frame!  Yes, the cause was found out, and I used the Do Loop you suggested and I could do reordering, freezing of the attribute table!  Thank you so much, Jon!

Kyung
0 Kudos