HOW TO
Note: This article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
This sample code shows how to save a .lyr file as a blob in a database. Any object that supports persistence can be saved in this manner.
Note: Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward
Note: For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
' Get the IPersistStream for the 1st Layer from the map
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pPersist As IPersistStream
Set pPersist = pMxDoc.FocusMap.Layer(0)
' Now persist the layer to the memory blob stream
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = New MemoryBlobStream
pPersist.Save pMemoryStream, False
' Finally save the blob into the database
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:\Source\BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("Layers")
Dim pRow As IRow
Set pRow = pTable.CreateRow
pRow.Value(pRow.Fields.FindField("Layers")) = pMemoryStream
pRow.Store
' Finally save the blob into the database
Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New AccessWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace =
pWorkspaceFactory.OpenFromFile("C:\Source\BLOB.mdb", 0)
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("Layers")
Dim pCursor As ICursor
Set pCursor = pTable.Search(Nothing, False)
Dim pRow As IRow
Set pRow = pCursor.NextRow
If (pRow Is Nothing) Then Exit Sub
Dim pMemoryStream As IMemoryBlobStream
Set pMemoryStream = pRow.Value(pRow.Fields.FindField("Layers"))
Dim pLayer As ILayer
Set pLayer = New FeatureLayer
Dim pPersist As IPersistStream
Set pPersist = pLayer
pPersist.Load pMemoryStream
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.FocusMap.AddLayer pLayer
Article ID: 000005714
Get help from ArcGIS experts
Start chatting now