Skip to content

Getting Text Values from an Option Group

How to get text values from the user’s selection of an option.

Q:  Julia H. writes:  “I have an option group with choices for different areas of the country, and when the user clicks on one, I want to get the name of the area.  But I can only assign numbers as default values to the option buttons.  How can I get the names of the areas?”

A:  Yes, buttons in option groups on Access forms can only be bound to numbers – this is one of the few areas where Outlook has the better way (in option groups on Outlook forms, the buttons can be bound to text).  To get the text values from the user’s selection of an option, you need to convert the number of the selected option to a text value.  This can be done using the Switch function (for up to eight options), or a Select Case statement.  The Switch function is more compact, but the Select Case statement allows you to do other things (such as run procedures or open forms) based on the value selected.

The first procedure below uses the Switch function and puts the text value in a text box on the form; the second procedure uses a Select Case statement to do the same thing.

Private Sub fraSalesArea_AfterUpdate()
 
   Dim intOption As Integer
   Dim txt As Access.TextBox
   
   Set txt = Me![txtSelectedSalesArea]
   intOption = Me![fraSalesArea].Value
   txt.Value = Switch(intOption = 1, “NorthEast”, _
      intOption = 2, “SouthEast”, _
      intOption = 3, “NorthCentral”, _
      intOption = 4, “SouthCentral”, _
      intOption = 5, “NorthWest”, _
      intOption = 6, “California”)
      
End Sub
 
Private Sub fraSalesArea_AfterUpdate()
 
   Dim intOption As Integer
   Dim txt As Access.TextBox
   
   Set txt = Me![txtSelectedSalesArea]
   intOption = Me![fraSalesArea].Value
   
   Select Case intOption
   
      Case 1
         txt.Value = “NorthEast”
         
      Case 2
         txt.Value = “SouthEast”
      
      Case 3
         txt.Value = “NorthCentral”
      
      Case 4
         txt.Value = “SouthCentral”
      
      Case 5
         txt.Value = “NorthWest”
      
      Case 6
         txt.Value = “California”
      
   End Select
   
End Sub

About this author

Office 2024 - all you need to know. Facts & prices for the new Microsoft Office. Do you need it?

Microsoft Office upcoming support end date checklist.