By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,478 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,478 IT Pros & Developers. It's quick & easy.

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

P: 12
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?
May 20 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 5K+
P: 8,679
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.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2. If Screen.ActiveControl.Name = "optComingFrom" Then
    3.   Select Case KeyAscii
    4.     Case vbKey1
    5.       Me![optComingFrom] = 1
    6.     Case vbKey2
    7.       Me![optComingFrom] = 2
    8.     Case vbKey3
    9.       Me![optComingFrom] = 3
    10.     Case Else
    11.       'not really concerned
    12.   End Select
    13. End If
    14. End Sub
  4. Expand the concept for additional Option Groups on the Form.
May 20 '09 #2

P: 12
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...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyPress(KeyAscii As Integer)
  3. If Screen.ActiveControl.Name = "Frame_Question1" _
  4. Then
  6. 'Highlight the question
  7. Me.HighlightBox_Question1.Visible = True
  9. Select Case KeyAscii
  10.     Case vbKey1
  11.       Me!Frame_Question1 = 1
  12.     Case vbKey2
  13.       Me!Frame_Question1 = 2
  14.     Case vbKey3
  15.       Me!Frame_Question1 = 3
  16.     Case vbKey4
  17.       Me!Frame_Question1 = 4
  18.     Case vbKey5
  19.       Me!Frame_Question1 = 5
  20.     Case vbKey6
  21.       Me!Frame_Question1 = 6
  22.     Case vbKey7
  23.       Me!Frame_Question1 = 7
  24.     Case vbKey8
  25.       Me!Frame_Question1 = 8
  26.     Case vbKey9
  27.       Me!Frame_Question1 = 9
  28.     Case vbKey0
  29.       Me!Frame_Question1 = 10
  30.     Case Else
  31.       'not really concerned
  32.   End Select
  33. End If
  35. If Screen.ActiveControl.Name <> "Frame_Question1" _
  36. Then
  38. 'Remove Highlight
  39. Me.HighlightBox_Question1.Visible = False
  41. End If
  43. End Sub
May 21 '09 #3

Expert 5K+
P: 8,679
Glad it all worked out for you.
May 21 '09 #4

Expert Mod 15k+
P: 31,709
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.
May 24 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.