[Solved] How to save current record after a field's edit?

Creating and using forms
Post Reply
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

[Solved] How to save current record after a field's edit?

Post by panoss »

I have a SubForm showing records in a grid.
One of the fields is a list box.
When I choose an item from the list, that is when I click on an item from the list, I want the data to be immediately saved in the table, without having to move to another record (clicking on another record in the grid).
I have easily managed that putting the code in a button:

Code: Select all

Sub btn_saveRecord(oEvent As Object)
	dim MainForm, SubForm as object
	
	MainForm=thisComponent.drawpage.forms.getByName("MainForm")
	SubForm=MainForm.getByName("SubForm")	
	
	if SubForm.isnew then SubForm.insertRow() else SubForm.updateRow()      
 End Sub
The same code in listbox's event 'Item status changed' does not update the table. (updates it only after moving to another record).

The only difference I can see is that when I click the button, focus moves from list box to button...
Last edited by panoss on Wed May 14, 2014 3:11 pm, edited 3 times in total.
LibreOffice 4.0.0.2 on puppy linux
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: How to save current record after a filed's edit?

Post by MTP »

Try adding a SubForm.reload() in there.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
User avatar
Villeroy
Volunteer
Posts: 31323
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to save current record after a filed's edit?

Post by Villeroy »

You are aware that every keystroke may modify the focussed list box value? You want to save the record with every keystroke?
Why did you use that code with a button? A macro free button with "action=save" property and "default button = true" (=catch Enter key) would do the job.
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
F3K Total
Volunteer
Posts: 1040
Joined: Fri Dec 16, 2011 8:20 pm

Re: How to save current record after a filed's edit?

Post by F3K Total »

Code: Select all

Sub btn_saveRecord(oEvent As Object)
    dim MainForm, SubForm as object
    ocontroller = Thiscomponent.currentcontroller
    ogrid = oEvent.source.model.parent
    ogridcontrol = ocontroller.getcontrol(ogrid)
    ogridcontrol.commit
    MainForm=thisComponent.drawpage.forms.getByName("MainForm")
    SubForm=MainForm.getByName("SubForm")   
    if SubForm.isnew then SubForm.insertRow() else SubForm.updateRow()     
End Sub

Code: Select all

.commit
replaces the enter key
R
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 10 AOO, LOLinux Mint AOO, LO
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a filed's edit?

Post by panoss »

MTP wrote:Try adding a SubForm.reload() in there.
No it doesn't work.
The record does not get saved. (I check this also by looking and in the table)
It gets saved only if the list loses focus.
LibreOffice 4.0.0.2 on puppy linux
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a filed's edit?

Post by panoss »

Villeroy wrote:You are aware that every keystroke may modify the focussed list box value? You want to save the record with every keystroke?
Why did you use that code with a button? A macro free button with "action=save" property and "default button = true" (=catch Enter key) would do the job.
I don't purposely move the focus on the list box.
When I choose an item from the list box, (clicking on it), it remains focused.
LibreOffice 4.0.0.2 on puppy linux
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a filed's edit?

Post by panoss »

The solution I've come out till now is (in List Box's event -> Item status changed ):

Code: Select all

List box event -> Item status changed

'''Save record''''''''''''''	
    oController = ThisComponent.currentController
    oGrid = oEvent.source.model.parent
    oGridControl = oController.getControl(oGrid)
    oGridControl.commit      
    if SubForm.isnew then SubForm.insertRow() else SubForm.updateRow()     
'''Remove focus from list box
    oControlView = ThisComponent.CurrentController.GetControl(SubForm_Grid)       
	 oControlView.SetFocus	
	 oControlView.setCurrentColumnPosition(1)
The code has two parts.
In the first (Save record), the record is, supposed, to be saved.
In the second (Remove focus from list box (moving focus to another field of the grid, Column Position 1)) it's actually saved.
(I remind you my purpose: save record without having to move to another record)
LibreOffice 4.0.0.2 on puppy linux
ardeley
Posts: 3
Joined: Sat Nov 24, 2012 6:00 pm

Re: How to save current record after a field's edit?

Post by ardeley »

panoss

With OpenOffice there are many programming problems which can be solved by making your macro pretend to be a user, ie by sending GUI commands to the system. In your case, a GUI user would send the "RecSave" command, by pressing the "Save Record" button on the Navigator toolbar.

I find it hard to access information on GUI commands, here is a useful link:
https://wiki.openoffice.org/wiki/Framew ... x_Commands

Just found a more up-to-date version:
https://wiki.openoffice.org/wiki/Framew ... x_Commands

There is elsewhere a suggestion for a generalised function to send such commands (scroll down to the section headed "Tidied UNO dispatch calls"):
https://wiki.openoffice.org/wiki/The_Op ... atch_calls

It's a kludgy way to do things. But often you want a quick solution rather than a beautiful one!

