Page 1 of 1

[Solved] Subform synchronization

Posted: Sat Nov 10, 2012 3:49 pm
by george++
Hi,
I have a main form and a subform. I need to syncronize the subform when I make a change in a record of the main form.
Unfortunatelly I dont know much about macros and events. Is there anyone can help me?

Thanks in advance

Re: Subform synchronization

Posted: Sat Nov 10, 2012 4:11 pm
by Villeroy
method #1: Go to another record and then back to the modified record.
#2: Add a push button to the subform with property "Action"="Refresh form".

Re: Subform synchronization

Posted: Sat Nov 10, 2012 11:07 pm
by george++
Villeroy, thank you very much. The second method works like a charm!
Just for fine tuning:Is it possible to generate a "Refresh" action of the subform when a list box, from the main form, changes it's selection?

Re: Subform synchronization

Posted: Mon Nov 12, 2012 10:47 am
by DACM
george++ wrote:Is it possible to generate a "Refresh" action of the subform when a list box, from the main form, changes it's selection?
The same 'Refresh form' action applied to a push-button on the SubForm will also process List Box selections on the MainForm. If you prefer a more automated approach, then you'll need a macro applied to an appropriate List Box Event. Here's a demo of both techniques:
http://forum.openoffice.org/en/forum/vi ... 13#p252113
...

Re: [Solved] Subform synchronization

Posted: Mon Nov 12, 2012 5:16 pm
by george++
Thank you again.
I looked into the code and although I am an experienced C++ programmer I feel helpless because I can't find where are the properties of an 'object' and what are represent.
Let's say:

Code: Select all

oForm = oEvent.Source.Model.Parent
I don't know, for example, that the object has a property named 'Source' (or method) and the 'Source' has a 'Model' and the 'Model' has a property named 'Parent' (or method)
For example, what represents the 'Model'?
I search to the Wiki section but I didn't find detailed explanations and answers to the questions above.
I don't know where to start.

Re: [Solved] Subform synchronization

Posted: Mon Nov 12, 2012 8:39 pm
by DACM
I see that you've marked this thread 'Solved' so I presume you're happy with Villeroy's solution. That's excellent. :D

With reguard to your follow-up question about List Boxes, I presume from your response above that you're simply expressing an interest in the *ooBasic macro language or underlying UNO API -- in general -- because you're an experienced C++ programmer. I'll offer you some links (below) as food-for-thought on the topic, but I don't recommend learning anything about macros or objects in pursuit of an automated solution to your List Box question.

Concerning your List Box question: I assumed that you had tested the push-button solution with a List Box, so I offered the additional macro-driven solution in response. The macro was offered as a plug-in solution requiring no additional knowledge beyond built-in Base facilities. In other words, the manual push-button solution requires you to dive under-the-hood to assign an appropriate form-refresh 'action' while also placing the button on the appropriate SubForm using the Form Navigator. Likewise, a boilerplate macro requires you to copy&paste the macro to your Base (.odb) document, assign the macro to the designated 'event' on the Events tab of the List Box, and ensure the macro code reflects the correct SubForm 'name' as reflected in the Form Navigator. That's it. There's no intent to drive you into actual coding in a case like this. For example...

Given the following macro from the demo file:
  • Code: Select all

    Sub RefreshForm (oEvent as Object) 'Form > List Box > Events > Execute action
    	oForm = oEvent.Source.Model.Parent
    	oEvent.Source.Model.commit() 'save the List Box selection to the host data-Form
    	'save all changes to the MainForm's Table... compiler bug doesn't allow comments on an IF-THEN-ELSE line
    	IF oForm.isnew THEN oForm.insertRow() ELSE oForm.updateRow() 
    	oForm.GetByName("SubForm1").reload 'reload the SubForm's individually to avoid jumping to the first record
    	oForm.GetByName("SubForm2").reload 'reload the SubForm's individually to avoid jumping to the first record
    End Sub
Your task is to:
  • 1. copy this macro to your Base (.odb) file as shown in the demo (.odb) file
    2. assign the macro to the event indicated in the macro comment: Form > List Box > Events tab > Execute action
    3. replace the name of your SubForm as appropriate in the macro code; your SubForm name is determined by opening the Form Navigator
    4. delete the last line of the macro if you don't have multiple SubForms to refresh
So your macro will likely become:
  • Code: Select all

    Sub RefreshForm (oEvent as Object) 'Form > List Box > Events > Execute action
    	oForm = oEvent.Source.Model.Parent
    	oEvent.Source.Model.commit() 'save the List Box selection to the host data-Form
    	'save all changes to the MainForm's Table... compiler bug doesn't allow comments on an IF-THEN-ELSE line
    	IF oForm.isnew THEN oForm.insertRow() ELSE oForm.updateRow() 
    	oForm.GetByName("SubForm").reload 'reload the SubForm's individually to avoid jumping to the first record
    End Sub
The only change is the SubForm name ("SubForm") and elimination of the last line as necessary. Actually, "SubForm" is the default name assigned by the Base Form wizard, so you'll find the exact final code above in many of my demos. I simply failed to point you to one of those demos in this case, so slight modification became necessary. :oops: That would have eliminated steps 3 & 4 above -- leaving a relatively simple, 2-step process similar to that of implementing a push-button.


As promised, some food-for-thought on macros. Note that Introduction into object inspection with MRI]object inspectors are available as add-ons or extensions:

Re: [Solved] Subform synchronization

Posted: Tue Nov 13, 2012 12:03 am
by george++
I'll offer you some links (below) as food-for-thought on the topic, but I don't recommend learning anything about macros or objects in pursuit of an automated solution to your List Box question.
Thanks a lot