Refresh Listbox in another form

Discuss the database features
Post Reply
firelex
Posts: 7
Joined: Thu Feb 21, 2008 12:02 am

Refresh Listbox in another form

Post by firelex »

Hi, all!

I have two forms:
1) Form "Books names" basing on a table tblBOOKS having the names of books
2) General form showing some other data AND having a listbox with all books from the table tblBOOKS.

The idea is:
First opens the second form. Here the user can, by pressing a button, open the first one and add/remove/edit books names.
Then after closing this form (with books names) the listbox (having all the books names) in the second form should be updated.

A Listbox can be updated by calling its Refresh method.
BUT how can I call this method from another from? :shock:
I can call this method from the general form but how can I get to know that something has changed in the table tblBOOKS?
firelex
Posts: 7
Joined: Thu Feb 21, 2008 12:02 am

Re: Refresh Listbox in another form

Post by firelex »

I solved the problem in this way:
In the first form I placed a macro which is fired after Data is updated

Code: Select all

	Dim tmp 	as Object
			
	tmp = thisComponent.Parent.FormDocuments
	if tmp.HasByName("frmMyForm") then		
		tmp = tmp.getByName("frmMyForm")
		if not IsNull(tmp.Component) then
			tmp = tmp.Component.DrawPage.Forms.getByName("frmMyForm")
			if tmp.HasByName("lbxAb") then		
				tmp = tmp.getByName("lbxAb").refresh
			endif
		endif
	endif
wurzel
Volunteer
Posts: 145
Joined: Sun Dec 09, 2007 10:39 am

Re: Refresh Listbox in another form

Post by wurzel »

Hi,

Couldn't you just use "Form.reload" in your main form ? That then updates all of the controls on the main form. If the listbox control on your main form defines its data set as a SQL query from your tblBooks, it should automatically update the available data in the list.

Alex
firelex
Posts: 7
Joined: Thu Feb 21, 2008 12:02 am

Re: Refresh Listbox in another form

Post by firelex »

the thing is: main form doesn't know that it should be updated.
User avatar
kabing
Volunteer
Posts: 678
Joined: Fri Nov 23, 2007 12:05 am
Location: Midwest, USA

Re: Refresh Listbox in another form

Post by kabing »

I've been needing to ask the same type of question about a ComboBox on one form that needs to be updated after new data is entered in another form. But I'm not sure I understand either solution given here enough to impliment them.

wurzel:

How, exactly, would I use "form.reload" on my main form? Does it nead to be part or a larger macro? Where would I attach it to?

firelex:

Where, exactly, did you assign the macro?

is "frmMyForm" the name of the form based on tblBOOKS, or the "General form" with the listbox?

Is "lbxAb" the listbox on the "General" form, or something on the form based just on tblBOOKS?

I can paste a macro into the macro organizer, and change out things like form names, but beyond that I'm pretty clueless when it comes to macros.

thanks,

kabing
NeoOffice (app store version) OpenOffice.org 4.1 and LibreOffice 4.3 on Mac OS X El Capitan
OpenOffice.org 4.1.2 on Windows 10 (Previously on Vista)
firelex
Posts: 7
Joined: Thu Feb 21, 2008 12:02 am

Re: Refresh Listbox in another form

Post by firelex »

I have two forms:
1) Form "Books names" (frmBooksNames) basing on a table tblBOOKS having the names of books
2) General form (frmMyForm) showing some other data AND having a listbox (lbxAb) with all books from the table tblBOOKS.

This macro

Code: Select all

Dim tmp    as Object
         
   tmp = thisComponent.Parent.FormDocuments
   if tmp.HasByName("frmMyForm") then      
      tmp = tmp.getByName("frmMyForm")
      if not IsNull(tmp.Component) then
         tmp = tmp.Component.DrawPage.Forms.getByName("frmMyForm")
         if tmp.HasByName("lbxAb") then      
            tmp.getByName("lbxAb").refresh
         endif
      endif
   endif
can be assigned eigther to the event "Changing record" or to "After update" in the Form frmBooksNames

And besides wurzel is right you can use

Code: Select all