Adrian
OpenOffice 4.0.1 on Windows Vista
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

Thank you I 'll try them too.
I 'm not looking for a quick solution but for the best one.
LibreOffice 4.0.0.2 on puppy linux
q123
Posts: 40
Joined: Thu May 08, 2014 4:22 pm

Re: How to save current record after a field's edit?

Post by q123 »

I am a bit new at this, so I am not sure this would work but wouldn't a small save record macro in the properties: list box>events>items status changed or when loosing focus do the job? Saving on status change I would find annoying as there is too much chance saving faulty records on a miss click, on loosing focus you'd just have to tab away and back again
windows vista OOo 4.1.0
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save current record after a field's edit?

Post by RPG »

Hallo

Maybe this is working?

Code: Select all

sub CommitCurrentControlAndRefreshRecord(oEvent as object)
' I have test this macro for a listbox in a grid control with
' the event: item status changed
dim oFormModel,oformView
dim oFormOperations
oDoc=thiscomponent
oFormModel=oEvent.source.model.parent

if oFormModel.implementationname="com.sun.star.comp.forms.OGridControlModel" then
	oFormModel=oFormModel.parent ' The control is in a grid control
end if
oformView=oDoc.currentcontroller.getFormController(oFormModel) ' This is the view of the form
oFormOperations=oformView.formoperations

oFormOperations.execute(8) 'This commit currentcontrol and currentrecord
	oFormModel.absolute(oFormModel.row) ' This does a reload of the current row. And so also a refresh of the 
	' The line for reloading the current record is for reason that the subforms must also been reloaded.
end sub
Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

Let me describe the structure of the forms + Grid in my application:
MainForm
--SubForm
----SubForm_Grid
The SubForm is the container of the Grid (SubForm_Grid) (I 'm not using te 'code' tag because I want the formatting of the letters to be visible)
oFormModel=oEvent.source.model.parent ' oFormModel = SubForm_Grid

if oFormModel.implementationname="com.sun.star.comp.forms.OGridControlModel" then
oFormModel=oFormModel.parent ' The control is in a grid control oFormModel = SubForm
end if
oformView=oDoc.currentcontroller.getFormController(oFormModel) ' This is the view of the form SubForm
oFormOperations=oformView.formoperations

oFormOperations.execute(8) '
It doesn't work, the record does not get saved.
LibreOffice 4.0.0.2 on puppy linux
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save current record after a field's edit?

Post by RPG »

Hello
panoss wrote:It doesn't work, the record does not get saved.
This is not clear to me. Which record?

Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save current record after a field's edit?

Post by RPG »

Hello

The macro I did wrote should save the data-form from where the event happens. The test for the gridcontrol is not for the forms but more for the controls. The macro is only working for one data-form. There we reload the current record also the refresh of the lower subforms happens.

I do assume there are no other macro's active

A gridcontrol is a container in a data-form. The gridcontrol does have the same rowset as the data-form.

Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

RPG wrote:Hello
panoss wrote:It doesn't work, the record does not get saved.
This is not clear to me. Which record?

Romke
The current record of the Grid.

ardeley wrote:panoss

With OpenOffice there are many programming problems which can be solved by making your macro pretend to be a user, ie by sending GUI commands to the system. In your case, a GUI user would send the "RecSave" command, by pressing the "Save Record" button on the Navigator toolbar.

I find it hard to access information on GUI commands, here is a useful link:
https://wiki.openoffice.org/wiki/Framew ... x_Commands

Just found a more up-to-date version:
https://wiki.openoffice.org/wiki/Framew ... x_Commands

There is elsewhere a suggestion for a generalised function to send such commands (scroll down to the section headed "Tidied UNO dispatch calls"):
https://wiki.openoffice.org/wiki/The_Op ... atch_calls

It's a kludgy way to do things. But often you want a quick solution rather than a beautiful one!

Adrian
I haven't managed to make it work with "UNO dispatch calls'.
LibreOffice 4.0.0.2 on puppy linux
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save current record after a field's edit?

Post by RPG »

Hello

The macro must be bound to the listbox event: Item status changed.

It is working for a listbox in gridcontrol and a listbox in a form.

It is made for this purpose:
panoss wrote:When I choose an item from the list, that is when I click on an item from the list, I want the data to be immediately saved in the table, without having to move to another record (clicking on another record in the grid).
Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

Romke you were right, it does get saved!
I thought it doesn't get saved because the pencil that shows that the record is in edit mode, was still visible after the save action. (I think it's the command ' oFormOperations.execute(8)')
Attachments
OOBaseUnsaved.gif
OOBaseUnsaved.gif (744 Bytes) Viewed 28645 times
LibreOffice 4.0.0.2 on puppy linux
ardeley
Posts: 3
Joined: Sat Nov 24, 2012 6:00 pm

