2

This is somewhat similar to a master-detail situation, with some variation.

Problem: I have two independant MS Access database files (.mdb), which need to be linked.

  • First database has part numbers, part details, vendors, etc...
  • Second database has individual part numbers as tablenames, and within those tables are the customers that use that part, and product number.

I couldn't find an appropriate master-detail linking method, mainly because the tablename has to change on the fly as cell data changes/updates in the grid.

So, I came up with an alternative linking method which is as follows. And I'm not entirely certain this is a good way to link them.

{DBGrid1 displays values from ADOTable1 } {DBGrid2 displays values from ADOTable2, which its tablename changes}

procedure TForm1.DBGrid1CellClick(Column: TColumn);
var PartTableName: string;
begin
if DBGrid1.Fields[2].IsNull then exit
  else
   begin
     if ADOTable2.active then ADOTable2.Close;
     PartTableName:= DBGrid1.Fields[2].AsString;
     while pos('-',TableString) > 0 do
       PartTableName[pos('-',PartTableName)] := '_';
     ADOTable2.TableName:= PartTableName;  //obtained from cell click in DBGrid1
     ADOTable2.Open;
   end;
end;

DBGrid2 displays the customers & products that use whatever part number, that is clicked on in DBGrid1.

There must be a better way of doing this? This way of linking seems crude to me. I'd also rather have this in the OnDataChange method, but doesn't seem to work.

7
  • Why not merge the 2 .mdb files and refactor the resulting database?
    – menjaraz
    Apr 9, 2012 at 7:17
  • True, I could do that, but I would kind of like to keep all the detail stuff all in one external .mdb file. Lumping all of them into one mdb also mean my database would end up having over 300+ tables in one file. Not really liking that idea too well. I'm pretty sure that access has a limitation of how many tables you can have within 1 file. Not sure what that is though (yet). Apr 9, 2012 at 7:22
  • 1
    I am sure menjaraz means by refactor to get rid of all those unnecessary tables. Simply add the part number as column.
    – NGLN
    Apr 9, 2012 at 7:36
  • each table is a part, and each part has several different customers that use that part, plus their product numbers. And each table points back or is linked to the part number in the first mdb file & table. Apr 9, 2012 at 7:45
  • 2
    Welcome to SO. Please consider asking a new question on how to normalize and simplify your database and table design.
    – NGLN
    Apr 9, 2012 at 8:24

3 Answers 3

3

You can set this up within Access. Thats a more robust solution than doing it in Delphi.

Find the Linked Table Manager in your version of Access and it allows you to link a table from another database into the one you have. Then you can access a single mdb with delphi and get at all the tables.

3
  • True as well. I've come across that information on the internet. However, I don't have MS Access. So I'm stuck with doing it by code. I did come across a neat little utility called MDB Plus Viewer. But haven't figured the linking part out yet. Apr 9, 2012 at 7:38
  • @M.L.Tomson: You can do it in code when you import the "Microsoft DAO 3.6 Object Library", also known as the Microsoft Jet Engine. You should be able to find it on a machine with MS Access installed. Possibly present if only MS Excel installed as that uses the Jet Engine as well. Otherwise you could try and download the MDAC (2.5 or higher) library. Microsoft changed its mind quite often on whether this would be included with Windows and/or Office or made available as a separate download. It will take a bit of OLE automation. Apr 9, 2012 at 9:32
  • @M.L.Tomson: Depending on your Delphi version, the Diamond Access components (no longer maintained but still available through Torry.net) might also be of help as they used to be the component set to access MS Access databases natively. They wrap the DAO tlb in easier to use components. Apr 9, 2012 at 9:35
2

TxQuery is another option for you.

Quote:

TxQuery component is a TDataSet descendant component that can be used to query one or more TDataSet descendant components using SQL statements.

0

Instead of requerying, I would suggest you look into using ADO components to filter the content of the table. This would be roughly equivalent to a master-detail arrangement, and would have the advantage of only performing an "in memory" operation, and not hitting the db each time the "master" row changes. (On each master row change, just change the filter criteria on the child table)

2
  • Filtering in Delphi Datasets has traditionally been VERY slow. This may not be the case with later versions of Delphi.
    – Toby Allen
    Apr 15, 2012 at 15:13
  • Filtering using ADO is very fast indeed, and always has been. Filtering with TTable+BDE could be slow, yes.
    – Warren P
    Apr 15, 2012 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.