By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,519 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,519 IT Pros & Developers. It's quick & easy.

Changing focus to a control on a different form

Expert 100+
P: 1,221
I'm having trouble changing the focus from form to form.

I've got a big form with two smaller forms. The main form takes the left 1/3 of the screen, subform1 the upper right, and subform2 the lower right portion of the screen. I want the user to be able to advance to subform1 with a control/M and to subform2 with a control/B keystroke.

I used this code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  3. ' control/M will set the focus to the packages
  4. If KeyCode = vbKeyM And vbKeyControl Then
  5.     'Forms!frm_OEShippingSelectCustomer!fsub_OEShippingProduct.
  6.     With Forms!frm_OEShippingSelectCustomer!fsub_OEShippingProduct
  7.         !txtQtyShipped.SetFocus
  8.         !txtQtyShipped = 99  ' just to see if anything happened
  9.     End With
  11.     KeyCode = 0
  12. End If
  14. ' CONTROL/B will load the form for boxes/packages
  15. If KeyCode = vbKeyB And vbKeyControl Then
  16.     Forms!frm_OEShippingSelectCustomer!fsub_OEShippingProduct.Form!!txtWeight.SetFocus
  17.     KeyCode = 0
  18. End If
  20. End Sub
I want to end up with the focus on a control in the proper subform. But I keep ending up still on the main form. The "99" is showing up on the control txtQtyShipped, so I can see that it is executing the code.

How do I get parked on the proper form after my control keystroke?

Dec 26 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 2.5K+
P: 2,653
Hi, Jim.

The following is from Access help.
You can use the SetFocus method to move the focus to a subform, which is a type of control. You can also move the focus to a control on a subform by using the SetFocus method twice, moving the focus first to the subform and then to the control on the subform.
So the code will look like this.
Expand|Select|Wrap|Line Numbers
  1. With Forms!frm_OEShippingSelectCustomer!fsub_OEShippingProduct
  2.     .SetFocus
  3.     !txtQtyShipped.SetFocus
  4. End With
Dec 26 '07 #2

P: 3
yeah you gotta do it in two steps, like fish said.

Another example using more with blocks - lol
Expand|Select|Wrap|Line Numbers
  2. With Me
  3.   With .fsub_OEShipping
  4.     .SetFocus
  5.     With .Form
  6.       With .txtQtyShipped
  7.         .SetFocus
  8.       End With
  9.     End With
  10.   End With
  11. End With
As you can see you are trying to reference and object that has a nesting level 4 deep.

The best I come up with a two liner:
Expand|Select|Wrap|Line Numbers
  1. Me.Form!fsub_OEShipping.SetFocus
  2. Me.Form!fsub_OEShipping.Form!txtQtyShipped.SetFocus

Alan ";0)
Dec 26 '07 #3

Expert 100+
P: 254
If both of your subforms are based on the same table as their data source (your choices on which subform to pick seems to be based on package or box - basically package size). I would enable or disable controls within a single subform - if package enable QTY and disable weight and viceversa for box. Make it less for end users to have to remember. A little more development work but a better user experience.
Dec 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.