Re: How to save current record after a field's edit?

Post by ardeley »

panoss

So now that you've saved the data, perhaps you need to refresh the form? Will that get rid of the edit-pencil?
OpenOffice 4.0.1 on Windows Vista
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

No, form reloading causes unwanted behavior: deselects current record and selects first record.
Anyway i don't mind the edit pencil.

If I want, I can move the focus to another field (column) without changing selected row.
I don't mind as is.
LibreOffice 4.0.0.2 on puppy linux
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save current record after a field's edit?

Post by RPG »

Hello

When I change a field in the grid control then I see the pencil.
Then I change the listbox in the same gridcontrol then there is no more pencil.

So I have no problem.
There can be an other macro active on the same rowset which change the record.
There is a problem in the version used by the person who does seen the pencil.

Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

RPG wrote:Hello

When I change a field in the grid control then I see the pencil.
Then I change the listbox in the same gridcontrol then there is no more pencil.

So I have no problem.
There can be an other macro active on the same rowset which change the record.
There is a problem in the version used by the person who does seen the pencil.

Romke
No there's no other macro that changes the record.
I see you 're using a newer version of libreoffice than mine, I 'll give it a try, maybe that's the reason.
LibreOffice 4.0.0.2 on puppy linux
User avatar
Villeroy
Volunteer
Posts: 31323
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to save current record after a field's edit?

Post by Villeroy »

Enter_Items.odb
Save by enter key, no macros
(11.95 KiB) Downloaded 745 times
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
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

Villeroy no that's not what I want.
I want the record to be saved without having to move to another record.

I 've found 2 solutions by now, mine and Romke's (RPG).

Anyway thanks!
LibreOffice 4.0.0.2 on puppy linux
User avatar
Villeroy
Volunteer
Posts: 31323
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] How to save current record after a field's edit

Post by Villeroy »

Well, then change the button's action from "Next Record" to "Save Record".
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
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: How to save current record after a field's edit?

Post by panoss »

panoss wrote:
RPG wrote:Hello

When I change a field in the grid control then I see the pencil.
Then I change the listbox in the same gridcontrol then there is no more pencil.

So I have no problem.
There can be an other macro active on the same rowset which change the record.
There is a problem in the version used by the person who does seen the pencil.

Romke
No there's no other macro that changes the record.
I see you 're using a newer version of libreoffice than mine, I 'll give it a try, maybe that's the reason.
Romke for once more you were right.
There 's no edit pencil after record save. (I had some code after record save, which was changing the record :mrgreen: )
LibreOffice 4.0.0.2 on puppy linux
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: [Solved] How to save current record after a field's edit

Post by panoss »

Villeroy wrote:Well, then change the button's action from "Next Record" to "Save Record".
I changed it, I think I 'm close.
I only have to make the button being clicked with code. (to raise it's click event with code)
LibreOffice 4.0.0.2 on puppy linux
User avatar
Villeroy
Volunteer
Posts: 31323
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] How to save current record after a field's edit

Post by Villeroy »

Hitting the enter key is too much?
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
RPG
Volunteer
Posts: 2257
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: [Solved] How to save current record after a field's edit

Post by RPG »

Hello

I think this thread proofs again that Villeroy has right when he tell every time: avoid macros and have a good knowledge about the tool you use.

In the case of forms in OOo you must have knew knowledge about controls and the little properties in the controls. For the database you need knowledge about the database engine you use.

Getting those knowledge is not easy for normal users, as me. I think reading simple tutorials and reading the help file of OOo is important. I still read the help file there I do not know all details in the help file. For the Dutch forum I have to spent more time to OOo-calc and that makes I have to read more about the pivot tables.

Some time ago on the Dutch forum was a problem about importing data in a spreadsheet. It was solved with lots of functies and sheets. When it was done in a single column and a pivot table OOo did every thing in one or three seconds. This all was possible in Excel and OOo-calc. I do tell about Excel there the sheets started in Excel.

I think it is good to work with the more powerful parts of OOo and accept some more clicks. It is more easy to learn other people to work with the more powerful tools of OOo then learning macros.

Romke
LibreOffice 24.2.4.2 on openSUSE Leap 15.6
panoss
Posts: 23
Joined: Tue Apr 16, 2013 11:52 am

Re: [Solved] How to save current record after a field's edit

Post by panoss »

Villeroy wrote:Hitting the enter key is too much?
Yes.
LibreOffice 4.0.0.2 on puppy linux
mrodent33
Posts: 7
Joined: Wed Dec 08, 2010 6:52 pm

Re: [Solved] How to save current record after a field's edit

Post by mrodent33 »

... many years later...

For anyone looking how to assign a keystroke to do this it is currently (2022-03-28, LO 5.4) easy:

Tools --> Customise --> Controls --> Save record
OpenOffice 3.3 on Windows XP
Post Reply