Topic: Automatically pre fill a text field when adding a new record

Hi guys,

Thanks Derek for the last support. Can someone please help with an example of how to automatically fill a text field in a new record with an existing record from another form and table.

See attached picture for my dilemma.

Thanks guys

Post's attachments

Attachment icon show process title on new record form.png 108.39 kb, 194 downloads since 2020-06-18 

2 (edited by CDB 2020-06-18 23:21:13)

Re: Automatically pre fill a text field when adding a new record

There are three ways that I can think of.


1. Set a button to 'SHOW RECORD'  and make sure you assign the  second combobox to the correct foreign key.


2. Possibly (I haven't tried this), set the first forms combobox as the parent of the second forms combobox.


3. Use script as in the following:


procedure frmEditPart_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
     ShowRecord(frmAddAltSupplier,'productsupplier',frmEditPart.grdEditPart.dbItemID);
    // testsql.show;  do not use this line
end;

The format is  ShowRecord(Form you want the info to show on, table name that contains the data as a string, form and component info is from), in your case that would be the first combobox and it's ID.


For a combobox I think you would put the code in either the 'OnCloseUp' or 'OnChange' even of the originating combobox.



Edited to comment out tstsql.show  - this was something for me that I copied across by mistake.

On a clear disk you can seek forever

Re: Automatically pre fill a text field when adding a new record

Thanks cdb,

I tried using the script but got an error on the last part    testsql.show;

here's what I did

procedure neworeditprocess_Button5_OnClick (Sender: TObject; var Cancel: boolean);

begin
     
     ShowRecord(neworeditactivity,'processes',neworeditprocess.ComboBox1.dbItemID);
     testsql.show;

end;

Is there any thing I'm missing

Re: Automatically pre fill a text field when adding a new record

I got an "undeclared identifier" error on the test.sql

Re: Automatically pre fill a text field when adding a new record

Oh sorry, testsql.show is the name of my form for testing - remove that line.

All that does is display a form I called testsql, you don't need it.

On a clear disk you can seek forever

Re: Automatically pre fill a text field when adding a new record

Hi Senioke, Hi CDB,
Attached is perhaps another option.
ADD, EDIT and DELETE work as normal.  The COPY button takes the row that is highlighted in the grid and copies data across to the 'COPY' form;  you then make any necessary changes (if required) and then save as a new reccord. 
Depending on your requirements, you can copy all or just a sub-set of the fields from the record that is highlighted by adding more 'select' statements in your script (see lines 24-26) and these can be a mix of text, dates, relationships etc.  All the other lines are purely for cosmetic purposes and can be removed if not needed.
Regards,
Derek.

Post's attachments

Attachment icon copyrow.zip 339.68 kb, 374 downloads since 2020-06-19