Problem Creating Access DB Tables 
Author Message
 Problem Creating Access DB Tables

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



Sun, 16 Apr 2000 03:00:00 GMT  
 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



Sun, 16 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with creating a table in Access from existing table

2. Create table at Access DB via vbscript

3. Newbie: problems using a multi-table Access DB and Recordsets

4. Copy Access/Oracle table from one DB to another DB.

5. Create a db access db with password

6. Create a db access db with password

7. MS Access DB - Copy Table A from Database A to Table A Database B

8. How to copying from one DB table to another DB table

9. Problem using DataGrid after Creating Table in Access

10. Problems creating a table based on a stored Query in an Access-MDB

11. How to create a form in VB (with access db) and create an ActiveX Object

12. HELP: Need help in creating a table from a table within Access

 

 
Powered by phpBB® Forum Software