Board index » delphi » DATABASE LOOKUP USING DBGRID - HELP!

DATABASE LOOKUP USING DBGRID - HELP!

Hi there,

I have 2 tables:  Simply TABLE1 and  TABLE2 .    On my form I have a
DBGrid of Table 1.   I have written an OnDoubleClick event handler for
a particular Column which refers to a Companies Name.

Table 2 contains a list of all Companies and their details.   What I
need is code to extract the existing Company Name from Table 2 and
insert it in Table1 DBGrid's Cell.  

The easy was out is using a DBLookUpCombo but I need it using the
DBGrid.

By double clicking on this particular cell / column,  I need a dialog
window to pop up with a list of all company names in Table2.  By
double clicking on the relevant company or selecting one and clicking
OK, the code should transfer this data across to the DBGrid's column.

The example of MAST in Delphi is driving me nuts!

Help!  I am sure it's not too difficult, maybe a dialog box and a
function.  Can one perform this without the Show / Hide form
functions?

I will appreciate any help on this!

Regards

 

Re:DATABASE LOOKUP USING DBGRID - HELP!


Quote
Anton Grobbelaar wrote:

> Hi there,

> I have 2 tables:  Simply TABLE1 and  TABLE2 .    On my form I have a
> DBGrid of Table 1.   I have written an OnDoubleClick event handler for
> a particular Column which refers to a Companies Name.

> Table 2 contains a list of all Companies and their details.   What I
> need is code to extract the existing Company Name from Table 2 and
> insert it in Table1 DBGrid's Cell.

> The easy was out is using a DBLookUpCombo but I need it using the
> DBGrid.

> By double clicking on this particular cell / column,  I need a dialog
> window to pop up with a list of all company names in Table2.  By
> double clicking on the relevant company or selecting one and clicking
> OK, the code should transfer this data across to the DBGrid's column.

> The example of MAST in Delphi is driving me nuts!

> Help!  I am sure it's not too difficult, maybe a dialog box and a
> function.  Can one perform this without the Show / Hide form
> functions?

> I will appreciate any help on this!

> Regards

If I have it correctly, DBGrid is connected to Table1. You want a Lookup
field for the Company Name in the DBGrid from Table2. If I have your
intentions correct then you need to do as follows:

On the form where the DBGrid is, you should have both Table components.
The Right click on Table1 to bring up the Fields editor. Then right
click in the empty fields editor and choose add Fields. Then right click
again and choose add new field.This will bring the new fields dialog.
Once there, supply a name for the lookup field(Company Name), The next
box should be alright the way it is(Table1), then choose a field
type(String). The supply the field size. Next choose the LOOKUP radio
button. Lastly in the lookup definition section select the
keyfield(Company Name), the dataset (Table2), the next two should be
the Company name. That should do it with the fields dialog.

Now, Make sure the DBGrid is active.

The new lookup field should be there. If not then right click on the
dbgrid to bring up the columns editor. If the lookup field is not in
the list, then choose add new and pull down the field list and choose
the lookup field. That should do it. When running the program, you can
click on the lookup field in the grid and drag it to where ever you
want. when you tab to the new lookup field, clicking on it will bring up
the list. good luck! any problems let me know.

Re:DATABASE LOOKUP USING DBGRID - HELP!


Anton Grobbelaar <fl...@mail.global.co.za> wrote in article
<54nop4$...@osprey.global.co.za>...

Quote
> Hi there,

> I have 2 tables:  Simply TABLE1 and  TABLE2 .    On my form I have a
> DBGrid of Table 1.   I have written an OnDoubleClick event handler for
> a particular Column which refers to a Companies Name.

> Table 2 contains a list of all Companies and their details.   What I
> need is code to extract the existing Company Name from Table 2 and
> insert it in Table1 DBGrid's Cell.  

1) Add a calculated field to TABLE1, called CompanyName using the fields
editor.
2) Create another TTable component on your form that also accesses TABLE2.
Call it TABLE2b.
3) Override the OnCalcFields event of TABLE1, and put the following:

        begin
                if (TABLE2b.findkey([DataSet.FieldByName('Company').AsString])) then
                        DataSet.FieldByName('CompanyName').AsString :=
TABLE2b.FieldByName('Name').AsString
                else
                        DataSet.FieldByName('CompanyName').AsString := 'invalid company!';
        end;
4) Now each record of TABLE1 will display company names, not numbers!

NOTE:  If you don't use a second TTable, then every time you switch to a
different record in TABLE1, then
        TABLE2 will jump to that companies record also.  The second TTable will
cause TABLE2 to
        be unaffected.  Maybe the prior is the effect you want.  If so, then just
use that one.

Quote
> The easy was out is using a DBLookUpCombo but I need it using the
> DBGrid.

> By double clicking on this particular cell / column,  I need a dialog
> window to pop up with a list of all company names in Table2.  By
> double clicking on the relevant company or selecting one and clicking
> OK, the code should transfer this data across to the DBGrid's column.

??? I don't understand the question.

Dave Calkins
st93z...@dunx1.ocs.drexel.edu

Other Threads