The code below is meant to build an array with certain properties neccesairy to create column for a table in a database.
Would there be any more effcient way of doing this?
Public fieldName As String
Public fieldType As ADOX.DataTypeEnum
Public fieldProperties As String
Public fieldPropertiesValue
Public fieldIndex As Boolean
Public fieldUnique As Boolean
Public fieldKey As Boolean
Public Sub New(ByVal fieldName As String, ByVal fieldType As ADOX.DataTypeEnum, Optional ByVal fieldProperties As String = Nothing,
Optional ByVal fieldPropertiesValue As Object = Nothing, Optional ByVal fieldIndex As Boolean = False,
Optional ByVal fieldUnique As Boolean = False, Optional ByVal fieldKey As Boolean = False)
Me.fieldName = fieldName
Me.fieldType = fieldType
Me.fieldProperties = fieldProperties
Me.fieldPropertiesValue = fieldPropertiesValue
Me.fieldIndex = fieldIndex
Me.fieldUnique = fieldUnique
Me.fieldKey = fieldKey
End Sub
Public Shared IDOwner As classObjectField = New classObjectField("IDOwner", ADOX.DataTypeEnum.adInteger, "AutoIncrement", True, True, True, True)
Public Shared OwnerAccount As classObjectField = New classObjectField("OwnerAccount", ADOX.DataTypeEnum.adVarWChar, fieldIndex:=True)
Public Shared OwnerName As classObjectField = New classObjectField("OwnerName", ADOX.DataTypeEnum.adVarWChar, fieldIndex:=True)
Public Shared OwnerMail As classObjectField = New classObjectField("OwnerMail", ADOX.DataTypeEnum.adVarWChar, fieldIndex:=True, fieldUnique:=True)
Public Shared ArrayTableOwners = New Object(3) {
IDOwner, OwnerAccount, OwnerName, OwnerMail
}
Public Shared IDUser As classObjectField = New classObjectField("IDUser", ADOX.DataTypeEnum.adInteger, "AutoIncrement", True, True, True, True)
Public Shared UserName As classObjectField = New classObjectField("OwnerAccount", ADOX.DataTypeEnum.adVarWChar, fieldIndex:=True)
Public Shared UserFunctionCode As classObjectField = New classObjectField("OwnerName", ADOX.DataTypeEnum.adVarWChar, fieldIndex:=True)
Public Shared ArrayTableUser = New Object(2) {
IDUser, UserName, UserFunctionCode
}
This function below creates the table of an existing Access DB
Public Shared Function createDBTable(Cat As ADOX.Catalog, tableName As String, objField As Object) As Boolean
Dim confirm As Boolean = True
Try
Dim objTable = New ADOX.Table
Dim objIndex = New ADOX.Index
objTable.Name = tableName
For Each valueField In objField
objTable.Columns.Append(valueField.fieldName, valueField.fieldType)
If Not valueField.FieldProperties = Nothing Then
objTable.Columns.Item(valueField.fieldName).ParentCatalog = Cat
objTable.Columns.Item(valueField.fieldName).Properties(valueField.fieldProperties).Value = valueField.FieldPropertiesValue
End If
If valueField.fieldIndex = True Then
objIndex = New ADOX.Index
objIndex.Unique = valueField.fieldUnique
objIndex.Name = valueField.fieldName
objIndex.PrimaryKey = valueField.fieldKey
objIndex.Columns.Append(valueField.fieldName)
objTable.Indexes.Append(objIndex)
End If
Next
Cat.Tables.Append(objTable)
objIndex = Nothing
objTable = Nothing
Catch ex As Exception
MsgBox(ex.Message)
confirm = False
End Try
Return confirm
End Function
Usage:
Dim objCn = New ADODB.Connection Dim objCat = New ADOX.Catalog objCn.Open(DBSourceLocation) objCat.ActiveConnection = objCn createDBTable(objCat, "OWNERLIST", ArrayTableOwners) createDBTable(objCat, "USERLIST", ArrayTableUser)