Link to home
Start Free TrialLog in
Avatar of Monroe406
Monroe406

asked on

How to use dDase NDX index files?

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?

Avatar of mayhew
mayhew

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.
Avatar of Monroe406

ASKER

>> 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?

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?
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
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.
>> 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.
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.
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.
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!
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.
ASKER CERTIFIED SOLUTION
Avatar of mayhew
mayhew

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
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?
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.