SetFocus from Unbound Subform to Main Form (1 Viewer)

DBUserNew

New member
Local time
Today, 11:11
Joined
Sep 12, 2023
Messages
22
Greetings All,

I have a main form (frmTrk) and there is a combo box (cboTrk) along with other controls in this form.
frmTrk has an UNBOUND sub form (subfrmTrk) as continous form, which shows record from a query. On this continous form has an unbound text box shows "Click & Schedule" which opens a form related to the PK on this sub form. Everything works here.

The problem is, when user click on "Click & Schedule", VBA has an IF statement that check whether 'cboTrk' has a value or not. If no value has selected, then SetFocus to this combo box (cboTrk).

I have tried different variance of SetFocus method, but none of them works.

Forms!frmTrk.SetFocus
Forms!frmTrk.cboTrk.SetFocus

No results. Access doesn't show any errors, the control is not setfocus.

I have just tried: Forms!frmTrk.cboTrk.SetFocus - same result, no focus.

I don't know what to do? Is that because both are not parent/child forms?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:41
Joined
Aug 30, 2003
Messages
36,125
What event is it in? The click event of a button? I have code that lets users jump out of a subform to a control on the main form by hitting the tab key, and the code is just:

Forms!frmCharterBus.txtCharges.SetFocus

The only time I've seen focus not working is if you're in an event where it's still processing focus.
 

DBUserNew

New member
Local time
Today, 11:11
Joined
Sep 12, 2023
Messages
22
Thank you Paul. It is on click event. The actual code is ,

Code:
If IsNull(Forms!frmhome!txtID) Then
    MsgBox "Please select truck from the list", vbExclamation
    Forms!frmTrk.SetFocus
    Forms!frmTrk.cboTrk.SetFocus
    Exit Sub
End If

The msgbox display correctly, when I click OK in the msgbox, it doesn't SetFocus to the main form.

Edit: There is no other event code in this subform, unless the KeyDown to deactivate ESC key.

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyEscape Then
        KeyCode = 0
    End If
End Sub
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:41
Joined
Aug 30, 2003
Messages
36,125
Can you attach the db here, or a representative sample?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:41
Joined
Feb 19, 2002
Messages
43,275
Doesn't sound like it is unbound. Unbound means that there is no RecordSource for the form - ever. Forms like this cannot be continuous.

I see at least three problems with the If statement
"If IsNull(Forms!frmhome!txtID) Then"
... You are using a different form name than what you mentioned in the description. You said the form name is frmTrk
...You are testing for only null. It is possible on a form for a control to contain a Zero Length String (ZLS) and there is no way to tell that visually. To get around this problem, I always use concatenation and check for a ZLS
"If Forms!frmhome!txtID & "" = "" Then"
This works regardless of whether txtID is null or contains a ZLS
.... You are testing txtID but your description was referencing cboTrk
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:41
Joined
Feb 28, 2001
Messages
27,186
Let's simplify something else.

If you are responding to events on a form then to control the focus, don't use the Forms!formname!controlname.SetFocus syntax, use Me.controlname.SetFocus syntax. Less typing but should be the same control. (If it isn't because this code is running from an unrelated form, you have a design issue that really needs to be explored.)

But then you show TWO .SetFocus actions in a row. I'm going to suggest that you will probably have a couple of GotFocus & LostFocus events firing in this melee. Do any of those events have event routines? And if so, do any of the event routines ALSO do a .SetFocus somewhere? Because the events WILL fire and the routines WILL get called.
 

DBUserNew

New member
Local time
Today, 11:11
Joined
Sep 12, 2023
Messages
22
Thank you all for your time & responses!

The issue was, the form (frmTrk) was opening as acDialog. When I remove this and opened the form as normal everything works well. Access successfully SetFocus to cbotrk.

So how can I ensure that this form pop up in front if any other forms were already opened?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:41
Joined
Feb 19, 2002
Messages
43,275
If you want a certain control to have the focus when a form opens, change the tab order to make that control "0". OR, in the load event of the form, set the focus to the control in question. Then you can continue to open the form as a dialog to ensure that it is on top.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 06:41
Joined
Sep 12, 2006
Messages
15,656
A popup form is always on top. Isn't that a separate option to dialog? It might be a different name to popup. It's a yes/no option. You have to be careful not to cover anything you might need as the popup form will always be on top. It uses something called z order to manage the logical order of open forms.

If it's really a subfotm, I don't see why you can't do me.parent.setfocus
 

Users who are viewing this thread

Top Bottom