Full Version: VBA Can't Move Focus From Field On Subform To Control On Mainform
UtterAccess Forums > Microsoft® Access > Access Forms
reinerm1
I have a Main Form which has two cbo boxes and a 3 txt boxes and 1 bttn, also on this main form I have a sub formDS which populates via result of the selections of the two cboBoxes. I then dblClick on a field from within the subform to populate the 3 txtboxes (which I then do something else with). Immediately after the dblClick of the field in the subform, the txt boxes populate as desired on the main form, with the desired data. BUT, then I'd like to code VBA to move the focus out of the subform and set the focus back to bttn on the main form. This move of focus is what I can't seem to get working? I have tried [Forms]![MainForm]![bttn1].setfocus and many other ideas I've read about, but can't seem to get the focus to leave the subform and "stick" at the bttn1. Any help would be great! Thanks!
theDBguy
Hi,

Have you tried something like?

Me.Parent.ButtonName.SetFocus

Hope it helps...
reinerm1
Thought I had, but will try again.
theDBguy
Okay. Let us know how it goes. If it doesn't work, maybe posting your code would help someone figure out something.

Cheers!
reinerm1
Well, I must not have tried that, it seems to work fine. The focus goes to the bttn, I press enter and the button on click event fires as coded. Maybe I need to break this into a different post, but if I use the same me.parent.txtbox1.setfocus, I see the focus bounce to the txtbox1 then right back to the field in the subform, why or how does this happen?
theDBguy
Hi,

Are you placing the line of code in the same place, both times? Seeing your code might help us figure out what's really going on.
reinerm1
Private Sub EqpNum_DblClick(Cancel As Integer)

[Forms]![Form2]![txtNew] = [Forms]![Form2]![Form4]![EqpNum]
[Forms]![Form2]![txtDept] = [Forms]![Form2]![Form4]![Dept]
[Forms]![Form2]![txtLine] = [Forms]![Form2]![Form4]![Line]

'Me.Parent.Command30.SetFocus (This is where I tested as you said and it worked, kind of looked like the focus was in both places? but when I pressed enter the bttn event fired)
Me.Parent.txtNew.SetFocus (Here is what I was playing with, and after the dbl click in the subform, [txtNew] blinked "black" but then the focus returns to [EqpNum] field in the subform?)

End Sub
theDBguy
Hi,

For troubleshooting purposes, I might try putting a MsgBox in the GotFocus event of the control to see if it's really getting the focus.

Just a thought...
reinerm1
Cool test, I wrote MsgBox "Got Focus", and yes the msgbox did pop up. As soon as I click "ok" on the msgbox, the [txtNew] on the main form blinked black and the focus ended on the [EqpNum] field in the subform. At this point, if I hit "enter" the field in the subform goes blank, so the focus is surly there.
theDBguy
I guess you may have to use a workaround if sending the focus to another textbox does not cancel the focus on the current one.
tina t
QUOTE
if I use the same me.parent.txtbox1.setfocus, I see the focus bounce to the txtbox1 then right back to the field in the subform

suggest you check the Enter and GotFocus events of txtbox1, to see if there is code in either event procedure that moves focus back to the subform.

hth
tina
reinerm1
Nope......no code behind txtbox, on any event. Could it be something that I'm unknowingly activating within the record within the subform when I dblClick on the field, that the focus instantaneously changes but goes right back because I provoked some action with in the record I dblClicked in the subform?
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.