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

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")
oForm.moveToInsertRow
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.

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.

Edit:

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")
oForm.moveToInsertRow
oField = oForm.getByName("Field")
CtlView = DocCtl.getControl(oField)
CtlView.Text = x
'CtlView.setFocus()      'Remove comment to set focus at this control
oField.commit()

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 :heavy_check_mark: (upper left area of answer).

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.