Topic: Default value of a textbox equal to a field

Hi everyone,
it is possible to put as default value of a TextBox what is written of a field of a table. The table has only one field and one record.
Thank you and greetings.

2 (edited by derek 2019-03-24 17:01:06)

Re: Default value of a textbox equal to a field

Hi,
A couple of ways to do it.
1)  use 'sqlexecute' and select the value from the table;  as there can only ever be one row in your table, there is no need for a 'where' clause.
2)  copy the value from the tablegrid cell (as there is only one row and one column, you can hard-code the cell location ([0,0]).  Perhaps this is a slightly better option as it avoids making an extra call to the database.
Regards,
Derek.

Post's attachments

Attachment icon onerecordcopyvalue.zip 337.17 kb, 357 downloads since 2019-03-24 

3 (edited by reteinformatica 2019-03-25 17:41:44)

Re: Default value of a textbox equal to a field

Thanks derek,
studying your project I tried to adapt it to my needs but there is something I did not understand and it does not work well: I have to click twice on the button and the result does not remain permanently, would you be so kind as to look at the project that I have attached?
Thank you!

Post's attachments

Attachment icon prova.zip 321.61 kb, 307 downloads since 2019-03-25 

Re: Default value of a textbox equal to a field

Hi Reteinformatica,
I downloaded your project but there is just a form - there is no script with it so I can't see the problem with clicking twice or the result not being saved.  Nor is there a tablegrid.
Derek.

5 (edited by reteinformatica 2019-03-26 19:27:10)

Re: Default value of a textbox equal to a field

Thanks Derek,
In the meantime I have done many tests and only one thing I miss: Is there an instruction to automatically select the first line of a tablegrid so that if I press an associated button I should not first select the record from the Tablegrid?

6 (edited by derek 2019-03-26 21:20:00)

Re: Default value of a textbox equal to a field

Hi,
Yes, the instruction is in the script.
Remember that in MVD, the first column in the tablegrid starts at Column 0 and the first row in the tablegrid starts at Row 0 (this is what [0,0] in the line of script...... form1.tablegrid1.cells[0,0] refers to).  It is the co-ordinates of the cell value that we want to use.
So, in the script, the line 
if form1.tablegrid1.rowcount > 0 then form1.edit2.text := form1.tablegrid1.cells[0,0] else form1.edit2.Text:= ' ';
takes the contents of the cell in Column 0 and Row 0 in the tablegrid and copies the contents to form1.edit2.text.
Hope that's clear.
Derek.

7 (edited by reteinformatica 2019-03-27 12:58:59)

Re: Default value of a textbox equal to a field

Thanks Derek,
Unfortunately I understand little. I Put in an Annex A project that perhaps explains the matter better.
I put a button (Change) to change the value of the record of the Tablegrid but first you have to select the cell otherwise you receive the error ' Unable to certain the record ID '. I would like to hide this tablegrid but so you could not select the record before. So I would need that when the program starts the 0:0 cell is already automatically selected so that I can only click on the button without receiving the error.
To do also look at the script I created, I changed the textbox with a Label.
Thank you very much.

Post's attachments

Attachment icon Prova.zip 322.14 kb, 301 downloads since 2019-03-27 

Re: Default value of a textbox equal to a field

Hi,
Just add this line to the script
  form1.tablegrid1.selectedrow := 0;
This places the cursor on the first row in the (hidden) tablegrid and tells the program which record ID to use (you can make the tablegrid visible to see how it has highlighted the row and then make it invisible again).
Derek.

Post's attachments

Attachment icon Prova.zip 336.71 kb, 323 downloads since 2019-03-27 

9 (edited by reteinformatica 2019-03-27 14:57:55)

Re: Default value of a textbox equal to a field

Thanks Derek,
I understand something more and now something is advanced. The only thing I don't understand is why I don't work this statement

procedure Start_OnShow (Sender: string; Action: string);
begin
   if Start.grDominio.rowcount = 0 then Start.Label18.caption := Start.grDominio.cells[0,0] else Start.label8.caption := 'Text Here   --->' ;
end;

"Start" is a form.
Does not write "Text here" in the label

EDIT:

This is how I solved it:

If Start. grDominio. RowCount < > 1 then Start. Label18. Caption: = ' Text Here ---> ' ;

10 (edited by reteinformatica 2019-03-27 15:13:39)

Re: Default value of a textbox equal to a field

What's wrong with this education?

if Start.bNuovodominio.visible:= True then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->' ;

It says ' THEN execpted at 12:33'

Re: Default value of a textbox equal to a field

reteinformatica wrote:

What's wrong with this education?

if Start.bNuovodominio.visible:= True then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->' ;

It says ' THEN execpted at 12:33'

this is wrong - Start.bNuovodominio.visible: = True
Just delete the : after visible
this is true - Start.bNuovodominio.visible = True
this is full script
if Start.bNuovodominio.visible = True then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->'

12 (edited by reteinformatica 2019-03-27 16:38:46)

Re: Default value of a textbox equal to a field

Thanks Ivan,
so I don't get errors but the instruction doesn't work or the caption of laberl doesn't become "Qui va il nome del dominio, modificalo   --->" but it's empty.
Below is the total code of the script:

procedure Start_OnShow (Sender: string; Action: string);
begin
   if Start.grDominio.rowcount <> 1 then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->' ;

end;

procedure Start_grDominio_OnChange (Sender: string);
begin
  if Start.bNuovodominio.visible = True then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->' ;
  if Start.grDominio.RowCount = 1 then Start.bNuovodominio.visible := False else Start.bNuovodominio.visible := true;
  Start.grDominio.selectedrow := 0;
  Start.Label18.caption:= sqlexecute('select NomeDominio from Dominio');
  Start.Label18.font.size:= 10
end;

begin
end.


EDIT:
I put the istruction on the bottom and so it seems to work

procedure Start_grDominio_OnChange (Sender: string);
begin
  if Start.grDominio.RowCount = 1 then Start.bNuovodominio.visible := False else Start.bNuovodominio.visible := true;
  Start.grDominio.selectedrow := 0;
  Start.Label18.caption:= sqlexecute('select NomeDominio from Dominio');
  Start.Label18.font.size:= 10
   if Start.bNuovodominio.visible = True then Start.Label18.caption := 'Qui va il nome del dominio, modificalo   --->' ;
end;

begin
end.

13 (edited by reteinformatica 2019-03-27 17:25:37)

Re: Default value of a textbox equal to a field

How do you add more content for a condition? If I understand the language for the scripts is Delphi, I tried to search but I didn't find anything.
For example "if 'condition' then text1.text = something AND text2.text = something else".
I tried with "and", "&" and "&&" but it doesn't work.

EDIT:

OK, I found it

if 'condition' then
   begin text1.text = something;
             text2.text = something else";
  end;