Using keystroke to select an option from an option group in an Access form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luvgis
    New Member
    • Jul 2008
    • 12

    Using keystroke to select an option from an option group in an Access form

    I have an Access form to be used for data entry of returned bus surveys. The survey consists of 15 questions and several of the questions will be options groups in the form. For example, question 1 is “When you boarded the bus today, where were you coming from?” The available options are (1) Home, (2) Work, (3) Shopping, ect. I want the data entry person to be able to either select the radio button with the mouse OR be able to tab through hit the appropriate number on the keyboard, (1 for home, 2 for work…) when the first control in the group has focus. I haven’t been able to find any examples of using the On KeyPress event (if that is the right one) in this way. Could someone point me in the right direction?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by luvgis
    I have an Access form to be used for data entry of returned bus surveys. The survey consists of 15 questions and several of the questions will be options groups in the form. For example, question 1 is “When you boarded the bus today, where were you coming from?” The available options are (1) Home, (2) Work, (3) Shopping, ect. I want the data entry person to be able to either select the radio button with the mouse OR be able to tab through hit the appropriate number on the keyboard, (1 for home, 2 for work…) when the first control in the group has focus. I haven’t been able to find any examples of using the On KeyPress event (if that is the right one) in this way. Could someone point me in the right direction?
    It is not exactly intuitive how this can be done since an Option Group does not have any Key Events, but here is a little trickery that you can try:
    1. Set the KeyPreview Property of the Form = Yes which will allow the Form to process any Keystrokes prior to any Controls contained within the Form.
    2. In the Form's KeyPress() Event, make sure the specific Option Group has the Focus, in this case I'll name it optComingFrom.
    3. Select the appropriate Option in the Option Group if either a 1, 2, or 3 is pressed on the Keyboard.
      Code:
      Private Sub Form_KeyPress(KeyAscii As Integer)
      If Screen.ActiveControl.Name = "optComingFrom" Then
        Select Case KeyAscii
          Case vbKey1
            Me![optComingFrom] = 1
          Case vbKey2
            Me![optComingFrom] = 2
          Case vbKey3
            Me![optComingFrom] = 3
          Case Else
            'not really concerned
        End Select
      End If
      End Sub
    4. Expand the concept for additional Option Groups on the Form.

    Comment

    • luvgis
      New Member
      • Jul 2008
      • 12

      #3
      It worked!

      Thank you so much, your directions were perfect.
      I kept looking for the "OnKeyPress " property of the Details section of the form when I should of been looking at the properties of the form itself (which by the way for other newbies can be viewed by right clicking in the little square on the upper left-hand corner of the form in design view). I added a "highlight box" to show up when the control group is active so the user would know which question they were on. I want to share the code for anyone else who may be interested...

      Code:
      Private Sub Form_KeyPress(KeyAscii As Integer)
      
      If Screen.ActiveControl.Name = "Frame_Question1" _
      Then
      
      'Highlight the question
      Me.HighlightBox_Question1.Visible = True
      
      Select Case KeyAscii
          Case vbKey1
            Me!Frame_Question1 = 1
          Case vbKey2
            Me!Frame_Question1 = 2
          Case vbKey3
            Me!Frame_Question1 = 3
          Case vbKey4
            Me!Frame_Question1 = 4
          Case vbKey5
            Me!Frame_Question1 = 5
          Case vbKey6
            Me!Frame_Question1 = 6
          Case vbKey7
            Me!Frame_Question1 = 7
          Case vbKey8
            Me!Frame_Question1 = 8
          Case vbKey9
            Me!Frame_Question1 = 9
          Case vbKey0
            Me!Frame_Question1 = 10
          Case Else
            'not really concerned
        End Select
      End If
      
      If Screen.ActiveControl.Name <> "Frame_Question1" _
      Then
      
      'Remove Highlight
      Me.HighlightBox_Question1.Visible = False
      
      End If
      
      End Sub

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Glad it all worked out for you.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32661

          #5
          By placing an ampersand character (&) before the digits in the associated labels, it is possible to press Alt- followed by the associated digit to trigger the button automatically.

          This isn't exactly what's requested I know, but it does the benefit of being an inbuilt, available option.

          Comment

          Working...