Form greyed out unless ...

J

JB

Hello
Is this possible?
I'd like all the controls and fields in a subform to be greyed out
(disabled) until a check box in the parent form is checked.
Or is it pie in the sky?

Thank you
Jen
 
D

Douglas J. Steele

It's definitely possible.

I'd create a function to which you pass a boolean value. Depending on the
value passed, the function would either disable or enable the various
controls.

To refer to controls on a subform, you need to use

Forms![NameOfParentForm]![NameOfSubformControl].Form![NameOfControlOnSubform]

or, if referring to it from the parent form

Me![NameOfSubformControl].Form![NameOfControlOnSubform]

Note that depending on how you added the form as a subform, the name of the
subform control may not be the same as the name of the form being used as a
subform.
 
D

Dirk Goldgar

JB said:
Hello
Is this possible?
I'd like all the controls and fields in a subform to be greyed out
(disabled) until a check box in the parent form is checked.
Or is it pie in the sky?


You can easily enable or disable the whole subform as a unit:

' Disable the subform
Me!sfMySubform.Enabled = False

' Enable the subform
Me!sfMySubform.Enabled = True

That will make the subform inoperable, but it won't grey out anything but
the subform control's attached label (if any).

It would be possible to use a loop to disable *almost* all the controls on
the subform, while leaving the subform itself enabled, and thus have them
appear greyed out. However, you can't disable the control on the subform
that is currently its active control. So you need to designate one control
to get the focus while others are disabled. Is this something you want to
pursue?
 
J

JB

Yes I do, thank you.
The parent form is 'Candidates' for registration etc and suitability, if
the 'Suitable' check box isn't ticked, I want the subform to be dormant so
to speak. If it is ticked then I want it to be 'enabled' to be completed. I
would really prefer it to seem greyed until the suitable check box in the
Candidates form is ticked.
I'm ready for instructions!!
J
 
D

Dirk Goldgar

JB said:
Yes I do, thank you.
The parent form is 'Candidates' for registration etc and suitability, if
the 'Suitable' check box isn't ticked, I want the subform to be dormant so
to speak. If it is ticked then I want it to be 'enabled' to be completed.
I would really prefer it to seem greyed until the suitable check box in
the Candidates form is ticked.
I'm ready for instructions!!


Okay. As I mentioned before, you're going to have to pick one control on
the subform to receive the focus while the others are disabled. This
control cannot be disabled.

Here's code for a function you can put into a standard module:

'------ start of code for standard module ------
Public Function fncEnableDisableControls( _
frm As Form, _
EnableIt As Boolean, _
FocusControl As String)

' Enable or disable all data-bound controls on form <frm>,
' depending on the value of <EnableIt>: True = Enable; False = Disable.

On Error GoTo Err_fncEnabledisableControls

Const conERR_NO_PROPERTY = 438

Dim ctl As Control

If EnableIt = False Then
frm.Controls(FocusControl).SetFocus
End If

For Each ctl In frm.Controls
If ctl.Name <> FocusControl Then
ctl.Enabled = EnableIt
End If
Skip_Control: ' come here from error if no .Enabled property
Next ctl

Exit_fncEnabledisableControls:
Exit Function

Err_fncEnabledisableControls:
If Err.Number = conERR_NO_PROPERTY Then
Resume Skip_Control
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_fncEnabledisableControls
End If

End Function
'------ end of code for standard module ------

Now, in the main form's module you'll need to call this function from two
places: the form's Current event and the AfterUpdate event of the
[Suitable] check box. Suppose that your subform is named "sfSuitability",
and the control on that form that you want to hold the focus when disabling
other control is called "ID". (Of course, you'll need to amend these names
in the code below.) Then your code for the main form's module would be
something like this:

'------ start of code for form module ------
Private Sub Form_Current()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub


Private Sub Suitable_AfterUpdate()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub
'------ end of code for form module ------
 
J

JB

Wow!! how d'you do that so quick?!!
Thank you SO much.
Ok I'll give it a go and get back to you.
Jen


