Solved

DB Grid "AutoFill" Please Help

Posted on 1997-10-25
4
184 Views
Last Modified: 2010-04-04
I am writing a database program for my final year project and I'm stuck with this problem, could somebody assist me.

I would like to enter a two character airline code into one field and have the corresponding airline field automatically fill with the correct airline name, for example when the user types AF in the airline code DBEdit box the program autofills the airline DBEdit box with the airline name AIR FRANCE and then tabs onto the next entry field.

I would be greatful if someone could offer assistance.

Regards
0
Comment
Question by:tam97
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
itamar earned 150 total points
ID: 1348889
I think the right approach is a code like this:

procedure TForm1.DBEdit1Change(Sender: TObject);
begin
With Sender as TDBEdit do begin
if Length(Text) = 2 Then begin
  DBEdit1.Text := AirLineName(Text);
  //AirLineName is the procedure that returns the airline name
  ActiveControl := TWinControl(Components[ComponentIndex + 1]);
end;
If there are any doubts, place a comment before rejecting the answer.

IHTH
0
 

Author Comment

by:tam97
ID: 1348890
Hi Itamar,
Thanks for such a speedy reply, this is my first time using the service,very impressed !

I am very much a beginner with Delphi would it be possible for you to explain your answer in a bit more "beginners detail" sorry for being a pain.

It may help if I explain the database is using Paradox tables constructed using the Desktop database tool that comes with Delphi 3, the airline table has two fields 1/ Airline Code 2/Airline Name. This table is linked with one other to the main table which contains the rest of the data.

Thanks again for your reply

Regards
0
 
LVL 4

Expert Comment

by:itamar
ID: 1348891
I think i can make things more simple.
Here is a sample working code with comments.
I supose you're using TTable components to manage your tables, so i did my sample on it.
I don't know your reasons, but consider using a BDLookUpComboBox instead, because it's a "clean" solution.
Feel free to place comments if you still have some doubts regardind to THIS question.

The code:

unit Unit2;

interface

uses
      SysUtils, Windows, Messages, Classes, Graphics, Controls,
      StdCtrls, Forms, DBCtrls, DB, DBTables, Mask, ExtCtrls,
      Dialogs, Buttons;

type
      TForm2 = class(TForm)
        Table1RegistrationNumber: TFloatField;
        Table1AirLineCode: TStringField;
        Table1Flight: TFloatField;
        Table1Hour: TTimeField;
        DBNavigator: TDBNavigator;
        Panel1: TPanel;
        DataSource1: TDataSource;
        Table1: TTable;
    Table2: TTable;
        Table2AirLineCode: TStringField;
    Table2AirLineName: TStringField;
        Table1AirLineName: TStringField;
    Label1: TLabel;
    EditRegistrationNumber: TDBEdit;
        Label2: TLabel;
    EditAirLineCode: TDBEdit;
    EditAirLineName: TDBEdit;
        Label3: TLabel;
    EditFlight: TDBEdit;
    Label4: TLabel;
        EditHour: TDBEdit;
    procedure FormCreate(Sender: TObject);
        procedure EditAirLineCodeChange(Sender: TObject);
        function AirLineName(Code: String): string;
      private
        { private declarations }
      public
        { public declarations }
      end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

function TForm2.AirLineName(Code: string): string;
begin
      Table2.IndexName := ''; //Sets the index to the primary key
      if not Table2.FindKey([Code]) Then begin
            ShowMessage('Air Line not found !'); //Dialogs unit in uses clause
            Result := '';
            Exit;
      end;
            Result := Table2['AirLineName'];
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
      Table1.Open;
      Table2.Open; //Opens the AirLine Table
end;

procedure TForm2.EditAirLineCodeChange(Sender: TObject);
begin
      With Sender as TDBEdit do
      if Length(Text) = Field.Size Then begin //Field size is more generic, so you can change the size of the field tomorrow
            EditAirLineName.Text := AirLineName(Text);
            //AirLineName is the function that returns the airline name
            if EditAirLineName.Text <> '' Then
                  EditFlight.SetFocus; //Replace EditFlight with the name of your next field in the form
      end;
end;

end.
0
 

Author Comment

by:tam97
ID: 1348892
Hi itamar

Thanks once again for your time and coming down to my level, I understand this answer much better and intend to implement it into my program for college with acknowledgements to yourself.

I look forward to using the service again and you never know you might be able to help again, in fact I know I'll be using the service again !.

Thanks again
tam97
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

14 Experts available now in Live!

Get 1:1 Help Now