Solved

How to use dDase NDX index files?

Posted on 1998-07-09
13
921 Views
Last Modified: 2008-02-01
I have been working with Delphi for the past 2 years, however, up until a few weeks ago I have avoided using any of Delphi's database components.  I simply never had any need for a database.

Now that I have a need to create a database, I have chosen Delphi,...of course.  However, I am puzzled with how indexes work with a DBF table created with Database Desktop 7, and used under Delphi 3.

I have a TDBGrid, TTable and TDataSource component on my main form.  Data is entered into the grid/table programmatically, rather than manually typing data into the grid.  E.g.,

   Table1.Edit;
   Table1.FieldByName('CONFIRMED').AsBoolean := True;
   Table1.Post;

I created two indexes (LAST.NDX and EMAIL.NDX) in Database Desktop 7.  After the indexes are created in Database Desktop, this is where I get confused. I understand what an index is...this is not the problem.  The problem is
regarding the TTable's IndexFiles, IndexFieldNames, and IndexName properties.  I have a problem understanding the difference between these properties.  When are they used?  What do they do?

I have been assigning the IndexFiles property to "LAST.NDX" and "EMAIL.NDX" at design time using the Object Inspector.  However, as I run my application, and records are added to the database, I start to notice something very odd.  Records that should be at the bottom of the DBGrid are strangely not visible.  I can use the vertical scroll/elevator bar and/or
TNavigator "End" button, and bring the table pointer to the very bottom of the grid, however, the last record shown in the grid is NOT the last record in the table IF the index is working properly.  The more records I add, the more records disappear from the bottom of the grid.   If I remove the .NDX files, then all of the records appear in the grid!

E.g, if I have 4 records, and the "last" fields are displayed in the grid "B", "C", "D", "E" at the outset, then as I add "F" to a new record, post it, then add "G" to a new record, then post it, "F" and "G" are missing from the grid!  If I add a new record "A", then "E" disappears from the grid. It's as if the index thinks that there are still only 4 records to keep sorted, rather than reindexing itself based upon the new "F", "G" and "A" entries.  It's as if the TDBGrid refuses to add more rows!

I therefore suspect that the index file(s) are not getting updated as I add records to the table.

If I delete the .NDX files, and then recreate them, and reattach them, then the grid displays all of the records just fine....until I start adding more records.

What might I be doing wrong?

0
Comment
Question by:Monroe406
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 5

Expert Comment

by:mayhew
ID: 1357493
What you might consider doing is using an .mdx instead of an .ndx.

I had trouble recreating your problem.  The closest I was able to come was when I created a non maintained index (.ndx) and then added records. Only records added after the .ndx where created showed up properly.   An .mdx was maintained and updated every time I added a new record.

IndexFieldNames is an alternative way to list columns for an index.  For dBASE and Paradox tables, the columns must already be indexed.  For SQL engines they don't already have to be indexed.

IndexName is so that you can use a secondary index.  If this is blank, the primary key is used.

You cannot specify an IndexName *and* IndexFieldNames.  You must use one or the other.

IndexFiles is used to specify a dBASE index file (eg .mdx) for use with your table.  All of the indexes available in your .mdx will show up the in the IndexName drop down list where you can pick the actual index you want to use.

I got this information out of Delphi help.

BTW, my test was with a DBASE IV dbf.
0
 

Author Comment

by:Monroe406
ID: 1357494
>> What you might consider doing is using an .mdx instead of an .ndx.

How can I do this?  Database Desktop 7 only creates .NDX files with DBF III files (which is what I am using), not .MBX files!

>>IndexFieldNames is an alternative way to list columns for an index.  For dBASE
>> and Paradox tables, the columns   must already be indexed.

OK, but what happens to the index when a user ADDs or DELETEs a record from the DBF table?

0
 

Author Comment

by:Monroe406
ID: 1357495
It appears that I must convert my DBF III table to DBF IV format before the MDX index format is created.  Does the "M" in "MBX" stand for "multiple"?  If so, then how do I at run time change the index?  With NDX files, I could simply...

            Table1.IndexName := 'EMAIL.NDX';
            Table1.SetKey;
            Table1.FieldByName('EMAIL').AsString := edtEmailAddress.Text;
            If Table1.Gotokey = False then begin
                  ShowMessage(CRLF4 + 'Unable to locate: '+edtEmailAddress.Text + '    ');
            end else begin
                  Table1.Edit;
                  Table1.FieldByName('CONFIRMED').AsBoolean := True;
                  Table1.Post;
            end;
            Table1.IndexName := 'LAST.NDX';

