You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

Creating a table in a geodatabase in ArcMap   Lauren Hall Jan 10, 2007
Re: Creating a table in a geodatabase in Ar...   Colin Zwicker Jan 10, 2007
Re: Creating a table in a geodatabase in Ar...   Lauren Hall Jan 11, 2007
Re: Creating a table in a geodatabase in Ar...   Colin Zwicker Jan 11, 2007
Re: Creating a table in a geodatabase in Ar...   Lauren Hall Jan 11, 2007
Re: Creating a table in a geodatabase in Ar...   Lauren Hall Jan 12, 2007
Re: Creating a table in a geodatabase in Ar...   Colin Zwicker Jan 12, 2007
Re: Creating a table in a geodatabase in Ar...   Lauren Hall Jan 16, 2007
Re: Creating a table in a geodatabase in Ar...   Colin Zwicker Jan 16, 2007
Re: Creating a table in a geodatabase in Ar...   Lauren Hall Jan 17, 2007
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 10, 2007 
Message Hello all,

I am someone new to arcObjects and I am trying to create a table in a geodatabase in ArcMap, then populate that table with values taken from a text file.

I was unable to figure out how to create the table using VBA, but had created a test table in the hopes I would be able to work out more of the code. I was able to read the values out of the text file, but was UNable to add those values to the test table.

So, I guess my question is 2 fold:
1. How would I create a geodatabase table in arcMap?
2. How can I add values to the cells in the table?

Thanks for any input,
Lauren 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Colin Zwicker 
Date Jan 10, 2007 
Message Lauren,

The 9.2 SDK documentation contains a series of HowTo articles you might find useful. For instance the following link walks you through the logic for creating an objectclass (basically a table that is registered with the geodatabase).

