Disable All Controls on a SubForm

  • Thread starter Thread starter Rick A
  • Start date Start date
R

Rick A

I want to set enable=false for all controls on a subform but I cannot do that because one control needs focus. At least that is what I am lead to believe.

Code
subform.form!control.enable = false

Perhaps my real question is why does a control on the subform have focus when I've changed focus to a control on the main form. I would think that since focus is on a main form control no control on the subform would have focus.

Any help is appreciated.
 
How are you changing focus to the main form? If focus is actually on the main form then your code should work. I did a little test and when I tried to disable all subform controls by clicking a button on the main form I discovered that the subform (which previously had the focus) did in fact still have the focus until AFTER the button click event had completed. So, by adding a setfocus statement to the button click event, I was able to disable all controls.

Me.Command5.SetFocus
For Each ctl In Me.Child0.Form.Controls
ctl.Enabled = Not ctl.Enabled
Debug.Print ctl.Name
Next ctl

Note that without error handling, this code doesn't work since labels don't have an Enabled property. If you are using a loop you need to include error handling or logic to not attempt changing the enabled property on controls that do not have the property.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

I want to set enable=false for all controls on a subform but I cannot do that because one control needs focus. At least that is what I am lead to believe.

Code
subform.form!control.enable = false

Perhaps my real question is why does a control on the subform have focus when I've changed focus to a control on the main form. I would think that since focus is on a main form control no control on the subform would have focus.

Any help is appreciated.
 
ummmm...

in my code I do what you suggested, at least I think I am. This is the code from a cancel button.

Me.AllowAdditions = False
Me.cboSelectName.Enabled = True
Me.cboSelectName.SetFocus
Me.cmdCancel.Visible = False
Me.cmdAddRecord.Enabled = True
EnableControls Me, acDetail, False ' this disables all controls on the main form
SubformControls (False)

SubformControls is a subroutine that does this:
Sub SubformControls(TrueFalse As Boolean)
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
end sub

It seems that txtOrganizationCD has focus on the subform and I'll be darned if I know why.
If I flip the order...
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse ' this is now last
...the other two controls are handled correctly.

Anything pop out in what I've shared.

Thanks,

--
Rick Allison
How are you changing focus to the main form? If focus is actually on the main form then your code should work. I did a little test and when I tried to disable all subform controls by clicking a button on the main form I discovered that the subform (which previously had the focus) did in fact still have the focus until AFTER the button click event had completed. So, by adding a setfocus statement to the button click event, I was able to disable all controls.

Me.Command5.SetFocus
For Each ctl In Me.Child0.Form.Controls
ctl.Enabled = Not ctl.Enabled
Debug.Print ctl.Name
Next ctl

Note that without error handling, this code doesn't work since labels don't have an Enabled property. If you are using a loop you need to include error handling or logic to not attempt changing the enabled property on controls that do not have the property.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

I want to set enable=false for all controls on a subform but I cannot do that because one control needs focus. At least that is what I am lead to believe.

Code
subform.form!control.enable = false

Perhaps my real question is why does a control on the subform have focus when I've changed focus to a control on the main form. I would think that since focus is on a main form control no control on the subform would have focus.

Any help is appreciated.
 
