Alright so the basic script for this.
You have a few choices here. Choice one include your datetime field in the query that you build your form on, either displayed or not, of course if your form is based on the actual table this is what you have.
Choice two do not include the datetime field in the query.
I never quite know where to start on this.
I suppose just jump in with an example:
Attached to this post you will find a database file named "current_timestamp.odb".
It has one table, Table1
ID IDENTITY Primary Key, VAL_1 VARCHAR(50), LastEdit Timestamp
The command ALTER TABLE "Table1" ALTER COLUMN "LastEdit" SET DEFAULT CURRENT_TIMESTAMP was applied from the SQL window.It has one form, Table1 - based on the table Table1.
The form displays a grid with 3 columns - Val_1, LastEdit(Date), LastEdit(Time)
The ID field is hidden by default, LastEdit(Date), LastEdit(Time) are read only.
At this point you can open the form add a record by entering some data in Val_1 and you will see that when the record is written to the table the LastEdit fields are automatically filled with the current date and time.
OK - you should have grabbed a copy of that database.
Now at this point if you edit an existing record the LastEdit value does not change, so that is what you will add now.
The first thing to do is to create our script. Open the database on your desktop. From the main window select 'Tools > Macros > Organize macros > OpenOffice.org Basic'
You will want to put your script ( macro ) in the standard library under 'Your Macros'

- Basic macro dialog
I want you to select "Module1"under "Standard" and click on 'Edit'.
In the Basic IDE window that opens you copy the following script ( macro )
Code: Select all
Sub onUpdateRecord( oEvent as object )
dim oDataForm as variant
dim oStatement as variant
dim oResultSet as variant
if oEvent.Source.SupportsService( "com.sun.star.form.component.DataForm" ) then
if oEvent.Action = com.sun.star.sdbc.ChangeAction.UPDATE then
oDataForm = oEvent.Source
oStatement = oDataForm.ActiveConnection.CreateStatement
oStatement.EscapeProcessing = False
oResultSet = oStatement.executequery( "CALL CURRENT_TIMESTAMP" )
oResultSet.next
oDataForm.updateTimeStamp( oDataForm.FindColumn( "LastEdit" ), oResultSet.getTimestamp( 1 ) )
end if
end if
End Sub
| Edit: Corrected typo and added explicit call to turn escape processing off |
You can close the Basic IDE.
Now from the Base window open the form 'Table1" for editing. Right click on the form, select edit.
With the form open for edit ( design mode on ) you can assign the macro.
To do this you want to open the form navigator - this is on the toolbar 'Form Design'.

- Form Navigator
- Form Navigator.png (5.29 KiB) Viewed 59845 times
Notice that I have highlighted the entry "MainForm". This is the hidden dataform that is the link between the data table ( or query ) and the form controls. It is where we want to assign the macro. So right mouse click on this and select 'Properties'.
On the property editor select the Events tab.
You will find an event 'Before Record Actrion', click on the button just to right of this field.
Now browse to 'My Macros', Standard, Module1 and select the macro onUpdateRecord.
Close the forum and save the database.
Your done.
Try it out, you should be able to add a record in the form, the timestamp is set - edit a record and it changes to the current date time also.
What does the code mean - line for line...well, first you could check out the Basic Programmers Guide. You can find a reference to that at the top of the Macros forum on this site.
or
If you are going to need to do this on more then one form, maybe I could show you how to make that macro a bit more generic and dissect the lines as we go.
Drew