http://edndoc.esri.com/arcobjects/9.2/NET/E1E93AF5-D224-491A-BDEA-C0EEF2251A7A.htm
(if you want to learn more about accessing your workspace check out "How to connect to a workspace" @ http://edndoc.esri.com/arcobjects/9.2/NET/c778d2bb-eb36-4793-9c89-20795811c5eb.htm )

At the bottom of many of the HowTos are links to related documents. In this case you might be interested in how to add additional fields to your table (How to create new fields).

You can then follow the link at the bottom of this page to "How to work with fields". This will walk you through the processes of accessing existing fields in a table (which you will need for updating the values with your text file information.)

Lastly the "How to create features within the geodatabase" (see the contents drop down on the right when viewing any of the above howtos, under geodatabase management \ creating and converting data). This howto will walk you through the two ways to create features. Features are really just Rows with a shape attribute. In almost every case you can replace IFeature with IRow or IFeatureClass with ITable and ignore any geometry references if you just want to work with a non spatial table.

Through out the documents are hyperlinks to individual API calls which might be useful. (check out the ITable hyperlink for your case)

I hope that these documents will allow you to reach your goal.
Note: The code is written in vb.net or C# however the concepts and interface names are virtually the same for vb6\vba.

- Colin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 11, 2007 
Message Colin,

Thanks for the links, I tried to change the code into VBA, but, as I said before, I'm relatively new to the language and ran into some difficulties. I've attatched my code, hopefully you could tell me where I went wrong?

Also, I want to call this from a button click event... I understand i just simply say:

Call CreateObjectClass_Example(workspace, nameOfObjectClass, fields, configKeyword)

But I'm confused exactly how to declare those variables.

Thanks
-- Lauren 
 
Public Function CreateObjectClass_Example(ByVal workspace As IWorkspace2, ByVal nameOfObjectClass As String, ByVal fields As IFields, ByVal configKeyword As String) As IObjectClass

    Dim featureWorkspace As IFeatureWorkspace
    Set featureWorkspace = CType(workspace, IFeatureWorkspace)
    
    If workspace.NameExists(esriDatasetType.esriDTTable, nameOfObjectClass) Then
       ' If table with that name already exists, return that table.
        featureWorkspace.OpenTable (nameOfObjectClass)
    End If
    
    ' Create an object class description to use if needed for fields collection and for the CLSID parameter.
    Dim ocDesc As IObjectClassDescription
    Set ocDesc = New ObjectClassDescription
    
    ' Get the set of required fields collection.
    If fields Is Nothing Then
      fields = ocDesc.RequiredFields
    End If
    
   featureWorkspace.CreateTable(nameOfObjectClass, fields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, configKeyword)

End Function
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Colin Zwicker 
Date Jan 11, 2007 
Message Lauren,

I converted the first two functions, this will open a Personal Geodatabase workspace and create the most basic table (ie just an OID column). I have included the C# or VB.Net code which was converted as an example.

Note that one of the changes I made was the naming of variable (inserted a p in front, this is not required) while another change that is required was how new objects are created. In vb6 this must be done on two lines (see IObjectClassDescription).

Hope this gives you a jump start!

- Colin 
 
Option Explicit

Public Function Lauren_Hall()

Dim pWorkspace As IWorkspace
Set pWorkspace = open_pGDB_Workspace("C:\temp\mypGDB.mdb")

Dim pTable As ITable
Set pTable = CreateObjectClass(pWorkspace, "LaurenTable", Nothing, "")

End Function

'C#
'//For example, connectionString = "C:\\myData\\mypGDB.mdb".
'public IWorkspace openFromFile_pGDB_Workspace(string connectionString)
'{
'IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
'return workspaceFactory.OpenFromFile(connectionString, 0);
'}
Public Function open_pGDB_Workspace(sconnectionString As String) As IWorkspace
  Dim pWorkspaceFactory As IWorkspaceFactory
  Set pWorkspaceFactory = New AccessWorkspaceFactory
  Set open_pGDB_Workspace = pWorkspaceFactory.OpenFromFile(sconnectionString, 0)
End Function

'VB.Net
'Public Function CreateObjectClass_Example(ByVal workspace As IWorkspace2, ByVal nameOfObjectClass As String, ByVal fields As IFields, ByVal configKeyword As String) As IObjectClass
'Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace)
'If workspace.NameExists(esriDatasetType.esriDTTable, nameOfObjectClass) Then
'  ' If table with that name already exists, return that table.
'  Return CType(featureWorkspace.OpenTable(nameOfObjectClass), IObjectClass)
'End If
'' Create an object class description to use if needed for fields collection and for the CLSID parameter.
'Dim ocDesc As IObjectClassDescription = New ObjectClassDescription
'' Get the set of required fields collection.
'If fields Is Nothing Then
'  fields = ocDesc.RequiredFields
' End If
' Return CType(featureWorkspace.CreateTable(nameOfObjectClass, fields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, configKeyword), IObjectClass)
'End Function

Public Function CreateObjectClass(pWorkspace As IWorkspace2, sNameOfObjectClass As String, pFields As IFields, sConfigKeyword As String) As IObjectClass
  Dim pFeatureWorkspace As IFeatureWorkspace
  Set pFeatureWorkspace = pWorkspace
  If pWorkspace.NameExists(esriDTTable, sNameOfObjectClass) Then
    Set CreateObjectClass = pFeatureWorkspace.OpenTable(sNameOfObjectClass)
  End If
  Dim pOCDesc As IObjectClassDescription
  Set pOCDesc = New ObjectClassDescription
  If pFields Is Nothing Then
   Set pFields = pOCDesc.RequiredFields
  End If
  
  Set CreateObjectClass = pFeatureWorkspace.CreateTable(sNameOfObjectClass, pFields, pOCDesc.InstanceCLSID, pOCDesc.ClassExtensionCLSID, sConfigKeyword)
  
End Function

 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 11, 2007 
Message Ohhhh.... I get it, I think I just needed to see it. This was definitely a great jump start, hopefully I'll be able to figure the other stuff out now, but I may be back!

Thanks for all your help,
Lauren 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 12, 2007 
Message Well... The table is created in the geodatabase, the text file is read and ready to go into the table... but I can't find any clear explaination as to how to put the data in the empty table.

Any helpfull links or insights?

-- Lauren 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Colin Zwicker 
Date Jan 12, 2007 
Message Lauren,

I think the example from IRow would help.
http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/IRow.htm

The c# example is actually more correct because it initalizes the default values of the row. I assume that your table has one field for each of the values that you want to store per row from your text file.

If you were going to create many new rows in your table you might consider using an ICursor \CreateRowBuffer approach for the performance benifits.

In the example the first variable from the text file is a string (so the field type must also be string), the second variable is an integer.

I would also suggest reading the "How to create features within a geodatabase" @ http://edndoc.esri.com/arcobjects/9.2/NET/e7b33417-0373-4f87-93db-074910907898.htm

Although it is written for features, the general workflow is virtually the same (again ignore all geometry\shape references) that of creating a new feature.

Good luck!
- Colin 
 
Public Sub CreateNewRow(pTable As ITable, sNameOfField1 As String, sNameOfField2 As String)
   
  Dim pRow As IRow
  Set pRow = pTable.CreateRow
  
  'This will populate your row with any default values\subtypes that you have
  'defined in your geodatabase table schema
  Dim pRowSubtypes As IRowSubtypes
  Set pRowSubtypes = pRow
  pRowSubtypes.InitDefaultValues
  
  Dim iFieldIndex As Integer
  iFieldIndex = pRow.fields.FindField(sNameOfField1)
  pRow.Value(iFieldIndex) = "Hi" 'variableHoldingFirstValue
  
  iFieldIndex = pRow.fields.FindField(sNameOfField2)
  pRow.Value(iFieldIndex) = 999 'variableHoldingSecondValue
  pRow.Store

End Sub
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 16, 2007 
Message Oh, I think I know why it wasn't working. When the table is created in the geodatabase, there is no 'refresh' command. So, when the table is called from the other functions I get an error basically saying that it doesn't exist ("item not found in this collection"). That's because the geodatabase isn't refreshed, correct?

If so, how would I go about doing that? 
 
Private Sub Create_Table_Click()

    'create the table in the geodatabase
    Dim pWorkspace As IWorkspace
    Set pWorkspace = open_pGDB_Workspace("D:\users\lhall\Research\test.mdb")
    
    'Declare Fields
    Dim pField1 As IFieldEdit
    Set pField1 = New Field
    pField1.Type = esriFieldTypeString
    pField1.Name = "Observation"
    
    Dim pField2 As IFieldEdit
    Set pField2 = New Field
    pField2.Type = esriFieldTypeDouble
    pField2.Name = "Measured"
    
    Dim pField3 As IFieldEdit
    Set pField3 = New Field
    pField3.Type = esriFieldTypeDouble
    pField3.Name = "Simulated"
    
    Dim pField4 As IFieldEdit
    Set pField4 = New Field
    pField4.Type = esriFieldTypeDouble
    pField4.Name = "WeightedResidual"
    
    'Collect the fields into a fields collection
    Dim pFieldsEdit As IFieldsEdit
    Set pFieldsEdit = New fields
    pFieldsEdit.AddField pField1
    pFieldsEdit.AddField pField2
    pFieldsEdit.AddField pField3
    pFieldsEdit.AddField pField4
        
    'Prepare the required arguments for CreateTable
    Dim pFields As IFields
    Set pFields = pFieldsEdit
    
    Dim pTable As ITable
    Set pTable = CreateObjectClass(pWorkspace, "ObservationResiduals", pFields, "")
    
    'refresh geodatabase
    
    'add text file to table
    Call AppendTextToTable("D:\users\lhall\Research\ObservationResiduals.txt", pTable)


End Sub
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Colin Zwicker 
Date Jan 16, 2007 
Message Lauren,

The geodatabase does not need refreshing (if you were in actcatalog the UI display might need a refresh to see the newly added table).

Without seeing the line the error is being raised on I believe that you are actually missing an error (and the table is not being created) or it is being created, but you have left out the ObjectID field.

Lets change your workflow slightly to allow ArcGIS to determine which fields are required, and then you can add your desired fields after the table has been created.

Please see below.

- Colin

ps. The way that you were creating the fields collection is not exactly the pattern that should be used. I know that the IFieldsEdit and IFieldEdit can be a little\very confusing.

The basic pattern is that you create the object (Dim pFields as IFields, set pFields = new fields) then you set the editing object equal to the new object, (Dim pFieldsEdit as IFieldsEdit, Set pFieldsEdit = pFields), then you could add the fields like you did after creating them in the pattern shown below. 
 
Option Explicit

Public Function Create_Table_Click()

'Open the workspace
Dim pWorkspace As IWorkspace
Set pWorkspace = open_pGDB_Workspace("C:\temp\mypGDB.mdb")

'Create the table
Dim pTable As ITable
Set pTable = CreateObjectClass(pWorkspace, "LaurenTable2", Nothing, "")
' Table has now been created in GDB with the required fields (ObjectID)

'Add my fields
AddDesiredFieldsToTable pTable

'AppendTextToTable("D:\users\lhall\Research\ObservationResiduals.txt", pTable)

End Function

Public Function AddDesiredFieldsToTable(pTable As ITable)

Dim pFieldEdit As IFieldEdit
'Create the fields
Dim pField1 As IField
' create field 1
Set pField1 = New field
' use the edit interface to change the properties of your new field
Set pFieldEdit = pField1
pFieldEdit.Type = esriFieldTypeString
pFieldEdit.Name = "Observation"
' Length is required for String fields
pFieldEdit.Length = "50" ' this is a default, you should change as needed
'Add the field to the table
pTable.AddField pField1

Dim pField2 As IField
Set pField2 = New field        ' Create new object
Set pFieldEdit = pField2       ' set editing object
pFieldEdit.Type = esriFieldTypeDouble  ' work with editing object
pFieldEdit.Name = "Measured"
pTable.AddField pField2        ' add field to table

Dim pField3 As IField
Set pField3 = New field
Set pFieldEdit = pField3
pFieldEdit.Type = esriFieldTypeDouble
pFieldEdit.Name = "Simulated"
pTable.AddField pField3

Dim pField4 As IField
Set pField4 = New field
Set pFieldEdit = pField4
pFieldEdit.Type = esriFieldTypeDouble
pFieldEdit.Name = "WeightedResidual"
pTable.AddField pField4

End Function
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Creating a table in a geodatabase in ArcMap 
Author Lauren Hall 
Date Jan 17, 2007 
Message I've got it!!! It's working perfectly now!

Thanks for all your patience.

-- Lauren