
Problem Creating Access DB Tables
My suggestion is to change the order of your process -- create the
database,
create and append the tabledef(s), and _then_ create and append the fields
to
the tabledef(s). Below is the subroutine I have used successfully for
creating
a database with indexed fields.
Good luck,
Randy Gritton
Public Sub CreateTables()
Dim MyDb As Database
Dim MyTd As TableDef
Dim MyFld As Field
Dim MyIdx As Index
Dim MyIdxFlds As Field
Set MyDb = wsPWReps.CreateDatabase("MyDatabase.mdb", dbLangGeneral,
dbVersion30)
Set MyTd = MyDb.CreateTableDef("tblMyTable")
Set MyFld = MyTd.CreateField("MyField", dbText, 8)
MyFld.AllowZeroLength = False
MyFld.Required = True
MyTd.Fields.Append MyFld
Set MyIdx = MyTd.CreateIndex()
MyIdx.Name = "MyField"
MyIdx.Primary = False
MyIdx.Unique = False
Set MyIdxFlds = MyIdx.CreateField("MyField")
MyIdx.Fields.Append MyIdxFlds
MyTd.Indexes.Append MyIdx
MyDb.TableDefs.Append MyTd
MyDb.Close
Set MyFld = Nothing
Set MyIdx = Nothing
Set MyIdxFlds = Nothing
Set MyTd = Nothing
Set MyDb = Nothing
End Sub
Quote:
> In the code segment that follows, I'm trying to create a table in a
> database but on the first .Append instruction, VB returns "Can't perform
> operation; it is illegal". This is VB3 under Win95. Here's the code
(the
> all caps constants have been defined previously), anyone have any ideas?
> TIA
> Sub MakeNewDatabase (ByVal DatabaseName As String)
> Dim mytable As New TableDef
> Dim fld1 As New Field
> Dim fld2 As New Field
> mytable.Name = TABLE1_NAME
> 'tooltable.Attributes = DB_ATTACHTABLE
> fld1.Name = TABLE1_FIELD1_NAME
> fld1.Type = DB_TEXT
> fld1.Attributes = DB_UPDATABLEFIELD
> fld2.Name = TABLE1_FIELD2_NAME
> fld2.Type = DB_TEXT
> fld2.Attributes = DB_UPDATABLEFIELD
> mytable.Fields.Append fld1 <----- This is where the error occurs
> mytable.Fields.Append fld2
> Set MyDB = CreateDatabase(DatabaseName, DB_LANG_GENERAL)
> 'add the tool table to the new database
> MyDB.TableDefs.Append tooltable
> MyDB.TableDefs(TABLE1_NAME).Fields.Append fld1
> MyDB.TableDefs(TABLE1_NAME).Fields.Append fld2
> End Sub