Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

No fields defined?

Posted on 1998-05-27
7
362 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:victorlong
  • 4
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
Dalin earned 50 total points
ID: 1461810
victorlong,
Your Set F1 = Ind1.CreateField("field1")
should be
Set F1 = NewTb1.CreateField("field1")
NewTb1.Fields.Append F1  

Regards
Dalin
0
 

Author Comment

by:victorlong
ID: 1461811
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"?  
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1461812
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

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:victorlong
ID: 1461813
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
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1461814
victorlong,
Sorry that I was not able to get back to you yesterday.
Which version of VB are you working with?
0
 

Author Comment

by:victorlong
ID: 1461815
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 :-)
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1461816
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question