how to disable option button in a frame

  • Thread starter NuBie via AccessMonster.com
  • Start date
N

NuBie via AccessMonster.com

i have sereval option buttons grouped in a frame (group box). how do i
disable some of the buttons.

scenario:

today's month is feb. i want to disable mar to dec

Code:

For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
defValue = Format(Now(), "mm")
grpMonths.Value = defValue
Exit For
End If
Next ctl
 
A

Allen Browne

You can set the Enabled property of the option button to disable it in the
group, but that won't work if it is the currently selected button of the
group.

You will therefore need to examine the Parent property of the button to see
which option group it belongs to, and see if the Value of the Parent equals
the OptionValue of the button (in which case you can't disable it.)

Note that if an option button is not part of an option group, its Parent is
the form itself. In this case you need to ensure the option button is not
the ActiveControl of the form if you want to disable it.

I'm not clear from your sample code, whether you have option buttons that
are *named* 01, 02, etc, or whether those are the OptionValue of the
buttons, or even if it might be the Caption of the attached label. If you
are working with the button names, use:
ctl.Name
If option values:
ctl.OptionValue
If the caption of the attached label:
ctl.Controls(0).Caption

(I'm also unclear what the Exit For is in the loop for, if you want to
continue disabling buttons through December.)

Hopefully that will give you an understanding so you can work with these.
 
N

NuBie via AccessMonster.com

here's what i got
1 and 2 are working the way i want to, but i got run-time error on 3.
Run-time error 438: Object doesn't support this property or method

also is there any better way to do this in one for...next statement.

CODE:
1. 'set default month to current month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
grpMonths = Format(Now(), "mm")
Exit For
End If
Next ctl

2. 'get tag for the default month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(ctl.OptionValue) = CInt(grpMonths) Then
thisTag = ctl.Controls(0).Tag
End If
End If
Next ctl

3. 'disable some months
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(thisTag) <= CInt(ctl.Controls(0).Tag) Then
ctl.Controls(0).Enabled = False
End If
End If
Next ctl


Allen said:
You can set the Enabled property of the option button to disable it in the
group, but that won't work if it is the currently selected button of the
group.

You will therefore need to examine the Parent property of the button to see
which option group it belongs to, and see if the Value of the Parent equals
the OptionValue of the button (in which case you can't disable it.)

Note that if an option button is not part of an option group, its Parent is
the form itself. In this case you need to ensure the option button is not
the ActiveControl of the form if you want to disable it.

I'm not clear from your sample code, whether you have option buttons that
are *named* 01, 02, etc, or whether those are the OptionValue of the
buttons, or even if it might be the Caption of the attached label. If you
are working with the button names, use:
ctl.Name
If option values:
ctl.OptionValue
If the caption of the attached label:
ctl.Controls(0).Caption

(I'm also unclear what the Exit For is in the loop for, if you want to
continue disabling buttons through December.)

Hopefully that will give you an understanding so you can work with these.
i have sereval option buttons grouped in a frame (group box). how do i
disable some of the buttons.
[quoted text clipped - 12 lines]
End If
Next ctl
 
N

NuBie via AccessMonster.com

finally got it! here's the final code. instead of code 3 above. someone might
find it useful so i post the final solution to this problem.

'disable months beyond current/default month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(ctl.Controls(0).Tag) > CInt(thisTag) Then
'replace this :
'ctl.Controls(0).Enabled = False
'with :
ctl.Enabled = False
End If
End If
Next ctl

here's what i got
1 and 2 are working the way i want to, but i got run-time error on 3.
Run-time error 438: Object doesn't support this property or method

also is there any better way to do this in one for...next statement.

CODE:
1. 'set default month to current month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
grpMonths = Format(Now(), "mm")
Exit For
End If
Next ctl

2. 'get tag for the default month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(ctl.OptionValue) = CInt(grpMonths) Then
thisTag = ctl.Controls(0).Tag
End If
End If
Next ctl

3. 'disable some months
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(thisTag) <= CInt(ctl.Controls(0).Tag) Then
ctl.Controls(0).Enabled = False
End If
End If
Next ctl
You can set the Enabled property of the option button to disable it in the
group, but that won't work if it is the currently selected button of the
[quoted text clipped - 28 lines]
 
N

NuBie via AccessMonster.com

Thanks Allen for pointing me to the right direction. have a great day!
finally got it! here's the final code. instead of code 3 above. someone might
find it useful so i post the final solution to this problem.

'disable months beyond current/default month
For Each ctl In Me.Controls
If TypeOf ctl Is OptionButton Then
If CInt(ctl.Controls(0).Tag) > CInt(thisTag) Then
'replace this :
'ctl.Controls(0).Enabled = False
'with :
ctl.Enabled = False
End If
End If
Next ctl
here's what i got
1 and 2 are working the way i want to, but i got run-time error on 3.
[quoted text clipped - 34 lines]
 

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