Setting focus to a field when opening a form

I have created forms (a few years ago) in Base and copied these as Standalone forms. But now the user noticed that all of the forms open with the cursor at a character upper leftmost corner. Obvious question: cann’t the cursor be positioned at a given field.
Answer seems to be: yes with a macro.
I’ve been running previous questions up and down to get the right commands and syntax and clobbered two little macro’s together (I obviously need only one) that solves the issue only partly. Solved in the sense that the cursor is not any more at that character and in a data field, but not solved in the way that no matter what, the cursor appears in the FIRST datafield, not the one adressed in the macro.
I found someone in another forum reporting the same issue, no answer given.
The macro I last made (from one of the previous questions here):

Sub focus2

Dim oForm As Object
Dim oField, CtlView As object
'Get Form'
    oForm = ThisComponent.Drawpage.Forms.getByName("MainlidForm")
'Get Field'
    oField = oForm.getByName("txt1Naam")
'Get field VIEW'
    CtlView = ThisComponent.getCurrentController().getControl(oField)
'Set Focus'
	CtlView.setFocus()

End Sub

txt1Naam being the second data field on the form.

Doesn’t work Forms → Automatic Control Focus? Open the form, switch to edit form, open form navigator and set this option in “forms”.

Have just tested with LO 24.8.0.3 - will set the cursor into first field for entries instead of left upper corner.

Yes, that happens. But I want the focus on the second field. And selecting this field and set this option, makes no difference, the focus keeps coming on the first field.

If you want the second field the value for tabstop should be set. Automatic Control Focus will take the field with first tabstop.

Setting focus by macro should work also the way you have written down in your original post.

Yes, it SHOULD work but it doesn’t. It’s similar to the autofocus issue, the focus goes to the first field.

Which is the event you connected the macro to?
Set “Automatic Control Focus” off when using the macro.

But: I would prefer not to use a macro, set the right tabstops and the control focus.

The following code gets the required objects from the calling event and the name of the control to be focussed from a hidden control named “setFocus”.
Copy the code to somewhere (“MyMacros” or document does not matter).
Call the form navigator and add a hidden control named “setFocus” to the (sub-)form in question where you add the name of the control to be focussed as value.
Get the properties of the form having the hidden control and assign Sub form_Loading to the form’s loading and reloading events.

REM  *****  BASIC  *****

Sub form_Loading(ev)
frm = ev.Source
if not frm.hasByName("setFocus") then exit sub
sVal = frm.getByName("setFocus").HiddenValue
REM mri oFormController
focusControl(frm, sVal)
End Sub

Sub focusControl(oForm, sName)
doc = getParentObject(oForm,  "com.sun.star.document.OfficeDocument")
view = doc.getCurrentController()
oFormController = view.getFormController(oForm)
oControls = oFormController.getControls()
for i = 0 to uBound(oControls)
	box = oControls(i)
	if box.Model.getName() = sName then
		box.setFocus()
	next
next
End Sub

Function getParentObject(byval m, srvName$)
on error goto NullObj
	do until m.supportsService(srvName)
		m = m.getParent()
	loop
	getParentObject = m
	exit function
	NullObj:
	getParentObject = Null
End Function

Edit:
biblio_form.odt (51.9 KB) sample document puttin the focus on the “Title” control.

P.S. why do I do that like this?

  1. The macro should focus a control on any form or subform embedded in any kind of office document being embedded or stand-alone.
  2. Under certain conditions, ThisComponent may be a database document or any type of embedding office document being embedded in the database or not. Function getParentObject makes it possible to get the embedding form document from the calling form wherever that is in the existing hierarchy of forms.
  3. HiddenControls are made for macro configuration, so you can use the exact same code for the same task. In my sample document I set setFocus=Title. The macro finds the control “setFocus” (exiting silently otherwise) and reads the name from that control. The exact same code can focus any control on any of your forms without changing any constant names in the code.
  4. This code translates easily into other macro languages.

If you don’t like it that way:

REM Writer document only:
oForm = ThisComponent.DrawPage.Forms.getByName("Form Name")
REM call my routine with hard coded name:
focusControl(oForm, "Control Name")

Same thing in Python, translated in a Python editor from pasted Basic code:

def form_Loading(ev):
    frm = ev.Source
    if not frm.hasByName("setFocus"):return
    sVal = frm.getByName("setFocus").HiddenValue
    focusControl(frm, sVal)

def focusControl(oForm, sName):
    doc = getParentObject(oForm,  "com.sun.star.document.OfficeDocument")
    view = doc.getCurrentController()
    oFormController = view.getFormController(oForm)
    oControls = oFormController.getControls()
    for i in range(len(oControls)):
        box = oControls[i]
        if box.Model.getName() == sName:
            box.setFocus()
            return

def getParentObject(m, srvName):
    while not m.supportsService(srvName):
        try:
            m = m.getParent()
        except:
    	    return None
    return m

The macro is called from the Form-event When loading or something alike, since I’m running in Dutch.

Use the events of the logical form rather than the document’s events.

To Robert and Villeroy:
The easiest/quickest solution seems indeed to set the tab order for the field.
But I stored the biblio suggestion from Villeroy for reference, it might come in handy later on.
Tx both of you, this issue is solved for me.
Unless you agree that the macor I suggested or the setting of Autofocus should work as Robert suggested. In that case, I’m willing to log a bug.