Link to home
Start Free TrialLog in
Avatar of victorlong
victorlong

asked on

No fields defined?

Why the last line in the following code causes error:
No fields defined - cannot append Tabledef of Index.

    Set NewDB = CreateDatabase("c:\tem\try.mdb", dbLangGeneral)
    Set NewTbl = NewDB.CreateTableDef("table1")
    Set Ind1 = NewTbl.CreateIndex("index1")
    Ind1.Primary = True
    Set F1 = Ind1.CreateField("field1")
    Ind1.Fields.Append F1
    NewTbl.Indexes.Append Ind1
    NewDB.TableDefs.Append NewTbl
ASKER CERTIFIED SOLUTION
Avatar of Dalin
Dalin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of victorlong
victorlong

ASKER

Hi Dalin.

>   Your Set F1 = Ind1.CreateField("field1")
>   should be
>   Set F1 = NewTb1.CreateField("field1")
>   NewTb1.Fields.Append F1

That is came from a book! I will check again though.
But, if we change the line, how can VB know "field1" has "index1"?  
victorlong,
Set F1 = Ind1.CreateField("field1") only create a field to the index object, and it does not create it to your table, that's why you have the error.
To create Index, I would use
Set Ind1 = NewTbl.CreateIndex("Field1")
Ind1.Primary = True
Ind1.Unique = True ' if You want to

regards
Dalin

Hi Dalin.

Getting confused....I think an example may be more helpful. Can you add something or change something in the following code so that the database NewDB can be used? Many thanks.

 Set NewDB = CreateDatabase("c:\tem\try.mdb", dbLangGeneral)
 Set NewTbl = NewDB.CreateTableDef("table1")
 Set Ind1 = NewTbl.CreateIndex("index1")
 Ind1.Primary = True
 Set F1 = Ind1.CreateField("field1")
 Ind1.Fields.Append F1
 NewTbl.Indexes.Append Ind1
victorlong,
Sorry that I was not able to get back to you yesterday.
Which version of VB are you working with?
Hi Dalin.

I think I understan now....that is not my fault :-)
I always thought
Set F1 = Ind1.CreateField("field1")
is "create" a field. In fact it just tell VB that a field has an index. The field may existed before above code!
In English, "create" means something is bringed to the world but before "create" it not existed.....

This time I may confuse you :-)
victorlog,
You need to "create" the field for both TableDef and Index..
I am sorry that I was not more clear about it. Glad you get it.
If you have additional questions, let me know. I will try my best to be more prompt (I was mostly out of my office yesterday).
Good luck in your project.
Dalin