Dirk Goldgar said:
JB said:
Yes I do, thank you.
The parent form is 'Candidates' for registration etc and suitability, if
the 'Suitable' check box isn't ticked, I want the subform to be dormant
so to speak. If it is ticked then I want it to be 'enabled' to be
completed. I would really prefer it to seem greyed until the suitable
check box in the Candidates form is ticked.
I'm ready for instructions!!


Okay. As I mentioned before, you're going to have to pick one control on
the subform to receive the focus while the others are disabled. This
control cannot be disabled.

Here's code for a function you can put into a standard module:

'------ start of code for standard module ------
Public Function fncEnableDisableControls( _
frm As Form, _
EnableIt As Boolean, _
FocusControl As String)

' Enable or disable all data-bound controls on form <frm>,
' depending on the value of <EnableIt>: True = Enable; False = Disable.

On Error GoTo Err_fncEnabledisableControls

Const conERR_NO_PROPERTY = 438

Dim ctl As Control

If EnableIt = False Then
frm.Controls(FocusControl).SetFocus
End If

For Each ctl In frm.Controls
If ctl.Name <> FocusControl Then
ctl.Enabled = EnableIt
End If
Skip_Control: ' come here from error if no .Enabled property
Next ctl

Exit_fncEnabledisableControls:
Exit Function

Err_fncEnabledisableControls:
If Err.Number = conERR_NO_PROPERTY Then
Resume Skip_Control
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_fncEnabledisableControls
End If

End Function
'------ end of code for standard module ------

Now, in the main form's module you'll need to call this function from two
places: the form's Current event and the AfterUpdate event of the
[Suitable] check box. Suppose that your subform is named "sfSuitability",
and the control on that form that you want to hold the focus when
disabling other control is called "ID". (Of course, you'll need to amend
these names in the code below.) Then your code for the main form's module
would be something like this:

'------ start of code for form module ------
Private Sub Form_Current()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub


Private Sub Suitable_AfterUpdate()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub
'------ end of code for form module ------


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
J

JB

YEAY!!!
Fantastic. Thank you so much!!
Thank you!! thank you!!!
Jx

JB said:
Wow!! how d'you do that so quick?!!
Thank you SO much.
Ok I'll give it a go and get back to you.
Jen


Dirk Goldgar said:
JB said:
Yes I do, thank you.
The parent form is 'Candidates' for registration etc and suitability,
if the 'Suitable' check box isn't ticked, I want the subform to be
dormant so to speak. If it is ticked then I want it to be 'enabled' to
be completed. I would really prefer it to seem greyed until the suitable
check box in the Candidates form is ticked.
I'm ready for instructions!!


Okay. As I mentioned before, you're going to have to pick one control on
the subform to receive the focus while the others are disabled. This
control cannot be disabled.

Here's code for a function you can put into a standard module:

'------ start of code for standard module ------
Public Function fncEnableDisableControls( _
frm As Form, _
EnableIt As Boolean, _
FocusControl As String)

' Enable or disable all data-bound controls on form <frm>,
' depending on the value of <EnableIt>: True = Enable; False =
Disable.

On Error GoTo Err_fncEnabledisableControls

Const conERR_NO_PROPERTY = 438

Dim ctl As Control

If EnableIt = False Then
frm.Controls(FocusControl).SetFocus
End If

For Each ctl In frm.Controls
If ctl.Name <> FocusControl Then
ctl.Enabled = EnableIt
End If
Skip_Control: ' come here from error if no .Enabled property
Next ctl

Exit_fncEnabledisableControls:
Exit Function

Err_fncEnabledisableControls:
If Err.Number = conERR_NO_PROPERTY Then
Resume Skip_Control
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_fncEnabledisableControls
End If

End Function
'------ end of code for standard module ------

Now, in the main form's module you'll need to call this function from two
places: the form's Current event and the AfterUpdate event of the
[Suitable] check box. Suppose that your subform is named
"sfSuitability", and the control on that form that you want to hold the
focus when disabling other control is called "ID". (Of course, you'll
need to amend these names in the code below.) Then your code for the
main form's module would be something like this:

