KeyDown Procedure Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a drop down box on my form that I am trying to restrict users from
typing in a drop down list. I seem to be having trobule picking up any
letters being typed in. I get a type mismatch error.

When a letter is typed in, I want to tell the user that they cannot type in
this field, but rather select a selection in a drop down list. Code is below:

Private Sub Comments_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo ErrHandler

Select Case KeyCode
Case vbKeyDown
Me.Comments.Dropdown
Case vbKeyTab
Me.Add_Comment.SetFocus
Case 0 To 9, "A" To "Z", "a" To "z"
Beep
MsgBox "You cannot type comments or numbers in this field." _
& Chr(13) & Chr(13) & "Use the dropdown arrow to select a comment or
" _
& "the Tab key to advance one space to the left to add additional
comments.", vbCritical, "Invalid Character"
Me.Undo
Case Else
'Do Nothing
End Select

KeyCode = 0

ErrHandler_Exit:
Exit Sub

ErrHandler:
If Err.Number = 2105 Then
KeyCode = 0
DoCmd.Beep
GoTo ErrHandler_Exit
Else
MsgBox Err.Description
Resume ErrHandler_Exit
End If
End Sub
 
The problem is in the statement:

Case 0 To 9, "A" To "Z", "a" To "z"

KeyCode is an Integer value and you are trying to compare it with String
values.

OTOH, wouldn't it be easier to simply use the LimitToList Property of the
ComboBox?
 
Back
Top