Page 1 of 1

[Solved] Get all rows from subform grid

Posted: Tue Jan 31, 2017 10:15 pm
by Foryster
Hi All,

I'm fighting against macros in openoffice base, basic. I have a grid on the form. I want to iterate through all rows of that grid.

Currently I have

Code: Select all

subForm = mainForm.getByName("SubForm")
oGrid = subForm.getByName("SubForm_Grid")
allRows = oGrid.getRowSet()

FOR I = 1 to allRows.Count      
      print allRows.Columns.getByIndex(0).getString()
   NEXT I
However the RowSet has always only one Row, the selected one. Is there any other method I should use? Any suggestions?

Re: Get all rows from subform grid

Posted: Tue Jan 31, 2017 11:32 pm
by Villeroy
Obviously you have a problem with databases and forms. You must not write any macros before you fully understand the appication you are hacking. Have you ever created a working database application without macros? You would be surprised about the less frustrating options you have at hand with a little bit of SQL and normalization logic.

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 8:39 am
by Foryster
This is my application I'm "hacking". I have a normalised DB with all the keys and relations. And I'm fully understanding the logic of the app I'm writing. I just need to know if there is any method to get rows from grid one by one?

Simple as it is.

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 6:18 pm
by UnklDonald418
The rowset service has a number of navigation methods including next() and previous() that allow you to
to get rows from grid one by one

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 7:22 pm
by Foryster
Thx for suggerstion but it doesn't work. The rowset.Count is 1 and rowset contains only one row that was selected. The table has more rows.

So If I use next() or previous() in a loop, I enter the loop only once.

It seems that instruction below returns only the selected row, not all of them.

Code: Select all

allRows = oGrid.getRowSet() 

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 8:17 pm
by UnklDonald418
Not exactly the same as what you are attempting, but here is a link to an example of manipulating data on a grid using a rowset.
viewtopic.php?f=21&t=83531

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 8:25 pm
by F3K Total
The rowset lies on the subform, not in the Grid, which only dispays it.
This code works with attached example:

Code: Select all

Sub iterate_through_all_rows
    oMainForm = ThisComponent.Drawpage.forms.getbyName("MainForm")
    oSubForm = oMainForm.getbyName("SubForm")
    oResult = oSubForm.createResultSet
    oResult.beforefirst
    count = 0
    while oResult.next
        count = count + 1
        sSN = oResult.Columns.getbyName("SN").getstring
        sFN = oResult.Columns.getbyName("FN").getstring
        print "No. " & count & ": " & sSN & ", "& sFN
    wend
End Sub

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 8:46 pm
by Arineckaig

Code: Select all

 allRows = oGrid.getRowSet() 

The RowSet supplied by a Table/Grid form control tends to refer to the columns in the grid as opposed to the rows. The latter comprise the RowSet of the (data)form that contains the Grid form control: thus should be obtained from the form.

Re: Get all rows from subform grid

Posted: Wed Feb 01, 2017 10:02 pm
by Villeroy
A grid has no row set. A grid is just a bundle of other controls (list boxes, date controls, text boxes etc) showing more than one record at once. There is no "Data" tab in the properties window of a grid. The record set belongs to the underlying logical form which might be oGrid.getParent() [not sure]

No macro programming without MRI or XRay
[Tutorial] Introduction into object inspection with MRI

Re: Get all rows from subform grid

Posted: Thu Feb 02, 2017 1:18 pm
by Arineckaig
A grid has no row set.
This is indeed true and I should apologise for lack of precision. At risk of further confusion I should explain that my comment reflected the [MRI] finding that the object "com.sun.star.comp.forms.OGridControlModel" does somewhat surprisingly have a "RowSet" property so that no error is necessarily raised by:

Code: Select all

allRows = oGrid.getRowSet()
The ElementNames property of the "OGridControlModel" object comprise the names of the columns of the Grid form control. Calling its "RowSet" property however supplies the "com.sun.star.comp.forms.ODatabaseForm" object: i.e., the container of the Grid form control. While the ElementNames and the Count properties of this "ODatabaseForm" object refer to the various Controls that it contains, it does have a RowCount property and the customary .row methods. Thus the RowSet property of the Grid does appear in effect to give access to its containing (data)form's row set.

I would endorse however Villeroy suggestion: the RowSet can be reached just as easily, and indeed more logically, through the Parent property of the Grid form control.

Re: Get all rows from subform grid

Posted: Thu Feb 02, 2017 4:58 pm
by Villeroy
Arineckaig wrote:[MRI] finding that the object "com.sun.star.comp.forms.OGridControlModel" does somewhat surprisingly have a "RowSet" property so that no error is necessarily raised by:
:shock:
Very confusing indeed.

Re: Get all rows from subform grid

Posted: Thu Feb 02, 2017 5:28 pm
by Arineckaig
I have always understood the Grid form control to be a later addition to Base and in several respects, as both a form control and a container of form controls, tends to be a 'law unto itself'.

Re: Get all rows from subform grid

Posted: Thu Feb 02, 2017 8:49 pm
by Foryster
F3K Total wrote:The rowset lies on the subform, not in the Grid, which only dispays it.
This code works with attached example:

Code: Select all

Sub iterate_through_all_rows
    oMainForm = ThisComponent.Drawpage.forms.getbyName("MainForm")
    oSubForm = oMainForm.getbyName("SubForm")
    oResult = oSubForm.createResultSet
    oResult.beforefirst
    count = 0
    while oResult.next
        count = count + 1
        sSN = oResult.Columns.getbyName("SN").getstring
        sFN = oResult.Columns.getbyName("FN").getstring
        print "No. " & count & ": " & sSN & ", "& sFN
    wend
End Sub
This is exactly what I was looking for. Thanks a lot! It works :)