S
S
sakhdoc2015-10-19 17:55:54
Delphi
sakhdoc, 2015-10-19 17:55:54

How to properly use DBLookupComboBox to fetch data from a table (Delphi 7)?

There is an application from a DB on access from two tables. The application has two forms, each of which refers to a table.
The first table stores basic information (ID) with decryption (Name).
The second table is a slave. It stores information about the additional data described in table 1.
It is necessary to make sure that in the second form the "ID" field is filled out not by hand, but picks up the value of the "ID" field from the first table.
To do this, I used the DBLookupComboBox component, stuffed an additional ADOTable2 and DataSource2 onto the form to communicate with the first table.
Faced a problem. When I fill the DBLookupComboBox field with a drop-down list, the program creates a new entry in the table with this ID. And the remaining 3 fields, which are just text, go to the next line, i.e. in entry #2. Because of this, I can not make connections in the database (due to the fact that the key field is empty).
This results in a one line shift. How to make it so that all fields are recorded ONLY when the add button is clicked?
the code to fill is written:

procedure TForm2.Button1Click(Sender: TObject);
begin
ADOTable1.Insert;
ADOTable1['id']:=DBLookupComboBox1.Text;
ADOTable1['test1']:=Edit1.Text;
ADOTable1['test2']:=Edit2.Text;
ADOTable1['test3']:=Edit3.Text;
ADOTable1.Post;
begin
  Edit1.clear;
  Edit2.clear;
  Edit3.clear;
end;
end;

I'm attaching the screenshot and app.
Ps I cooked up the application in haste, you may have to fix the paths in the AdoTable components.
956054aebb24411db02c6273f0b6819a.jpgAppendix

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sakhdoc, 2015-10-19
@sakhdoc

I solved the problem as follows:
1. I created a separate button that creates a new (empty) line, and at the same time writes the value from this field.
2. left a record of the remaining fields in the second button. got this code:

procedure TForm2.Button3Click(Sender: TObject);
begin
ADOTable1.Insert;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
 if Edit3.Text = '' then Edit3.SetFocus;
 if Edit2.Text = '' then Edit2.SetFocus;
 if Edit1.Text = '' then Edit1.SetFocus;
 if (Edit1.Text = '') or (Edit2.Text = '') or (Edit3.Text = '') then begin
 ShowMessage('Не заполнено поле!');
 exit;
end;
ADOTable1['test1']:=Edit1.Text;
ADOTable1['test2']:=Edit2.Text;
ADOTable1['test3']:=Edit3.Text;
ADOTable1.Post;
begin
  Edit1.clear;
  Edit2.clear;
  Edit3.clear;
end;
end;

How it works:
1. add an empty string;
2. select the value in the DBLookupComboBox1 field;
3. fill in the rest of the fields;
4. click on the "Record" button. and we get what we need.
Minus two:
1. A new button has been added. Now, to make a new entry, you need to click on two buttons each time, instead of one:
2. If you have entered new data in table 1, in order to be picked up by the DBLookupComboBox1 component, you need to restart the program.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question