Select a field in a table

I have created a form (NEW_form) displaying all data as a table, There are only two fields in the underlying table, i.e. AutoIncrement field and a Data field, and only one field (DataField) is on the form. The form is called from a master form, however, it can be accessed as a standalone. Even in standalone mode I cannot convince Base to select the DataField without clicking on said field even though it is the only field on the form. If I just start typing, it does not appear anywhere on said form until I click on the field and start typing. What can I do to convince the macro to select the Data Field? I’ve tried so many methods today, I can’t recall them all.


Sub OpenFrmNEW_FORM
	CloseMaster  ' - this is a subroutine that closes the MasterForm
	mForm =	ThisDatabaseDocument.FormDocuments.getByName("NEW_FORM")    ' Main Form
	mForm.Open
' All works well to this point but I cannot get the column DATA_FIELD TO BE SELECTED AUTOMATICALLY. 

    mForm.findColumn("DATA_FIELD")
    
End Sub

Don’t know why you need a macro here, but:
With inField = mForm.findColumn("DATA_Field") you will get the position of the field in the table. There are two fields in your table. Might be the position is ‘2’, because the primary key is ‘1’…
With mForm.getString(inField) you will get the content of this field.
Direct way will be: mForm.getString(mForm.findColumn("DATA_Field"))
or, of it is position 2 in the table: mForm.getString(2)
If you only need the content as a string, not for calculating with it, this will work.

Hello,
i think LSemmens tries here to find a column on the Form Component (…FormDocument), but not on the structural form, placed on the drawpage of that Component.
So if the strucural form was named MainForm, the Code would be e.g.:

Sub open_Form_set_Focus
    oDBController = ThisDatabasedocument.CurrentController
    if not oDBController.isConnected then  oDBController.Connect
    oFormdocument = ThisDatabasedocument.Formdocuments.getbyName("NEW_FORM").open
    oForm = oFormdocument.Drawpage.Forms.getbyName("MainForm")
    oDATA_FIELD = oForm.getbyName("DATA_FIELD")
    oController = oFormdocument.CurrentController
    oDATA_FIELDControl = oController.getControl(oDATA_FIELD)
    oDATA_FIELDControl.setFocus
end sub

but, just to select one control on opening a form, you just need to adjust the tabstop on controls properties to Yes and the tab order to 1 .

Then switch on the Automatic Control Focus
CF1

1 Like

Thank you, the setFocus event was what I was looking for!

If so, please mark my post as solution of your problem.

Thank you for your input. For some reason I still can’t get this to work. I even tried setting up a single table database with only one form and every time it hits the
oDATA_FIELDControl = oController.getControl(oDATA_FIELD)
line it throws an error

“Object Variable not set”

I have declared all variables at the start of the macro as I always run OPTION EXPLICIT as a habit
oDATA_FIELD contains a variant/long of “2”. oController is set as SwTextView
Here is my test database with the macro. (Normally called by a button, but for testing I am just running the macro direct.)
TEST.odb (12.8 KB)

@LSemmens
The code has an error in that you need to get the table control and not the data position. That position is not useful in your example since you do not have both table columns displayed.
.
However, instead of the code after the open statement, @F3KTotal gave you the other option which works on your sample.

Edit:
For completeness, here is modified code from your sample:

Sub open_FormAndset_Focus
Dim oDATA_FIELD, oForm, oDBController, oDATA_FIELDControl
    oDBController = ThisDatabasedocument.CurrentController
    if not oDBController.isConnected then  oDBController.Connect
    oFormdocument = ThisDatabasedocument.Formdocuments.getbyName("form1").open
    oForm = oFormdocument.Drawpage.Forms.getbyName("MainForm")
    oController = oFormdocument.CurrentController
    oDATA_FIELDControl = oController.getControl(oForm.getByName("MainForm_Grid"))
Rem Can use with multiple columns in table control to set which column has focus
Rem    oDATA_FIELDControl.setCurrentColumnPosition(column_number_here)
    oDATA_FIELDControl.setFocus()
end sub
1 Like

Thank you both F3KTotal and Ratslinger both of you provided invaluable assistance. Whilst you concept might work on a simple database, RobertG, unfortunately, mine is not so simple. Multiple linked tables, Forms and Subforms. I broke the database down to a simple question that I can then expand into a more complex answer. Thank you all, again for your invaluable support!