Page 1 of 1

[Solved] Button to set inital values in form

Posted: Sun Jan 18, 2009 1:26 am
by ddauphin
Hello,

I am trying to have a button on a form set values into a text box using this code:

Code: Select all

Sub DoDate
	oController = ThisComponent.CurrentController
	oForm = ThisComponent.drawpage.forms.GetByName("MainForm")
	cControl = oForm.GetByName("DateBox")
	oControlView = oController.GetControl(cControl)
	oControlView.SetFocus  'sets the focus
	oControlView.Text = format(DATE,"YYYY-MM-DD")
End Sub
The Control updates but the underlying data does not. If I manually change the textbox moving to the next record and back saves the change. When I use the above script the text changes but moving to the next record and back looses the value.

Thank you very much for any help.

Re: Button to set inital values in form

Posted: Sun Jan 18, 2009 2:20 am
by Villeroy
Set CURRENT_DATE as default value for new records. This works with a SQL statement for all newly inserted records. If the user should be able to overwrite today's date show a label like "Date (leave blank for today's date)", if the user should not be able to overwrite the date, you can show Writer's date field.

Re: Button to set inital values in form

Posted: Sun Jan 18, 2009 2:33 am
by ddauphin
I am using HSQL database engine. I edited the table and tried setting 'CURRENT_DATE' in the default field and it does not stay there. In other words it wont save. Perhaps HSQL engine does not support this?

Re: Button to set inital values in form

Posted: Sun Jan 18, 2009 2:46 am
by Villeroy
Forget about the GUI. It can set constant default values only and fails with function names. You've got to use menu:Tools>SQL...
According to the HSQL documentation the syntax is:
HSQL wrote: ALTER TABLE <tablename> ALTER COLUMN <columnname> SET DEFAULT <defaultvalue>
For instance:

Code: Select all

ALTER TABLE "Table1" ALTER COLUMN "Date" SET DEFAULT CURRENT_DATE
... and then menu:View>Refresh Tables to notify Base about the change in the database.
If the date field is not allowed to be Null, Base will complain about the empty form control. There is a setting in main-menu:Edit>Database>Advanced... "Form checks for required fields" which has to be turned off in this case.

Re: Button to set inital values in form

Posted: Sun Jan 18, 2009 3:08 am
by ddauphin
That worked, now my new entries are Date and Time stamped by default. Thats all I need for this application.
I would still like to see a code snippet of how to update a textbox from script and have it save in the database.

Thank you for your help!

[Solved]Re: Button to set inital values in form

Posted: Sun Jan 18, 2009 3:27 am
by ddauphin
The code I used above seems to work fine for text fields. There must have been an issue with writing to a date field in a database. Nor sure but I am up and running now.
Thanks again.