Optional pFields As IFields = ???
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
'
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
pWorkspaceFactory = New ShapefileWorkspaceFactory
fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(strFolder) Then
MsgBox("Folder does not exist: " & vbCr & strFolder)
Exit Function
End If
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
pFields = New Fields
pFieldsEdit = pFields
pFieldsEdit.FieldCount_2 = 1
'Create text Field
pField = New Field
pFieldEdit = pField
With pFieldEdit
.Length_2 = 30
.Name_2 = "TextField"
.Type_2 = esriFieldType.esriFieldTypeString
End With
pFieldsEdit.Field_2(0) = pField
End If
createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")
Exit Function
EH:
MsgBox(Err.Description, vbInformation, "createDBF")
End Function
pFieldsEdit.FieldCount_2 = 1
createDBF("Test", "c:\", Nothing) Public Function createDBF(ByVal strName As String, ByVal strFolder As String, ByVal 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
'
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
pWorkspaceFactory = New ShapefileWorkspaceFactory
fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(strFolder) Then
MsgBox("Folder does not exist: " & vbCr & strFolder)
Exit Function
End If
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
pFields = New Fields
pFieldsEdit = pFields
pFieldsEdit.FieldCount_2 = 2 'Add 2 fields
'Create First Field
Dim pField0 As IField
pField0 = New Field
pFieldEdit = pField0
With pFieldEdit
.Length_2 = 30
.Name_2 = "TextField0"
.Type_2 = esriFieldType.esriFieldTypeString
End With
pFieldsEdit.Field_2(0) = pField0
'Create Second Field
Dim pField1 As IField
pField1 = New Field
pFieldEdit = pField1
With pFieldEdit
.Length_2 = 30
.Name_2 = "TextField1"
.Type_2 = esriFieldType.esriFieldTypeString
End With
pFieldsEdit.Field_2(1) = pField1
End If
createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")
Exit Function
EH:
MsgBox(Err.Description, vbInformation, "createDBF")
End Function
Public Function createDBF(strName As String, strFolder As String, Optional pFields As IFields = Nothing) As ITable
Public Function createDBF(strName As String, strFolder As String, pFields As IFields) As ITable 'Do stuff End Function Public Function createDBF(strName As String, strFolder As String) As ITable Dim defaultFields as IFields = Nothing 'Can change this to a real set of fields if necessary return createDBF(strName, strFolder, defaultFields) 'Call the function with the default value End Function