Can you make something happen on a userform if you push a button eg spacebar?????????????:confused: Thank you for any help
Printable View
Can you make something happen on a userform if you push a button eg spacebar?????????????:confused: Thank you for any help
You need to use the KeyPress event. As with any event trapping there are several factors to be taken into account.
There is not much point in using the Form Keypress because it is very rare, if ever, that the form has focus. This means trapping the keypress on the various controls.
The problem then is that several controls such as list boxes and combo boxes require the use of keys anyway. Pressing Shift or Escape etc. also activate the KeyPress, so you could use these or other such keys that are unlikely to be used in the context.
Another way is to use Shift + another key. Here is some code for a combobox that traps Shift + any ascii key. It does not solve the problem that the key value will appear in the combobox. I'll leave that one to you depending on what you want to do.
Code:Private Sub ComboBox1_KeyDown(ByVal KeyCode _
As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim c As String
If Shift = 0 Then Exit Sub ' shift key is up
If KeyCode > 31 Then ' trap only shift + ascii characters
c = Chr(KeyCode)
MsgBox ("Pressed key Shift and " & c)
End If
End Sub