I spent all day trying to make my program create a table dynamically and fix fields if they are not of a type I am expecting.

I couldn't find a case of someone already doing this, so this is an FYI post for any who are looking to do the same thing.

I like to keep my code as easy (for me) to use, so here is how you check a table:

VB Code:
  1. 'This code assumes a 'backup' folder exists under the sDataSet location
  2.         Dim sDataSet As String = Application.ExecutablePath
  3.         sDataSet = sDataSet.Remove(InStrRev(Application.ExecutablePath, "\"), Len(Application.ExecutablePath) - InStrRev(Application.ExecutablePath, "\")) 'defines the folder where the data is
  4.         Dim tdTable As New TableDescription()
  5.         With tdTable
  6.             .PriKey = "PriKey" 'Primary Key field. Can be left blank
  7.             .TableName = "Locations" 'Name of table to check
  8.             .Fields = MakeArray("Location_Description", "Location_Database") 'field names
  9.             .FieldTypes = MakeADOXArray(ADOX.DataTypeEnum.adVarWChar, ADOX.DataTypeEnum.adVarWChar) 'Field types of fields
  10.             .MissingIsFatal = MakeBooleanArray(False, False) 'whether to throw an error is a field doesn't match
  11.         End With
  12.  
  13.         VerifyDataBase("dataset.mdb", tdTable) 'Now verify the 'dataset.mdb' database

And the TableDescription Class...

VB Code:
  1. Public Class TableDescription
  2.     Private TName As String
  3.     Private aFields() As String
  4.     Private MFatal() As Boolean
  5.     Private FTypes() As ADOX.DataTypeEnum
  6.     Private PKey As String
  7.  
  8.     Property TableName() As String
  9.         Get
  10.             Return TName
  11.         End Get
  12.         Set(ByVal Value As String)
  13.             TName = Value
  14.         End Set
  15.     End Property
  16.  
  17.     Property Fields() As String()
  18.         Get
  19.             Return aFields
  20.         End Get
  21.         Set(ByVal Value() As String)
  22.             aFields = Value
  23.         End Set
  24.     End Property
  25.  
  26.     Property MissingIsFatal() As Boolean()
  27.         Get
  28.             Return MFatal
  29.         End Get
  30.         Set(ByVal Value() As Boolean)
  31.             MFatal = Value
  32.         End Set
  33.     End Property
  34.  
  35.     Property FieldTypes() As ADOX.DataTypeEnum()
  36.         Get
  37.             Return FTypes
  38.         End Get
  39.         Set(ByVal Value() As ADOX.DataTypeEnum)
  40.             FTypes = Value
  41.         End Set
  42.     End Property
  43.  
  44.     Property PriKey() As String
  45.         Get
  46.             Return PKey
  47.         End Get
  48.         Set(ByVal Value As String)
  49.             PKey = Value
  50.         End Set
  51.     End Property
  52. End Class