Dim tmp    as Object
         
   tmp = thisComponent.Parent.FormDocuments
   if tmp.HasByName("frmMyForm") then      
      tmp = tmp.getByName("frmMyForm")
      if not IsNull(tmp.Component) then
         tmp.Component.DrawPage.Forms.getByName("frmMyForm").Reload
      endif
   endif
instead. It refreshes the whole form, not only one listbox. But it depends on what you want to do. In my case frmMyForm.Reload resulted in some strange effect: my listbox lbxAb got empty.

And one more thing: the way I'm getting the frmMyForm in the macro is pretty ugly. I've found it just going through th object tree. I'm shure there is a better way. If you find it - post it here.
User avatar
kabing
Volunteer
Posts: 678
Joined: Fri Nov 23, 2007 12:05 am
Location: Midwest, USA

Re: Refresh Listbox in another form

Post by kabing »

Thanks so much! I think for my purposes your method will work best. It will be a couple of days before I can try it out, but I'll try to remember to report back.

I'm using a button with the following set of macros to open the second form:

Code: Select all

REM Generic macros needed to open any form
function OpenForm( formContainer as variant, oConnection as variant, sFormName as string) as variant
Dim aProp(1) As New com.sun.star.beans.PropertyValue
aProp(0).Name = "ActiveConnection"
aProp(0).Value = oConnection
aProp(1).Name = "OpenMode"
aProp(1).Value = "open"
OpenForm = formContainer.loadComponentFromURL(sFormName,"_blank",0,aProp())
end function

function getFormsTC() as variant
getFormsTC = thisComponent.Parent.getFormDocuments
end function

function getConnectionTC() as variant
getConnectionTC = thisComponent.Drawpage.Forms(0).ActiveConnection
end function


REM Macro to open specific form called 'Locations Data Entry'. One is needed for each form you wish to open from another form.

sub OpenForm_Locations_Data_Entry( oev as variant )
sFormName = "Locations Data Entry"
OpenForm( getFormsTC, getConnectionTC, sFormName )
end sub
That I found in this thread on oooforum.

kabing
NeoOffice (app store version) OpenOffice.org 4.1 and LibreOffice 4.3 on Mac OS X El Capitan
OpenOffice.org 4.1.2 on Windows 10 (Previously on Vista)
User avatar
kabing
Volunteer
Posts: 678
Joined: Fri Nov 23, 2007 12:05 am
Location: Midwest, USA

Re: Refresh Listbox in another form

Post by kabing »

It took me far longer than I had expected to get around to testing it out. It works o.k., although there are some limitations in my case.

I have a Field Trips Data Entry form that contains a Location combo box control. This combo box uses the LocationName field of the Locations table to populate its list. There is also a button on the Field Trips Data Entry form that opens the Locations Data Entry form (used to enter new locations).

I have assigned the macro specified by firelex to the After Record Change action.

But it only works if I actually type the new location name in the Location combo box before pressing the button. If I don't enter a Location name in the Field Trips Data Entry form before opening the Locations Data Entry Form, I get an error on returning to the Field Trips Data Entry form, saying that I the "Location" field must have an entry. And the combo box has not updated. I think this is because the Location field is set for entry required, and on refreshing the form, Base discovers that I haven't entered a value there.

As long as I type in a location name first, before hitting the button, though, it works o.k.

kabing
NeoOffice (app store version) OpenOffice.org 4.1 and LibreOffice 4.3 on Mac OS X El Capitan
OpenOffice.org 4.1.2 on Windows 10 (Previously on Vista)
Mellis
Posts: 2
Joined: Sat May 10, 2008 8:47 pm

Re: Refresh Listbox in another form

Post by Mellis »

Hello,

I'm having a little trouble with the refreshing of my listbox.
I'll try to explain the situation. It is very similar to the one explained above.

I have a table 1 with an "id" column and a "value" column that can be modified with form 1
I have a second table with a foreign key column where i save the id of the first table. For this I use a form 2 with a listbox that uses table1 as source.
I use the script i found here to open form 1 from within form 2. (It works)
However when I change a record from table 1 with form 1 and close it again, the listbox in form 2 is not refreshed (using the code i found here also). (the listbox is named "lbx")

What have I tried to solve my problem:

add a button to form 2 to refresh the listbox with the following code attached to "mouse button pressed" (to see if the refresh command even works at all)

Code: Select all

