How to force navigation between controls on a form

I have a data entry form which incorporates fields from seven tables (six represented by subforms). I can only set a tab order within each individually, but wish to force the focus on leaving one control (in the master table/form) to move in different directions, depending on whether the value entered does or does not link to a primary key in another table. How, within a macro, can I force the focus to move to any particular control?

Hello,

Please see all answers/comments posted here → How to get focus on a subform in LibreOffice Base using tab?

Thank you. I now have this working code attached to the Key_Pressed event in a specific control.

function InvNo_to_PotterNo(oEvent) as boolean	'Force navigation'
    Dim oForm	as Object
    Dim NextField	as Object
Rem Capture Enter and Tab
    If oEvent.KeyCode = 1280 OR oEvent.KeyCode = 1282 Then 
    	oForm = ThisComponent.Drawpage.forms.getByName("MainForm")
		form_ctrlr = ThisComponent.getCurrentController() 
		NextField = oForm."fmtPotter No."
		form_ctrlr.getControl(NextField).setFocus()
	Endif
End function

It could probably be generalised with input parameters to serve for other destinations, but I shall work that out later. For now, while focus goes to the desired control, the cursor is placed at the beginning of the field, while I would like it to select the whole field. What do I need to add?

@PhilipK,

Please understand this is a question and answer site. Ask a question and get an answer to that question. Asking another question, as you have, in comments from an answer buries it from others possibly finding this answer. In future please ask a a new question.

Selection is through the controls’ View. Your code modified:

sub InvNo_to_PotterNo(oEvent) as boolean   'Force navigation'
    Dim oForm   as Object
    Dim CtlView   as Object
    Dim NextField   as Object
    Dim oSelection As New com.sun.star.awt.Selection
Rem Capture Enter and Tab
    If oEvent.KeyCode = 1280 OR oEvent.KeyCode = 1282 Then 
        oForm = ThisComponent.Drawpage.forms.getByName("MainForm")
        form_ctrlr = ThisComponent.getCurrentController() 
        NextField = oForm."fmtPotter No."
        CtlView = form_ctrlr.GetControl(NextField)
        form_ctrlr.getControl(NextField).setFocus()
        oSelection.Min = 0
        oSelection.Max = Len( NextField.Text )
        CtlView.Selection = oSelection
    Endif
End sub

Thank you. That does what is required. Apologies for supplementary question. Sometimes it is easier to pursue a conversation than to explain context afresh in a new question - but I do understand that topics/solutions need to be easy to find.

Supplementary question here: the code

form_ctrlr.getControl(NextField).setFocus()
oSelection.Min = 0
oSelection.Max = Len( NextField.Text )
CtlView.Selection = oSelection

does not work if the control is numeric (‘text’ property not recognised). How is the length (or ‘whole’) of a numerical value described in this context.

@PhilipK,

Please, again, no further supplementary questions. This is a new question and actually answered in comment here → How to reset fields on a form. NextField.Value is needed.

Please see this post also for some links to macro documents and object inspection → To learn LibreOffice Base are there introductions or tutorials?

Apologies: it tied in directly with what preceded. But thank you in particular for the pointer to that topic on documentation and tutorials, now bookmarked. Another very quick question about to be posted (separately!).