Ask Your Question

Entering an integer value into a field in a Base form with a macro

asked 2017-08-02 21:22:53 +0100

baseplodder gravatar image

Please could someone tell me how to improve this simple macro: It is meant to add an integer value into a particular field in a form so that when the form is saved this value is part of the new record. If the form is called 'Form' and the name of the field where the value should be added is "Field" and the integer variable whose value is to be stored is called 'x' then trying

Dim oForm As Object
Dim oField As Object
Dim x As Integer
oForm = ThisComponent.Drawpage.Forms.getByName("Form")
oField = oForm.getByName("Field")
oField.Text = x

displays the value x in the field but it fails to appear on the new record when the form is saved. Anything that I enter into other fields is however saved. (If I overwrite the value in 'Field' after it has been displayed by the macro it will also be stored in the new record!). Hope someone will save me from further bashing my head against the wall.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-08-02 21:38:18 +0100

Ratslinger gravatar image

updated 2017-08-03 03:58:05 +0100

Just happened to be doing same in a Firebird test. Best to not just move into control (good for display) but move into record itself. From oForm get the column to be affected the move the value there. Columns (fields of records) are indexed starting at 0. So if the table field is ABC and the third field in the record definition, then:

oColumns = oForm.getColumns()
oColumns.getByIndex(2).Value = x

Your field should be stored when the record is updated/saved.


Just to keep all upfront, another way to do this is by getting the View of the control & issuing a commit():

Dim oForm As Object
Dim oField As Object
Dim DocCtl As Object
Dim CtlView As Object
Dim x As Integer
oDoc = ThisComponent
DocCtl = oDoc.getCurrentController()
oForm = ThisComponent.Drawpage.Forms.getByName("Form")
oField = oForm.getByName("Field")
CtlView = DocCtl.getControl(oField)
CtlView.Text = x
'CtlView.setFocus()      'Remove comment to set focus at this control

The above is based upon your question. It gives you a way to display & make sure the data is applied to the record.

And finally, in your example just add oField.commit() as the last line in your routine. That should work also.

Just wanted to present you with various ways to accomplish basically same thing. Of course there are advantages to using each under specific circumstances such as wanting to set the focus at a control or a blind update of a field (first code).

If this answers your question please click on the ✔ (upper left area of answer).

edit flag offensive delete link more


Many thanks not only for the quick reply but also for the alternative solutions. Not only has it solved my problem but your answer also gives insights into other useful techniques. Thanks.

baseplodder gravatar imagebaseplodder ( 2017-08-02 22:10:47 +0100 )edit
Login/Signup to Answer

Question Tools



Asked: 2017-08-02 21:22:53 +0100

Seen: 250 times

Last updated: Aug 03 '17