Select the SDE connection (source tab) of a layer in arcmap (9.3.1)

618
2
04-16-2010 08:07 AM
JohnHansen
New Contributor III
(previously posted in another forum - these new ones will take some time...)

Been banging my head on this one for too long. Would like to know how to programmatically (preferably vba) select (highlight) an SDE Connection in the source tab (see attached image). The mxd is for editing versioned sde data, and the user will be changing versions regularly, but the layers in the map will remain static. Thus, I know that the layer 'PageIndex' will always be in the map, but it's source (SDE Version) will always be different. I am sure that arcmap knows what the source of that layer is, I just can't seem to figure out how to expose that information (and then select it).


Here is my latest (feable) attempt at this - any help would be appreciated:

Sub SelectVersion_old()
'not working

Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim i As Integer
Dim LayerName As String
Dim pTOC As IContentsView
Dim workspaceFactory As IWorkspaceFactory
Dim pDocDataset As IDocumentDatasets
Dim pDataset As IDataset
Dim pDSName As IDatasetName
Dim pFeatureLayer As IFeatureLayer
Dim pVersion As IVersion
Dim pWorkspace As IWorkspace

' Set a reference to the current map document
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
Set pTOC = pMxDoc.CurrentContentsView
Set workspaceFactory = New SdeWorkspaceFactory
'sDatasetName = pDataset.Name

LayerName = "PageIndex"

' Determine the position or order of the Layer in the TOC
For i = 0 To pMap.LayerCount - 1
If UCase(pMap.Layer(i).Name) = UCase(LayerName) Then
' Make the layer active (like you selected it)
'pTOC.SelectedItem = pMap.Layer(i)
Set pDataset = pMap.Layer(i)
'Set pDSName = pDataset.Name
Set pWorkspace = pDataset.workspace

'Set pWorkspace = workspaceFactory
'pTOC.SelectedItem = pWorkspace
'Set pDataset = pFeatureLayer.FeatureClass
'Set pVersion = pWorkspace
'Set pMap.SelectedItem = pWorkspace
'MsgBox "Version: " & pVersion.VersionName
End If
Next i

' Refresh the table of contents (TOC)
pMxDoc.UpdateContents
End Sub
0 Kudos
2 Replies
KirkKuykendall
Occasional Contributor III
this works for me
Option Explicit
Sub Test()
    'highlight the first layer's workspace in the Source contentsview
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    Dim pFLayer As IFeatureLayer
    Set pFLayer = FindLayer(pMxDoc.FocusMap, "MyLayer")
    If pFLayer Is Nothing Then
        MsgBox "layer not found"
        Exit Sub
    End If
    
    Dim pDS As IDataset
    Set pDS = pFLayer.FeatureClass
    
    Dim pCV As IContentsView
    Dim l As Long
    For l = 0 To pMxDoc.ContentsViewCount - 1
        If pMxDoc.ContentsView(l).Name = "Source" Then
            Set pCV = pMxDoc.ContentsView(l)
            Exit For
        End If
    Next l
    If Not pCV Is Nothing Then
        Set pMxDoc.CurrentContentsView = pCV
        pCV.SelectedItem = pDS.Workspace
    End If
End Sub

Function FindLayer(pMap As IMap, s As String) As ILayer
    
    Set FindLayer = Nothing
    If pMap.LayerCount = 0 Then
        Exit Function
    End If
    Dim pEnumLayer As IEnumLayer
    Set pEnumLayer = pMap.Layers
    Dim pLayer As ILayer
    Set pLayer = pEnumLayer.Next
    Do Until pLayer Is Nothing
        If UCase(pLayer.Name) = UCase(s) Then
            Set FindLayer = pLayer
            Exit Do
        End If
        Set pLayer = pEnumLayer.Next
    Loop
    
End Function
0 Kudos
AmyRamsdell
New Contributor III
How would you loop through the contents of the Source tab instead of just getting the selected item? I am trying to figure out how to get a list of file geodatabases that are used in the mxd.
0 Kudos