Home arrow GIS Data & Resources arrow Scripts and Code arrow Visual Basic / VBA arrow VBA: Load Records Into GDB M:N Relationship Tables
VBA: Load Records Into GDB M:N Relationship Tables PDF Print E-mail

Written by Bert Granberg,

If you already have a table that describes the relationship between both sides of a many to many relationship, it would be nice to be able to load this into a geodatabase table. The following code attempts to do just that and requires that the temporary input table already be in the same file geodatabase that contains the related tables and the relationship class (must be pre-defined).

This seems to be harder than it needs to be as there should be a tool in the interface to do this. There is no 'Load' on the intermediate table's (a.k.a. m to n relationship class) context menu in ArcCatalog. Also the ArcToolBox Table to Relationship Class didn't seem to work. Hence the code below.

Another note. If you want the intermediate table to carry some attributes in a geodatabase, keep in mind that fields in the intermediate table can't utilize geodatabase domains. This is documented by ESRI in the ArcMap and ArcObjects help but seems like an unnecessary shortcoming.

 

Public Sub validateAndLoadMNRelationshipToGDBIntermediateTable()

    Dim pFGDB_WSF As IWorkspaceFactory
    Dim pFeatureWorkspace As IFeatureWorkspace
    Dim pWorkspaceEdit As IWorkspaceEdit
    Dim pTempInputIntermediateTable As ITable
    Dim pRelationshipClass As IRelationshipClass
    Dim pOriginTable As ITable
    Dim pDestinationTable As ITable
    
    Set pFGDB_WSF = New FileGDBWorkspaceFactory
    Set pFeatureWorkspace = pFGDB_WSF.OpenFromFile("D:\ZipInfo.gdb", 0)
    Set pWorkspaceEdit = pFeatureWorkspace
    
    Set pTempInputIntermediateTable = pFeatureWorkspace.OpenTable("Input_MtoN_Table")
    
    Set pRelationshipClass = pFeatureWorkspace.OpenRelationshipClass("ZipcodeToPlacename")
    Set pOriginTable = pRelationshipClass.OriginClass
    Set pDestinationTable = pRelationshipClass.DestinationClass
    
    
    Dim pRelCursor As ICursor
    Dim pRelRow As IRow
    Dim pOriginCursor As ICursor
    Dim pDestinationCursor As ICursor
    Dim pOriginQF As IQueryFilter
    Dim pDestQF As IQueryFilter
    Dim pOriginRow As IRow
    Dim pDestinationRow As IRow
    Dim pRelationship As IRelationship
    
    Set pOriginQF = New QueryFilter
    Set pDestQF = New QueryFilter
    
    Set pRelCursor = pTempInputIntermediateTable.Search(Nothing, True)
    Set pRelRow = pRelCursor.NextRow
    
    pWorkspaceEdit.StartEditing True
    pWorkspaceEdit.StartEditOperation
    
    Do Until pRelRow Is Nothing
    
        pOriginQF.WhereClause = "Zipcode = '" & pRelRow.Value(2) & "'"
        pDestQF.WhereClause = "Placename = '" & pRelRow.Value(1) & "'"
        
        Set pOriginCursor = pOriginTable.Search(pOriginQF, True)
        Set pDestinationCursor = pDestinationTable.Search(pDestQF, True)
        
        Set pOriginRow = pOriginCursor.NextRow
        Set pDestinationRow = pDestinationCursor.NextRow
        
        Set pWorkspaceEdit = pFeatureWorkspace
        
        If Not (pOriginRow Is Nothing Or pDestinationRow Is Nothing) Then
            Set pRelationship = pRelationshipClass.CreateRelationship(pOriginRow, pDestinationRow)
        End If
        
        Set pRelRow = pRelCursor.NextRow
    
    Loop
    
    pWorkspaceEdit.StopEditOperation
    pWorkspaceEdit.StopEditing True

End Sub

Users' Comments  
 

No comment posted

Add your comment

08, Sep. 2009
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for