Presumably, cboSelectName should have the focus unless you are doing something in EnableControls to change that (I'm also guessing you are leaving cboSelectName enabled).

Without seeing all of the code (and perhaps even with seeing it) I'm not sure why your focus is remaining on the subform. A workaround would be to create a transparent button on the subform and set focus to it first. Don't feel bad if you don't like the workaround without knowing why the problem exists in the first place - I don't either!

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

ummmm...

in my code I do what you suggested, at least I think I am. This is the code from a cancel button.

Me.AllowAdditions = False
Me.cboSelectName.Enabled = True
Me.cboSelectName.SetFocus
Me.cmdCancel.Visible = False
Me.cmdAddRecord.Enabled = True
EnableControls Me, acDetail, False ' this disables all controls on the main form
SubformControls (False)

SubformControls is a subroutine that does this:
Sub SubformControls(TrueFalse As Boolean)
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
end sub

It seems that txtOrganizationCD has focus on the subform and I'll be darned if I know why.
If I flip the order...
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse ' this is now last
...the other two controls are handled correctly.

Anything pop out in what I've shared.

Thanks,

--
Rick Allison
How are you changing focus to the main form? If focus is actually on the main form then your code should work. I did a little test and when I tried to disable all subform controls by clicking a button on the main form I discovered that the subform (which previously had the focus) did in fact still have the focus until AFTER the button click event had completed. So, by adding a setfocus statement to the button click event, I was able to disable all controls.

Me.Command5.SetFocus
For Each ctl In Me.Child0.Form.Controls
ctl.Enabled = Not ctl.Enabled
Debug.Print ctl.Name
Next ctl

Note that without error handling, this code doesn't work since labels don't have an Enabled property. If you are using a loop you need to include error handling or logic to not attempt changing the enabled property on controls that do not have the property.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

I want to set enable=false for all controls on a subform but I cannot do that because one control needs focus. At least that is what I am lead to believe.

Code
subform.form!control.enable = false

Perhaps my real question is why does a control on the subform have focus when I've changed focus to a control on the main form. I would think that since focus is on a main form control no control on the subform would have focus.

Any help is appreciated.
 
Yep - I put the transparent button on the subform, set focus to the button, and everything is working as planned. Thanks for the work around. I'm okay with doing this because it works!!

--
Rick Allison
Presumably, cboSelectName should have the focus unless you are doing something in EnableControls to change that (I'm also guessing you are leaving cboSelectName enabled).

Without seeing all of the code (and perhaps even with seeing it) I'm not sure why your focus is remaining on the subform. A workaround would be to create a transparent button on the subform and set focus to it first. Don't feel bad if you don't like the workaround without knowing why the problem exists in the first place - I don't either!

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

ummmm...

in my code I do what you suggested, at least I think I am. This is the code from a cancel button.

Me.AllowAdditions = False
Me.cboSelectName.Enabled = True
Me.cboSelectName.SetFocus
Me.cmdCancel.Visible = False
Me.cmdAddRecord.Enabled = True
EnableControls Me, acDetail, False ' this disables all controls on the main form
SubformControls (False)

SubformControls is a subroutine that does this:
Sub SubformControls(TrueFalse As Boolean)
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
end sub

It seems that txtOrganizationCD has focus on the subform and I'll be darned if I know why.
If I flip the order...
fsubLastEntered.Form!txtApplicationTypeCD.Enabled = TrueFalse
fsubLastEntered.Form!txtLastEnteredDate.Enabled = TrueFalse
fsubLastEntered.Form!txtOrganizationCD.Enabled = TrueFalse ' this is now last
...the other two controls are handled correctly.

Anything pop out in what I've shared.

Thanks,

--
Rick Allison
How are you changing focus to the main form? If focus is actually on the main form then your code should work. I did a little test and when I tried to disable all subform controls by clicking a button on the main form I discovered that the subform (which previously had the focus) did in fact still have the focus until AFTER the button click event had completed. So, by adding a setfocus statement to the button click event, I was able to disable all controls.

Me.Command5.SetFocus
For Each ctl In Me.Child0.Form.Controls
ctl.Enabled = Not ctl.Enabled
Debug.Print ctl.Name
Next ctl

Note that without error handling, this code doesn't work since labels don't have an Enabled property. If you are using a loop you need to include error handling or logic to not attempt changing the enabled property on controls that do not have the property.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

I want to set enable=false for all controls on a subform but I cannot do that because one control needs focus. At least that is what I am lead to believe.

Code
subform.form!control.enable = false

Perhaps my real question is why does a control on the subform have focus when I've changed focus to a control on the main form. I would think that since focus is on a main form control no control on the subform would have focus.

Any help is appreciated.
 
Rick A said:
Yep - I put the transparent button on the subform, set focus to the
button, and everything is working as planned. Thanks for the work
around. I'm okay with doing this because it works!!

In a form/subform arrangement, there's a control on the main form that
has the main form's focus, and a control on the subform that has the
subform's focus. The subform is active when the main form's active
control is the subform control (on the main form). The application's
focus may be on the main form and its active control, but the subform's
active control still has the focus *on that form* -- if the main form's
focus goes to the subform, the subform's active control is the control
that will have the application's focus. The message "You can't disable
a control while it has the focus" might be better expressed as "You
can't disable a form's active control."

You can't disable a control while it is the active control on its parent
form -- even if that form is a subform that doesn't currently have the
focus on *its* parent form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top