Sub Refresh_lbx ( oEv as object )

	oEv.Source.model.parent.getByName("lbx").Refresh()
	
End Sub
This does not work on openoffice 2.4 on my vista machine nor on xp (not with mysql dbase, nor with the native hsql dbase)
This does work on openoffice 2.3.1 on my xp machine (haven't tested on vista yet)

My conclusion is that refreshing a listbox, the way it is suggested here, does not work in version 2.4 (on windows)
Does anyone have a clue on why this happens (or in this case, doesn't happen) (I have been searching for some time now, really, but I haven't got much experience in base)

Furthermore, the following code (to update the listbox from within another form), does not work for me at all (and i have tried it with al the above settings):

Code: Select all

Sub Refresh_frm_2_lbx
Dim tmp as Object
	tmp = thisComponent.Parent.FormDocuments
	if tmp.HasByName("frm_leverancier") then
		tmp = tmp.getByName ("frm_2")
	rem	xray tmp
		if not IsNull (tmp.Component) then
		xray tmp.Component.DrawPage.Forms
		    tmp = tmp.Component.DrawPage.Forms.getByName("frm_2")
			if tmp.HasByName("lbx") then
			msgbox "tmp.hasbynamelbx"
			tmp.getByName("lbx").Refresh()
			endif
		endif
	endif
End Sub
When I xray thisComponent.Parent.FormDocuments.getByName("frm_2") and double click on elementnames it only displays "Standard" instead of all my controls (including "lbx")
(when I xray oEv.Source.model.parent from the first blok of code and look at the available elementnames I do get "lbx" as an option - which is why that block of code does works in 2.3.1)

i have noticed that with the first xray the internal name of the displayed object is "com.sun.star.comp.dba.ODocumentDefinition"
with the other xray it's "com.sun.star.comp.forms.ODatabaseForm"
I don't know if this has anything to do with it.

any thoughts or help would be great.
Thanks in advance

(and btw sorry for the crappy english, I'm not a native speaker, I can only try my best)
Mellis
Posts: 2
Joined: Sat May 10, 2008 8:47 pm

Re: Refresh Listbox in another form

Post by Mellis »

Doesn't anybody have any idea?
pandrask
Posts: 1
Joined: Sun Oct 12, 2008 2:37 pm

Re: Refresh Listbox in another form

Post by pandrask »

Hi,
I'm completely new to OOo Base and to Java programming (I have some scripting experience with php mysql and very little with javascript). I tried the following code suggested here in this forum:

Code: Select all

Dim tmp    as Object
         
   tmp = thisComponent.Parent.FormDocuments
   if tmp.HasByName("frmMyForm") then     
      tmp = tmp.getByName("frmMyForm")
      if not IsNull(tmp.Component) then
         tmp = tmp.Component.DrawPage.Forms.getByName("frmMyForm")
         if tmp.HasByName("lbxAb") then     
            tmp.getByName("lbxAb").refresh
         endif
      endif
   endif
It does nothing and if I try it without the if conditions the following error appears: NoSuchElementException Message: frmMyForm. The names of the form and the list box correspond to those in the script.

How can I make it work? Thank you for helping.
OOo 2.4.X on Ms Windows XP
ianonchwan
Posts: 1
Joined: Sat Nov 29, 2008 1:27 am

Re: Refresh Listbox in another form

Post by ianonchwan »

Hi, did you ever find a solution. I'm a real novice with openoffice and struggling with the same problem. I have tried the macro to refresh a specific listbox but with no success. Very frustrating

Ian
OOo 3.0.X on MS Windows Vista
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: Refresh Listbox in another form

Post by voobase »

Just make sure you use it with a "listbox" only. The same code will not work with a "combobox" as that does not have the refresh method. You need to reload the whole form for a combobox.
@pandrask
It does nothing
Make sure you are paying good attention to your character case for the form name.
You may also need to post more of your code as it is hard to see where you might be going wrong with that small excerpt.

Here is some more reading on a similar problem but with a combobox...
http://user.services.openoffice.org/en/ ... =39&t=5877

EDIT: What I said above is no longer true. As of version 3.1 of Open Office there is now a refresh method for the Combobox!! :super: This is great news! Use it just like you would for a Listbox.
OOo 2.3.X on MS Windows Vista
Post Reply