how to automatically put a label when i click an option button?

  • Thread starter Last user''''s name that modified a reco
  • Start date
L

Last user''''s name that modified a reco

hi. greetings!

i have an employee database and i have an option group called "Employee
Status" and a label called lblStatus. Now, what I want to do is to display
into the lblStatus the option I selected from the option group. For example,
when I select "AWOL" from the option group, lblStatus should display "AWOL".
Please help me.

Thank you very much!

Resty
 
S

Sprinks

Hi.

Set the Label's caption property in the Option Group's AfterUpdate and the
Form's OnCurrent events. Note that the value of the option group is
different than the label of the selected option.

Select Case Me![YourOptionGroup]
Case 1
Me![YourLabel].Caption = "AWOL"
Case 2
Me![YourLabel].Caption = "SomeOtherValue"
' Place other cases here
Case Else
Me![YourLabel].Caption = ""
End Select

Hope that helps.
Sprinks
 
W

WhyIsDoug

As with anything else in VBA/Access there are several ways to accomplish
this. I always prefer to make the code as dynamic as possible so anytime I
can make the code figure it out without me altering it when I add new options
that's what I would do.

In that vain I would suggest that you add the Status ("AWOL" in your
example) to the Tag property of the Option Button when you add the control to
the Option Groug. Then you can simply loop through the Controls collection of
the Option Group looking for OptionButton controls. Test there OptionValue
property against that of the OptionGroup and then set the Caption of your
Status Label to the Tag property of the Option Button selected.

Here's an example:

Private Sub opgStatus_AfterUpdate()
Dim ctl As Control

For Each ctl In opgStatus.Controls
If TypeOf ctl Is OptionButton Then
If Me.opgStatus.Value = ctl.OptionValue Then
Me.lblStatus.Caption = ctl.Tag
Exit For
End If
End If
Next opbButton

Set ctl = Nothing

End Sub

As you can see, you can now simply add OptionsButton's to the OptionGroup,
put the Status you want in the Tag property of the OptionButton and the code
will continue to work without modification.

This would work well (or even better) if you used a table of status' to
dynamically build the OptionGroup using the Status ID as the OptionValue. Of
course if you reach this point, a different control (such as a ListBox or
ComboBox) would probably work better.

If this answered your question please respond accordingly when asked. THis
will mark it as an answer and allow others to find it more easily.
 
L

Last user''''s name that modified a reco

hi, Sprinks!

Your code worked well. Thank you so much, dude!

Resty

Sprinks said:
Hi.

Set the Label's caption property in the Option Group's AfterUpdate and the
Form's OnCurrent events. Note that the value of the option group is
different than the label of the selected option.

Select Case Me![YourOptionGroup]
Case 1
Me![YourLabel].Caption = "AWOL"
Case 2
Me![YourLabel].Caption = "SomeOtherValue"
' Place other cases here
Case Else
Me![YourLabel].Caption = ""
End Select

Hope that helps.
Sprinks

Last user''''s name that modified a reco said:
hi. greetings!

i have an employee database and i have an option group called "Employee
Status" and a label called lblStatus. Now, what I want to do is to display
into the lblStatus the option I selected from the option group. For example,
when I select "AWOL" from the option group, lblStatus should display "AWOL".
Please help me.

Thank you very much!

Resty
 
Top