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

SetFocus to Main form from Subform

P: 17
I have a main form with a button that makes a subform visible.

I added a button in the form footer of the subform that is supposed to hide the subform. This does not work because the focus is on that form.
I tried moving the focus back to the main form and it fails because it says it does not recongize the entered form.
I tried a docmd.close of the subform also nothing is working

Please help!
Here is all 3 attempts

Main Form Name: frmRisk
Subform Name: tblWorkflowAddOns_subform

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Command11_Click()
  2.     Me![frmRisk]!Status.SetFocus
  3.     Me.Visible = False
  4. End Sub
  5.  
  6.  
  7. Private Sub Command7_Click()
  8. On Error GoTo Err_Command7_Click
  9.     'Me![tblWorkflowAddOns_subform].Visible = Not Me![tblWorkflowAddOns_subform].Visible
  10.     'Me!frmrisk!Status.SetFocus
  11.     'Me.Visible = False
  12.    Me![tblWorkflowAddOns_subform].Visible = Not Me![tblWorkflowAddOns_subform].Visible
  13.  
  14. Exit_Command7_Click:
  15.     Exit Sub
  16.  
  17. Err_Command7_Click:
  18.     MsgBox Err.Description
  19.     Resume Exit_Command7_Click
  20.  
  21. End Sub
  22. Private Sub Command10_Click()
  23.  
  24.     DoCmd.OpenForm "frmrisk", acNormal
  25.     'Me.ID.SetFocus
  26.     Me.Parent.SetFocus
  27.     DoCmd.Close acForm, "tblWorkflowAddOns_subform"
  28.  
  29. End Sub
  30.  
Jul 17 '08 #1
Share this Question
Share on Google+
3 Replies


missinglinq
Expert 2.5K+
P: 3,532
Expand|Select|Wrap|Line Numbers
  1. Private Sub HideSubform_Click()
  2.   Forms!frmRisk.ControlOnMainForm.SetFocus
  3.   Forms!frmRisk.SubformControlName.Visible = False
  4. End Sub
where ControlOnMainForm is the name of any control on your main form, and SubformControlName is the name of the subform control. It may or may not be the same as the form the subform is based on.

I'll explain why your attempts failed when I get a few minutes.

Linq ;0)>
Jul 17 '08 #2

P: 17
That worked great!

Thank you! Thank you! Thank you!

Expand|Select|Wrap|Line Numbers
  1. Private Sub HideSubform_Click()
  2.   Forms!frmRisk.ControlOnMainForm.SetFocus
  3.   Forms!frmRisk.SubformControlName.Visible = False
  4. End Sub
where ControlOnMainForm is the name of any control on your main form, and SubformControlName is the name of the subform control. It may or may not be the same as the form the subform is based on.

I'll explain why your attempts failed when I get a few minutes.

Linq ;0)>
Jul 17 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
I'm glad you got it working! Here's an explanation of why your various attempts didn't work. Most of them revolve around the line

Me![frmRisk]!Status.SetFocus

Status

Status
has no meaning in Access/VBA. Status is a property in straight Visual Basic. so the line above
really means nothing in Access/VBA.

Me

Me
, used in a form's code module, refers to the name of that form. Think of it as shorthand for the form name.

Me.Visible means the same as frmRisk.Visible. Because of this, when you wrote Me![frmRisk] you were actually saying frmRisk![frmRisk].

Using SetFocus with Forms

You can only set focus to a form if the form either has no controls on it that can receive focus, or if all of the controls that can receive focus are locked, disabled or invisible. So [frmRisk]!SetFocus will only work, basically if frmRisk falls within these conditions, which it doesn't, if for no other reason because the subform control is visible at this point.

Subforms


This is a critical part of thinking about forms/subforms. A subform is a control on a main form, just like a command button, a textbox or a combobox. You have to understand this, and think of it in this way.

When a form is used as the object source for a subform, it is really, for all intents and purposes, no longer a form! It has become a control, instead, and is subject to all the form's properties and behavior.

Good examples of this are the AllowEdit, AllowAdditions and AllowDeletions properties. When these properties are set on, say FormB, and FormB is run independently, not as a subform on a main form, these properties rule the form's behavior. But if FormB is then used as the source for the subform on a main form, call it FormA, FormB is no longer ruled by these settings. FormB is now ruled by the settings for these properties on FormA! The subform has, in essence, "inherited" these properties from the main form.

An example


FormA (main form)
AllowDeletions is set to No

FormB
(subform)
AllowDeletions is set to Yes

You cannot delete records from the subform. The main form's AllowDeletions set to No overrules the subform's AllowDeletions set to Yes.

Another, even stranger example

FormA (main form)
AllowEdits is set to No
AllowDeletions is set to Yes

FormB (subform)
AllowDeletions is set to Yes

You can delete records on the main form, but you cannot delete records from the subform! This seems really strange! The reason is that FormB is no longer a form; as the source for the subform, it has become a control of the main form, FormA! When you try to "delete" a record from the subform, FormB, Access doesn't look at it as trying to delete a record from the subform, Access looks at it as your trying to change the contents of a control on a record on the main form. In other words, to Access you're trying to "edit" a control on the main form.

Linq ;0)>
Jul 17 '08 #4

Post your reply

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