Load Layer from File Geodatabase (.gdb)

Discussion created by bdeverot on Aug 23, 2010
Latest reply on Apr 3, 2014 by sweety88
Below is a post I found on the old ESRI forum.  When I add a raster from my file geodatabase, I want it to programatically load the layer file as well.  Where do I insert the code below to do that?

I use a File Geodatabase with the name:
I have some layers in this gdb, which I want to load to ArcMap programmatically.(the database.gdb is a folder with files inside with extensions .freelist, .gdbindexes, .gdbtable, .gdbtablx and .spx)
Wherever I look in the forum I get code for an mdb Geodatabase.
How can I load a layer from a File Geodatabase?

The following code didn't work. I get an automation error at the line pWorkspaceFactory.OpenFromFile("C:\ZZZ\database.gdb", 0)
It only works (I think) with mdb files

Thank you 
  Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New FileGDBWorkspaceFactory

'Open the Access Workspace
Dim pWorkspace As IFeatureWorkspace
Set pWorkspace = pWorkspaceFactory.OpenFromFile("C:\ZZZ\database.gdb", 0)

'Open the featureclass
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pWorkspace.OpenFeatureClass("LayerName")

'Create a new Feature Layer
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = New FeatureLayer

'Set the feature class to the geodatabase featureclass
Set pFeatureLayer.FeatureClass = pFeatureClass
pFeatureLayer.Name = pFeatureClass.AliasName

'Add the new layer to ArcMap
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
pMap.AddLayer pFeatureLayer

'Refresh the view and table of contents