Table Controls, Reading and Writing to Columns via Macro

I wrote a very handy macro for Base that copies an array of fields from one form to another. It works no matter if the form is at the highest level, a sub form, or a sub-sub form, etc. However, it completely fails when I try to access forms represented by a table control (as opposed to separate fields). Some people call table controls “grid controls” where there are rows and columns of data representing your records.

Is there a special method or object I must use to access data displayed in a table control that differs from separate form fields?

As a quick example, normally to read the value from a field I do something like this:

thisComponent.drawpage.forms.getByName("form").getByName("subForm").getByName("subSubForm").getByName("fieldName").getCurrentValue()

And to write to the same field I would do something like:

thisComponent.drawpage.forms.getByName("form").getByName("subForm").getByName("subSubForm").getByName("fieldName").boundField.updateString("some value")

This always works for regular, separate fields. If, however, there is a table control (aka grid) as the final destination/source, nothing works. So I’m guessing there is a special object dealing with table controls. Do I need to add an extra step in the hierarchy navigation .getByName("tableControlName") ? I tried that, and it still didn’t work. Are .currentValue and .boundField.getString() (or .getXXX other type) not the right methods for table control columns? They work for regular fields every time.

I can’t find any documentation or questions answered here relating to table controls and how to read/write their column values within macros. I thought it was the same as with other fields, but I’m hitting a roadblock.

Trying to access a table column field as described above, I get the following error no matter what I try…
BASIC runtime error. An exception occurred. Type: com.sun.star.container.NoSuchElementException Message: . In fact, many times it completely crashes Base, and I have to recover my document.

I feel like there could be an error in my code or something. If so, then I’ll probably figure it out. If not, then who knows. I’ll keep hunting.

Hello,

When dealing with individual fields you are dealing with a single record. With a table control there are multiple records available, so first go to the wanted record:

  oDrawPage = ThisComponent.getDrawPage()
  oForms = oDrawPage.getForms()
  oObj1 = oForms.getByName("MainForm")
Rem Get record wanted  
  oObj2 = oObj1.absolute(3)

When you have the wanted record, the easiest way to insert data is using Columns. So if I want to place data in the field named COLOR2 then:

Rem Get column you are dealing with
  oColumns = oObj1.getColumns()
  oObj3 = oColumns.getByName("COLOR2")
Rem Update field with type data used - this is a varchar field  
  oObj3.updateString("Hello")

Then just insert or update row.

Edit:

Grid controls ARE different. See my post here → Looking to create grid (not table) control on Base form

Awesome. That sounds promising. Thanks, I’ll try it out.

Still can’t solve this nonsense, but I’m going to accept your answer anyway because it’s good for others to see. With all this weird behavior and confusing methods, I’m half wondering if I could hand-coded a web interface from scratch using web technologies in less time. boo. VB type languages are very, very odd.

There is a lot to understand when dealing LO and the Uno API. I realize my sample does not explain the details. This is where the docs are important in understanding a lot of the basics in controls & data manipulation. It is certain I failed to make clear that the field name used in the sample is that of the table field and not of the table control.

Yes, you can do a lot of things faster using something you already understand. Everything takes time to learn, even what you already know.

Agreed. I hoped Base would provide me a low-code interface that is easier to maintain than having to hand-code everything in PHP, HTML, Javascript & MySQL. Connecting controls to data is easy in Base, but much other functionality is lacking. Obviously macros can overcome the shortcomings, but to make it work requires quite advanced macro knowledge. I’m worried I’ll get it working, but later a LO update will break everything, and I won’t be able to maintain the code anymore :frowning:

I wish there was better documentation of every function in the LO Basic macro language. I have been scouring the web and the known resources and can’t find a thorough description of what methods and functions are available. The functions I want to use are barely mentioned if at all in the known resources, so I can’t get a handle on how they work. Sample code for every function is a must! The PHP website is AMAZING for this. Too bad LO doesn’t have such a thing (if they do, I don’t know where).

Here is probably another source which you will cringe at → Apache OpenOffice Developer’s Guide. It contains a lot of the ifs, ands and wherebys. It saved me many a time. Like the others, a LOT of delving into to get anywhere.

Hey, anything is better than nothing. I’m bookmarking every single thing you guys point at in case there are nuggets for me to discover. I much appreciate it. And even though my responses sound a little frustrated, I’m not giving up yet. I’m pretty determined to get this thing working.