| View previous topic :: View next topic |
| Author |
Message |
ch_ta General User

Joined: 05 Jan 2009 Posts: 8
|
Posted: Mon Jan 05, 2009 2:23 pm Post subject: Reload list box when exiting from opened form [SOLVED] |
|
|
Hello all, first time here. I am working on developing a functioning project database.
One issue I am having is reloading a list box for the "Projects" form based on new records input into the "Architect" form. The "Projects" form is the main form that contains all the project information. On the "Projects" form a listbox lists names of architectural firms, that may be selected for a particular project. I have a button to the side that allows a user to add additional architects. The button opens an additional form, the "Architect" form, that allows new architectural firms to be added to the database. New entries may be saved via a "Save Record" button. I desire to be able to exit out of the "Architect" form and reload the listbox in the "Projects" form.
I tried using a combination of Voobase's listbox reload macro (http://www.oooforum.org/forum/viewtopic.phtml?t=77927) and DrewJensens close macro (http://www.oooforum.org/forum/viewtopic.phtml?t=60613) on an exit button in the "Architect" form.
The macro:
| Code: |
Sub exit_reload_listbox()
dim oForm as object
Rem... Reference to the other form
oForm = thisComponent.Parent.FormDocuments
if oForm.HasByName("architect") then
oForm = oForm.getByName("architect")
if not IsNull(oForm.Component) then
oForm = oForm.Component.DrawPage.Forms.getByName("projects")
oForm.getByName("arch").refresh() Rem.. refresh listbox
End if
End if
thisComponent.CurrentController.Frame.close(true)
End sub
|
Unfortunately, the code does not allow the "Architect" form to close nor the listbox in the "Projects" form to reload. How may this be resolved?
Edit: This issue has been resolved.
Last edited by ch_ta on Tue Jan 13, 2009 2:52 pm; edited 1 time in total |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 400 Location: Australia
|
Posted: Tue Jan 06, 2009 1:48 am Post subject: |
|
|
Hi there,
You're macro is heading in the right direction... just a couple of things.
1./ The code you are using to get reference to your projects form would not need to mention the architect form but instead "project" and where you have written project you should probably had written "MainForm". Also check whether you are using capitals for these names as case is important.
2/ The close form section looks like it should work (unless Base has thrown an error before it gets to it). Using "thiscomponent" is convenient, but it is possible that before or while the code has got to that bit you have clicked on a different form and then "thiscomponent" is now the other form and the wrong one would close. If your macros grow in size then this becomes more likely. In this situation you can use similar code to how you got reference to the projects form, so as to guarantee you have reference to the correct form.
Give this a try....
| Code: | Sub exit_reload_listbox()
dim oProjForm as object
dim oArchForm as object
Rem... Reference to the other form (project). Remember case important
oProjForm = thisComponent.Parent.FormDocuments
if oProjForm.HasByName("project") then
oProjForm = oProjForm.getByName("project")
if not IsNull(oProjForm.Component) then
oProjForm = oProjForm.Component.DrawPage.Forms.getByName("MainForm")
'msgbox "About to refresh listbox"
oProjForm.getByName("arch").refresh() Rem.. refresh listbox
End if
End if
Rem... Reference to this form (architect). Remember case important
oArchForm = thisComponent.Parent.FormDocuments
if oArchForm.HasByName("architect") then
oArchForm = oArchForm.getByName("architect")
if not IsNull(oArchForm.Component) then
oArchForm = oArchForm.Component.DrawPage.Forms.getByName("MainForm")
'msgbox "About to close architect form"
oArchForm.parent.parent.CurrentController.Frame.close( true ) Rem... this way so dosen't close wrong form because of timing.
End if
End if
End sub |
The code should be driven from the "when initiating" event of your close button. A good faultfinding trick is to use msgbox in macro to show to you that the code has got to that line. Just uncomment the one's I have put in for them to function.
To check the Mainform in both your forms is actually called "MainForm" click on the "Form Navigator' button on the "Form Design" toolbar whilst you have your form in design mode.
Cheers
Voo |
|
| Back to top |
|
 |
ch_ta General User

