Solved

No fields defined?

Posted on 1998-05-27
7
359 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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