'------ start of code for form module ------
Private Sub Form_Current()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub


Private Sub Suitable_AfterUpdate()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub
'------ end of code for form module ------


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
J

JB

Hi.
Dirk hope you're still there.
It works great. But I've discovered that when I go to add a new Candidate
(in the parent form), I get a debug error.
Run-time error '94':
Invalid use of Null

If I 'end', I can continue, and subform doing what it's meant to.
If I 'debug', vb opens on the module with yellow highlight on
fncEnableDisableControls Me!fsubAssessment.Form, Me.Suitable,
"AssessmentDate"

Jen


JB said:
YEAY!!!
Fantastic. Thank you so much!!
Thank you!! thank you!!!
Jx

JB said:
Wow!! how d'you do that so quick?!!
Thank you SO much.
Ok I'll give it a go and get back to you.
Jen


Dirk Goldgar said:
Yes I do, thank you.
The parent form is 'Candidates' for registration etc and suitability,
if the 'Suitable' check box isn't ticked, I want the subform to be
dormant so to speak. If it is ticked then I want it to be 'enabled' to
be completed. I would really prefer it to seem greyed until the
suitable check box in the Candidates form is ticked.
I'm ready for instructions!!


Okay. As I mentioned before, you're going to have to pick one control
on the subform to receive the focus while the others are disabled. This
control cannot be disabled.

Here's code for a function you can put into a standard module:

'------ start of code for standard module ------
Public Function fncEnableDisableControls( _
frm As Form, _
EnableIt As Boolean, _
FocusControl As String)

' Enable or disable all data-bound controls on form <frm>,
' depending on the value of <EnableIt>: True = Enable; False =
Disable.

On Error GoTo Err_fncEnabledisableControls

Const conERR_NO_PROPERTY = 438

Dim ctl As Control

If EnableIt = False Then
frm.Controls(FocusControl).SetFocus
End If

For Each ctl In frm.Controls
If ctl.Name <> FocusControl Then
ctl.Enabled = EnableIt
End If
Skip_Control: ' come here from error if no .Enabled property
Next ctl

Exit_fncEnabledisableControls:
Exit Function

Err_fncEnabledisableControls:
If Err.Number = conERR_NO_PROPERTY Then
Resume Skip_Control
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_fncEnabledisableControls
End If

End Function
'------ end of code for standard module ------

Now, in the main form's module you'll need to call this function from
two places: the form's Current event and the AfterUpdate event of the
[Suitable] check box. Suppose that your subform is named
"sfSuitability", and the control on that form that you want to hold the
focus when disabling other control is called "ID". (Of course, you'll
need to amend these names in the code below.) Then your code for the
main form's module would be something like this:

'------ start of code for form module ------
Private Sub Form_Current()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub


Private Sub Suitable_AfterUpdate()

fncEnableDisableControls Me!sfSuitability.Form, Me.Suitable, "ID"

End Sub
'------ end of code for form module ------


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

JB said:
Hi.
Dirk hope you're still there.
It works great. But I've discovered that when I go to add a new Candidate
(in the parent form), I get a debug error.
Run-time error '94':
Invalid use of Null

If I 'end', I can continue, and subform doing what it's meant to.
If I 'debug', vb opens on the module with yellow highlight on
fncEnableDisableControls Me!fsubAssessment.Form, Me.Suitable,
"AssessmentDate"

Ah. Now that you mention it, that could happen on a new record, if the
value of Me.Suitable is Null. Try this version:

fncEnableDisableControls Me!fsubAssessment.Form, _
Nz(Me.Suitable, False), "AssessmentDate"
 
J

JB

PERFECT!
Thank you
Jen

Dirk Goldgar said:
Ah. Now that you mention it, that could happen on a new record, if the
value of Me.Suitable is Null. Try this version:

fncEnableDisableControls Me!fsubAssessment.Form, _
Nz(Me.Suitable, False), "AssessmentDate"

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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

Top