Joined: 05 Jan 2009 Posts: 8
|
Posted: Tue Jan 06, 2009 2:51 pm Post subject: |
|
|
| Voo, I'm having trouble getting your revised macro to work. An error is thrown when I execute it by pressing the exit button on the "Architect" form, indicating that no element "Mainform" exists. I have created the "Architect" and "Projects" forms as two seperate and distinct forms. How may the code be modified in order to deal with that setup? |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 400 Location: Australia
|
Posted: Tue Jan 06, 2009 10:48 pm Post subject: |
|
|
Hi again,
You will have to confirm the names of your MainForms, whether called "MainForm", "Mainform", "Standard" or something you have renamed them to. Go into your form in edit mode (right click form and choose edit) then launch the "form navigator" which is located on the "form design" toolbar. This tree like view of your form shows all the form and control names located within your "writer doc" form. Have a look to check the names, spelling or case is right.
Don't forget to uncomment the mesage box's and put other ones in to check the code is executing correctly, for example if you uncomment the first one does it say "About to refresh listbox"?
Cheers
Voo |
|
| Back to top |
|
 |
ch_ta General User

Joined: 05 Jan 2009 Posts: 8
|
Posted: Wed Jan 07, 2009 6:44 am Post subject: |
|
|
Yes, Voo I have named the Projects form as "projects" that stands alone within its own Writer document. The Architect form is named as "architect" that stands alone within its own Writer document, as well. I built the forms manually within Design View and modified the name of the "Standard" form for the two seperate forms as above.
When initiating the script I do not get to the message box explaining that the List box is about to refresh. Instead I receive a Basic runtime error as follows:
| Quote: | An exception occurred.
Type: com.sun.star.container.NoSuchElementException
Message: .
|
I hope that makes things clearer. |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 400 Location: Australia
|
Posted: Wed Jan 07, 2009 11:13 pm Post subject: |
|
|
Hi there,
I still think it is the names of your writerdoc's and forms which are being used in the macro which are causing the problem. I have added some Rem's and message boxes in the macro to help you find what might be going on.
Let me know which of the messagebox's appear when the macro runs.
| Code: | Sub exit_reload_listbox()
dim oProjForm as object
dim oArchForm as object
Rem... Reference to the other form (project). Remember case important
oProjForm = thisComponent.Parent.FormDocuments
if oProjForm.HasByName("project_Writerdoc_Name") then Rem... Use the name of the project writer doc
oProjForm = oProjForm.getByName("project_Writerdoc_Name") Rem... Use the name of the project writer doc
if not IsNull(oProjForm.Component) then
oProjForm = oProjForm.Component.DrawPage.Forms.getByName("projects") Rem... Use the name from the form navigator
msgbox "About to refresh listbox"
oProjForm.getByName("arch").refresh() Rem.. refresh listbox
Else
msgbox "Project writer doc form isNull"
End if
Else
msgbox "Can not find the project writer doc using that name"
End if
Rem... Reference to this form (architect). Remember case important
oArchForm = thisComponent.Parent.FormDocuments
if oArchForm.HasByName("architect_Writerdoc_Name") then Rem... Use the name of the architect writer doc
oArchForm = oArchForm.getByName("architect_Writerdoc_Name") Rem... Use the name of the architect writer doc
if not IsNull(oArchForm.Component) then
oArchForm = oArchForm.Component.DrawPage.Forms.getByName("architect") Use the name from the form navigator
msgbox "About to close architect form"
oArchForm.parent.parent.CurrentController.Frame.close( true ) Rem... this way so dosen't close wrong form because of timing.
Else
msgbox "Architect writer doc form isNull"
End if
Else
msgbox "Can not find the architect writer doc using that name"
End if
End sub |
Cheers
Voo |
|
| Back to top |
|
 |
ch_ta General User

