How to address a field in a row in a table on a form

I have a main form, and linked to that a subform which displays several rows of a table. I have a handle on the main form (“oForm”), from which I can get a handle on the subform and (I assume) on the table:

oSub = oForm."RefForm"
oTable = oSub."PubTable"

I know that the first works because I can get oSub.RowCount to return the number of entries in the subform table. How can I address (in a macro) a specific row in that table, and a specific field in that row (to obtain or insert a value)?

LATER: Apologies for obscurity. Entryform shown below:

This is recording information about pottery bearing makers’ marks, derived from published reports. The form shows a single entry for a single signature (‘Stamp’) and subforms show data about the vessel on which it is found etc. In the bottom RH corner is a subform showing a table grid. There is occasionally more than one publication which describes the same object. I use defaults on a second page of the form to supply repeated values - there may for instance be many entries from the same find spot in the same publication. I therefore want, when the cursor moves to the ‘Publication Code’ field, to force the insertion of the default publication code. I have figured out how to do this for the other controls, but not for the table grid. See further explanation in the comments below.

Please delete the duplicate of this question (https://ask.libreoffice.org/en/question/246472/how-to-address-a-field-in-a-row-in-a-table-on-a-form/). Thanks in advance …

@PhilipK,

Not quite certain what is wanted here. In a Table grid control (guessing this is what is being referred to) you can see multiple table items but usually only one is selected and accessible through code at a time. You are not selecting a row but rather a record. Fields are easy to get to once you have the record wanted.

So the question is, what are you trying to do with the table grid? Find a record? Use the selected record? Something else?

Sorry, I was trying to be too concise! Apologies also for use of language (‘row’ rather than ‘record’ - SQL-speak). I have a form for data entry (previously illustrated under another question) with a table grid for (potentially) multiple sources of reference for a single item of data on the main form. Because the data entries typically share common values in many fields (find spot, museum location etc.), these are stored in a subform where they serve as defaults (which can be changed on demand). So in the process of data entry, when a new record is sensed, the default values are retrieved and inserted in the relevant fields. Correspondingly, publication references need to be entered in the table grid, so I want a macro (presumably ‘receiving focus’) to sense arrival on a new (blank) record and to fill it with a supplied default value. In this particular instance (where there may be two references to insert), I want also to detect whether the new record is the only one or the second.

@PhilipK,

Sorry but I do not understand. This last comment is more confusing and I can’t match anything to any sample you have previously provided.

Original question now elaborated with image.

Hello,

Unfortunately the more information you have given, the more questions I have. Will not elaborate as this is not my project. Will attempt to give you adequate information to hopefully do what you want.

In a table grid control you can point to a new record with a macro such as:

Option Explicit
Sub GoToNew
  Dim oDrawPage As Variant
  Dim oForm As Variant
  Dim oSubForm As Variant
  Dim oObj1 As Variant
  oDrawPage = ThisComponent.getDrawPage()
  oForm = oDrawPage.getForms().getByName("MainForm")
  oSubForm = oForm.getByName("RefsForm")
  oSubForm.moveToInsertRow()
End Sub

When you want to access (read or write) the fields, the easiest method is through Columns:

oColumns = oSubForm.getColumns()
oCell = oColumns.getByName("COLUMN_NAME")

From that point use oCell to get or set value such as (as Integer):

iMyCellValue = oCell.getInt()

Some links with samples and/or code:

macro for highlighting sub-form row in Base

what code do I need in a Base macro to access a field in highlighted row in a datasheet

SOLVED: Read values from selected rows in a table control

MRI is extremely helpful in determining much information in this regard. This is in the documentation links given to you previously. Here is another link in answer → Update mri ‘Error reading from internet’

Edit:

Please note that COLUMN_NAME used in getting Columns is an actual table field name and not a grid column name.

Many thanks, Ratslinger, as ever. It all takes time and lots of experimentation. The project is very complex; it was fully implemented (by me) 25 years ago in Paradox, so I know that the logic is sound. It is just a matter of discovering how to replicate the functionality in LO. I am making more progress than I had expected.

For now, in order to be quite clear what is going on, I have put a pushbutton on the main form that attempts to access values in a table grid. The grid belongs to a subform (“RefsForm”) and is named “RefsTable”. The code on the pushbutton is as follows:

SUB Test
	Dim oForm, oSub, oTable, oCol as Object	
    oForm = ThisComponent.Drawpage.forms.getByName("MainForm")
	oSub = oForm."RefsForm"
	oTable = oSub."RefsTable"
	oCol = oTable.getByName("PubCode")
	msgBox "PubCode column reads: " & oCol.text
END SUB

It works, retrieving the value in the first column of the first row of the table. The line oCols = oTable.getColumns() generated an ‘unknown property or method’ error, and turned out not to be necessary. Incidentally, the getByName syntax seems not to be necessary.

The line oCols = oTable.getColumns() generated an ‘unknown property or method’ error

Yes because that is not the line of code given. What is needed is:

oCols = oSub.getColumns()

Columns in my answer is on the form and not on the control. Here it is referring to the columns in the table itself. Your code is trying to refer to it as a column in the grid. When using the code I presented, getByName is necessary. You have taken a different direction and in some ways a more complicated one.

I keep bringing this up but MRI is a major tool in researching all this.

Thank you. Endless learning process. Have two more queries today, but shall post them as new questions!