How to allow the insertion of information in the database using Dblookupcombobox?

Asked

Viewed 781 times

1

I have a DBLookupComboBox capturing data from the table Species for the user to select what they need, after that this data is saved in the table Animal.

My question is how do I manage to allow the user to add a new record on DBLookupComboBox at runtime and that it is saved to the database after the user confirms that they want to do so.

  • Let’s say you have an animal, and when choosing the a species in dblookcombobox there is no relative, so you want the user can register a new species, choose it and then register the animal?

  • @Artur_indio, I want him to type in his own combo and already register.

  • 1

    Programmatically there are many ways to do this, using the Dblookupcombobox or Dbcombobox components will not work because you cannot edit them. You can use a Combobox and then load the items from the dataset to it and then let the user edit and put a new one, when you click the save button you make a way to also save the species table, the way you save and generate the id will be the id that goes to animal.

  • @Artur_indio, would I have to set some property to allow the user to edit? Formulate this in an answer that I believe will solve my problem :D

  • @Artur_indio, eh como dito pelo Qmechanic73?

  • 1

    I think that as stated by Qmechanic73 does not, because you have to save the species ID before, to save the animal using the species ID, the animal table has a foreign key to the right species?

  • @Artur_indio, I agree with you, you have to do it this way. Thank you very much

  • I particularly do this way, I use Dblookupcombobox to bring the species names, in case you don’t have the species for that animal I put a button next to Dblookupcombobox that opens a window to register the new species, when the user type the name of the species and give the ok, I give a refresh in the dataset which the Dblookupcombobox is on and put it to the last record, in case what the user has just registered.

  • Cool @Artur_indio, a great idea

Show 4 more comments

1 answer

1


If your idea is to get the user to edit the selected content on DBLookupComboBox, this may not be possible as the purpose of this control is to list the data of a table as specified in the property ListSource.

If that’s what you want to do, maybe the DBComboBox can best serve this case.

What you can do to get around this is through the event onClick one-button.

const
MSG1 = 'Digite uma nova espécie:';
MSG2 = 'Tem certeza que deseja gravar a espécie %s no banco?';
var
 Entrada: string;
begin
Entrada := InputBox(Caption, MSG1, '');
if Length(Entrada) = 0 then Exit;
if MessageDlg(Format(MSG2, [Entrada]), mtConfirmation, [mbYes, mbNo], 0) = mrNo then Exit;

Tabela1.Append;
Tabela1.FieldByName('Campo1').AsString := Entrada;
...

It is necessary to link the property ListSource of DBLookupComboBox at the DataSource of the desired table.

  • 1

    Thank you for the answer :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.