How does one specify a particular index inside a MBX file, since a MBX could contain multiple indexes?
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357496
For detailed information about the usage of XBASE files (DBF, MDX, NDX...) visit the following link: http://www.e-bachmann.dk/docs/xbase.htm

Info also available on Borland site: http://loki.borland.com/winbin/bds.exe?getdoc+2821+Delphi
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1357497
With an mdx you do the same thing except you leave off the extension.

Table1.IndexName := 'LAST';

where 'LAST' is one of the indexes in your (DBASE IV) mdx file.

When you first point your Ttable component to your dbf, the IndexName property (dropdown list) will automatically populate with the names of the indexes you created.  You can just select the one you want at design time and/or at runtime you can change your index as listed above.

I don't know how factual this is, but I was under the impression the 'm' stood for master.

Give that a try and let us know how it goes.
0
 

Author Comment

by:Monroe406
ID: 1357498
>> I don't know how factual this is, but I was under the impression the 'm'
>> stood for master.

According to: http://www.e-bachmann.dk/docs/xbase.htm#MDX_STRUCT it stands for "multiple index file".

>> Info also available on Borland site: >>http://loki.borland.com/winbin/bds.exe?getdoc+2821+Delphi

This doesn't tell me about how to use DBF files.  It's just info on the internal structure of DBF files.

I guess the question I have is this...does Delphi maintain the NDX files to a DBF III table?  For it would appear from my testing that it does not maintain NDX files.  When I add new records to a DBF III file, the index gets screwed up.  When I detach the NDX file, then all the records appear (but of course they are no longer sorted).  Deleting the NDX file, and recreating it with Database Desktop 7 causes the problem to go away...until I add a new record programatically.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:Matvey
ID: 1357499
What I can tell so far, that NDX contins only one index and MDX can contain several. I think that the problem you're having is rather uniq (haven't seen such thing before), and if it's related somehow to the NDX structure then it should be documented in pages like I posted.
It could be also one of the BDE bugs - try downloading the last version of the BDE.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357500
What I can tell so far, that NDX contins only one index and MDX can contain several. I think that the problem you're having is rather uniq (haven't seen such thing before), and if it's related somehow to the NDX structure then it should be documented in pages like I posted.
It could be also one of the BDE bugs - try downloading the last version of the BDE.
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1357501
Apparently the dBASE III ndx file is not maintained.

I made a test table and created several indexes on it.  If you notice, when you create an index on a dBASE III table, the 'maintained' check box is not checked (by default).  When I created indexes like this, I got an ndx file that was not maintained.

So then I checked the 'maintained' box and created an index.  Instead of an ndx I got an mdx that appeared to be maintained on entering of data.

Take a look at your ndx indexes and see if the maintained box is checked or not.  I would wager that it is not.

Hope this helps!
0
 
LVL 1

Expert Comment

by:pjdb
ID: 1357502
With DBase III, you need to specify the index file (the ndx file) when you open the database. This has become automatic with the dBase IV and the mdx index files. Why not using dBase IV files and mdx indexes?

i've never try using dBase III files with the BDE, but you may have to the same.
0
 
LVL 5

Accepted Solution

by:
mayhew earned 150 total points
ID: 1357503
Monroe, are you still interested in this question?

I'm going to rephrase my last couple of posts as I think they contain the answer to your original question.

DBASE III ndx files are not maintained so you can't use them for your app.  Instead, make them maintained (or rather an mdx) and this should clean up your problem.

I don't think it really matters what version of DBASE you use as long as you make sure your indexes are maintained.
0
 

Author Comment

by:Monroe406
ID: 1357504
But how does Delphi know to maintain a DBF file?  Are not the "Maintained" and "Unique" check boxes in Database Desktop valid only while you are editing the DBF file from the Database Desktop?  At runtime, how does Delphi know that a DBF file is to be maintained, and that only unique records are to be entered?
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1357505
Monroe, I believe there is something in the mdx or ndx that tells Delphi what's going on.

I wish I could speak more authoritatively on the mechanics of the subject.  But if your question is whether options you set in the Database Desktop only work while in the Database Desktop, the answer is no.  The options you set are valid for the table regardless of what tool you use to access the data.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now