45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:AO入门之创建一个dbf文件 的方法

AO入门之创建一个dbf文件 的方法

2016-08-30 13:16:34 来源:www.45fan.com 【

AO入门之创建一个dbf文件 的方法

Public Function createDBF(strName As String, _
             strFolder As String, _
         Optional pFields As IFields) As ITable
' createDBF: simple function to create a DBASE file.
' note: the name of the DBASE file should not contain the .dbf extension
' Email to me :cnwangyx@163.com
 On Error GoTo EH
 
 ' Open the Workspace
 Dim pFWS As IFeatureWorkspace
 Dim pWorkspaceFactory As IWorkspaceFactory
 Dim fs as object
 Dim pFieldsEdit As IFieldsEdit
 Dim pFieldEdit As IFieldEdit
 Dim pField As IField

 Set pWorkspaceFactory = New ShapefileWorkspaceFactory
 Set fs = CreateObject("Scripting.FileSystemObject")
 If Not fs.FolderExists(strFolder) Then
  MsgBox "Folder does not exist: " & vbCr & strFolder
  Exit Function
 End If

 Set pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)
 
 ' if a fields collection is not passed in then create one
 If pFields Is Nothing Then
  ' create the fields used by our object
  Set pFields = New Fields
  Set pFieldsEdit = pFields
  pFieldsEdit.FieldCount = 1
  
  'Create text Field
  Set pField = New Field
  Set pFieldEdit = pField
  With pFieldEdit
    .Length = 30
    .Name = "TextField"
    .Type = esriFieldTypeString
  End With
  Set pFieldsEdit.Field(0) = pField
 End If
 
 Set createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")
 
 Exit Function
EH:
  MsgBox Err.Description, vbInformation, "createDBF"

End Function
使用注意:
'参数strName和strFolder是必需的。
'要注意在这个方法中,pFieldsEdit.FieldCount必须和后面添加的field数目相同,否则会出现错误。
'在一些项目中,会要求对已有的属性表进行统计分析,生成统计结果,如果要求是dbf格式,
'大家可以用这个方法生成统计结果表。
'第一、第二个参数是必须的。同样,大家可以根据需要,改写此方法,参数中可以传入如FieldCount
'等在调用此方法前,已经经过计算得到的一些值。
 

本文地址:http://www.45fan.com/a/question/69803.html
Tags: 入门 Create 创建一个dbf文件
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部