Daniel
There is a lot of code involved with the VB6 solution.
I have, however tried to create some code in Access to:
a) create a table in an existing database
b) Add a Key field
c) add a text field
d) add the table.
This is the code:
Public Function CreateTable(TableName As String) As Boolean
Dim DB As Database
Dim IDX As Index
Dim TBL As TableDef
Dim FLD As field
Set DB = DBEngine.Workspaces(0).Databases(0) 'Get correct database
Set TBL = DB.CreateTableDef(TableName)
With TBL
'Create the index
Set IDX = .CreateIndex("Index")
With IDX
.CreateField ("Key")
.Primary = True
.Unique = True
End With
.Indexes.Append IDX 'Add the index
.Indexes.Refresh
'Create the key field
Set FLD = .CreateField("Key", vbLong)
'Set the values
With FLD
.Required = True
.ValidationRule = ""
.ValidationText = ""
.Attributes = dbAutoIncrField
End With
'Add the field
.Fields.Append FLD
'Create another field
Set FLD = .CreateField("Data", vbString, 25)
'Set the values
With FLD
.Required = True
'.AllowZeroLength = True 'Why doesn't this work (Invalid operation)?
.ValidationRule = ""
.ValidationText = ""
End With
'Add the field
.Fields.Append FLD
End With
'Now add the table
DB.TableDefs.Append TBL 'Fails (Invalid argument)
DB.Close
End Function
The TableName passed is "Dummy"
I had two problems with this code.
- Trying to set "AllowZeroLength" caused an error. I do not think that this should fail since it is a string
- Trying to add the table definition caused an error.
This code should be enough to show what I am trying to accomplish. It is very simple, just 2 fields. But if it works for 2 it will work for as many as needed.
Thanks for looking into this
John