[Solved] Button to set inital values in form

Creating and using forms
Post Reply
ddauphin
Posts: 4
Joined: Sun Jan 18, 2009 1:15 am

[Solved] Button to set inital values in form

Post 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.
Last edited by ddauphin on Sun Jan 18, 2009 3:27 am, edited 1 time in total.
OOo 3.0.X on openSuse 11 + WinXP
User avatar
Villeroy
Volunteer
Posts: 31284
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Button to set inital values in form

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
ddauphin
Posts: 4
Joined: Sun Jan 18, 2009 1:15 am

Re: Button to set inital values in form

Post 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?
OOo 3.0.X on openSuse 11 + WinXP
User avatar
Villeroy
Volunteer
Posts: 31284
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Button to set inital values in form

Post 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.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
ddauphin
Posts: 4
Joined: Sun Jan 18, 2009 1:15 am

Re: Button to set inital values in form

Post 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!
ddauphin
Posts: 4
Joined: Sun Jan 18, 2009 1:15 am

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

Post 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.
OOo 3.0.X on openSuse 11 + WinXP
Post Reply