opening and saving a dbf file using a macro (Vba)

1772
2
05-14-2011 12:43 PM
Florianflo
New Contributor
hi

i created a VBA Macro in with i calculate a dbf table.
now i want by clicking a button that the table open
i found this code but it doesn´t work.

Private Sub CommandButton3_Click()
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap
Dim sDir As String, strTableName As String
sDir = "D:\file\output\NEWTABLE"
strTableName = "T_Test.DBF"
Dim pFact As IWorkspaceFactory
Set pFact = New ShapefileWorkspaceFactory
Dim pWorkspace As IWorkspace
Set pWorkspace = pFact.OpenFromFile(sDir, 0)
Dim pFeatWs As IFeatureWorkspace
Set pFeatWs = pWorkspace
Dim pTable As ITable
Set pTable = pFeatWs.OpenTable(strTableName)
' Create a new standalone table and add it
' to the collection of the focus map
Dim pStTab As IStandaloneTable
Set pStTab = New StandaloneTable
Set pStTab.Table = pTable
Dim pStTabColl As IStandaloneTableCollection
Set pStTabColl = pMap
pStTabColl.AddStandaloneTable pStTab
' Refresh the TOC
pDoc.UpdateContents
End Sub

before i create the dbf file i used a shape file with the next code i opened the shape file
on the same way it should work with the dbf files.



Dim pMXDocument As IMxDocument
Set pMXDocument = ThisDocument
Dim pPfad As String
pPfad = "D:\File\output\NEWTABLE"
Dim pShapefile As String
DBFile = "T_TesT_5"
Dim pwFactory As IWorkspaceFactory
Set pwFactory = New ShapefileWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pwFactory.OpenFromFile(pPfad, 0)
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pShapefile)
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = New FeatureLayer
Set pFeatureLayer.FeatureClass = pFeatureClass
pFeatureLayer.Name = "o_length" ' Hier wurde ner neue layer bennant
pMXDocument.AddLayer pFeatureLayer

pMXDocument.ActiveView.Refresh
pMXDocument.UpdateContents
'Dim pMXDocument As IMxDocument
'Set pMXDocument = ThisDocument
Dim pfLayer As IFeatureLayer
Dim pMap As IMap
Set pMap = pMXDocument.FocusMap
Set pfLayer = pMap.Layer(1) 'pMXDocument.SelectedLayer '###############

Dim pFClass As IFeatureClass
Set pFClass = pfLayer.FeatureClass
Dim pFCursor As IFeatureCursor
Set pFCursor = pFClass.Update(Nothing, False)
Dim pFeature As IFeature
Dim aoiApp As Application
Dim aoiDoc As IMxDocument
Dim aoiStandAloneTable As IStandaloneTable
Dim aoiTwin As ITableWindow
Dim aoiTab As IClass
Dim aoiUnknown As IUnknown
Dim aoiFlayer As IFeatureLayer2
Set aoiApp = Application
Set aoiDoc = ThisDocument
Set aoiTwin = New TableWindow
Set aoiFlayer = pMap.Layer(0) ' aoiDoc.SelectedLayer
If aoiFlayer Is Nothing Then
MsgBox " Es ist kein Layer ausgewählt"
Exit Sub
End If

Set aoiTab = aoiFlayer
If aoiTab Is Nothing Then
MsgBox "Auf die Tabelle kann nicht zu gegriffen werden."
Exit Sub
End If

Set aoiTwin.Table = aoiTab
Set aoiTwin.Application = aoiApp
aoiTwin.Show (True)

at the end i want save the file by clicking another button.
could somebody help me

sorry for my bad english

Flo
0 Kudos
2 Replies
Florianflo
New Contributor
i solved it:

  Dim filePath As String
  filePath = "D:\xxxxx\output\NEWTABLE"
  Dim tableName As String
  '#########################
  'Dateiname
  '#########################
 
  tableName = "TSX_Capacity1.dbf"
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
  Dim pWorkspace As IWorkspace
  Dim pFact As IWorkspaceFactory
  Set pFact = New ShapefileWorkspaceFactory
  Set pWorkspace = pFact.OpenFromFile(filePath, 0)
  Dim pFWorkspace As IFeatureWorkspace
  Set pFWorkspace = pWorkspace
  Dim pTable As ITable
  Set pTable = pFWorkspace.OpenTable(tableName)
  'use the table accordingly
  'MsgBox "Total rows of the table: " & pTable.RowCount(Nothing)
  Dim aoiStandAloneTable As IStandaloneTable
  Dim aoiTwin As ITableWindow
  Dim aoiApp As Application
  Set aoiApp = Application
  Set aoiDoc = ThisDocument
  Set aoiTwin = New TableWindow
  Set aoiTab = pTable
If aoiTab Is Nothing Then
MsgBox "Auf die Tabelle kann nicht zu gegriffen werden."
Exit Sub
End If
Set aoiTwin.Table = aoiTab
Set aoiTwin.Application = aoiApp
aoiTwin.Show (True)

and for saving i used the model builder 🙂
0 Kudos
BELAYACHISara
New Contributor
Hi,
please i really need help, that's my topic : http://forums.arcgis.com/threads/30770-How-to-Open-a-Table-Convert-it-to-ShapeFile-amp-projecting-th...

i'm new, so i don't know how to get data from a table "*.dbf"  i'm sticked plz help me if u could
0 Kudos