Joined: 05 Jan 2009 Posts: 8
|
Posted: Thu Jan 08, 2009 2:23 pm Post subject: |
|
|
Voo, the code executes! However the listbox does not refresh. I replaced "project_Writerdoc_Name" with "projects" and "architect_Writerdoc_Name" with "architect". I get the following messages:
"About to refresh listbox"
"About to close architect form"
But strangely after entering a new record, and saving it in the architect form, executing the macro via the exit button does not refresh the listbox in the projects form. Is there an issue with the call involving the refresh method? |
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 400 Location: Australia
|
Posted: Thu Jan 08, 2009 11:21 pm Post subject: |
|
|
Hi again,
That's good that those message box's displayed. It shows that the code got to the right places. I'm not sure why your listbox did not reset though.
In my experiences the refresh method should reload the list. If the name of the listbox control was not "arch" then I would have expected an error to be thrown on that line. Below are a few more things to check...
1/ Double check that you are actually saving the changes back to the architect table. If you go to the tables section of the GUI and open the table directly, press the refresh button up the top on the toolbar and see if your latest entry has actually made it back to the table.
2/ In design mode, check it is in fact a listbox and not a combobox (although I would expect an error when trying to use refresh() with a combobox, as it does not have that method).
3/ Is the listbox a "stand alone" listbox or is it a listbox that has been created inside a "table grid" control.
4/ When you created the listbox, did you have the "wizard" button, which is on the "form control" toolbar, turned on? It is highly advised to have this button pressed in as it steps you through the settings (at least when creating a stand-alone listbox, not with table grid listbox).
Cheers
Voo |
|
| Back to top |
|
 |
ch_ta General User

Joined: 05 Jan 2009 Posts: 8
|
Posted: Sun Jan 11, 2009 3:13 pm Post subject: |
|
|
Voo, thanks a lot for your help! I've been scouring the API documentation and using Xray to understand the code. I've been able to recreate the functions from your code, fix the bug and created a more generalized form of what I wanted to achieve.
Here it is:
| Code: |
REM Subroutine "exit_reload" is attached to an exit button
REM and its function is to exit the current form and reload
REM a listbox in the form that initiated the recently opened
REM form.
Sub exit_reload ()
Dim dbForms as Object
Dim formMDoc as Object
Dim formMDocName as String
Dim formSDoc as Object
Dim formSDocName as String
Dim formM as Object
Dim formMName as String
Dim formS as Object
Dim formSName as String
Dim lbCtrl as Object
Dim lbCtrlName as String
'Retrieve the form documents within the database
dbForms = thisComponent.Parent.getFormDocuments()
'Retrieve the "main" form document
formMDocName = "projects"
if dbForms.hasByName(formMDocName) then
formMDoc = dbForms.getByName(formMDocName)
else
MsgBox "Nonexistent form document: " & formMDocName
end if
'Retrieve the "sub" form document; form document initiated by "main"
formSDocName = "architect"
if dbForms.hasByName(formSDocName) then
formSDoc = dbForms.getByName(formSDocName)
else
MsgBox "Nonexistent form document: " & formSDocName
end if
'Retrieve form within "main" form document
formMName = "projects"
formM = formMDoc.Component.DrawPage.Forms
if formM.hasByName(formMName) then
formM = formM.getByName(formMName)
else
MsgBox "Nonexistent form: " & formMName
end if
'Retrieve form within "sub" form document
formSName = "architect"
formS = formSDoc.Component.DrawPage.Forms
if formS.hasByName(formSName) then
formS = formS.getByName(formSName)
else
MsgBox "Nonexistent form: " & formSName
end if
'Obtain listbox control from the "main" form
lbCtrlName = "arch"
if formM.hasByName(lbCtrlName) then
lbCtrl = formM.getByName(lbCtrlName)
else
MsgBox "Nonexistent listbox: " & lbCtrlName
end if
'Refresh the listbox
lbCtrl.refresh()
'Close "sub" form document
formS.Parent.Parent.CurrentController.Frame.close( TRUE )
End Sub
|
|
|
| Back to top |
|
 |
Voobase OOo Advocate


Joined: 21 Nov 2007 Posts: 400 Location: Australia
|
Posted: Mon Jan 12, 2009 2:48 am Post subject: |
|
|
| Quote: | | Voo, thanks a lot for your help! I've been scouring the API documentation and using Xray to understand the code. I've been able to recreate the functions from your code, fix the bug and created a more generalized form of what I wanted to achieve. |
No worries. What you have written looks pretty good. It's good you have discovered Xray. It is a good thing for checking the properties and methods of the controls/forms and helps with the learning.
Good luck with your project.
Cheers
Voo |
|
| Back to top |
|
 |
|