Often times it's necessary to have the Plus/Minus keys act as spin controls in a
numeric or text fields, in other words, have the date/number increase/decrease it's value
when the plus or the minus keys are pressed.
You can use this public function from the KeyPress event of a control to have the
desired effect on the value of the control.
Public Function PlusMinus(intKey As Integer, strFormName As String, Optional _
strSubformName As String = "", Optional strSubSubFormName As String = "") _
As Integer
On Error GoTo TheHandler
Dim ctl As Control
If strSubformName <> "" Then
If strSubSubFormName <> "" Then
Set ctl = Forms(strFormName).Controls(strSubformName).Form.Controls(strSubSubFormName).Form.ActiveControl
Else
Set ctl = Forms(strFormName).Controls(strSubformName).Form.ActiveControl
End If
Else
Set ctl = Forms(strFormName).ActiveControl
End If
ctl = CDate(ctl)
Select Case intKey
Case Is = 43
ctl = ctl + 1
intKey = 0
Case Is = 45
ctl = ctl - 1
intKey = 0
Case Is = 61
ctl = ctl + 1
intKey = 0
End Select
ExitHandler:
PlusMinus = intKey
Set ctl = Nothing
Exit Function
TheHandler:
Select Case Err.Number
Case Is = 94
Case Is = 13
Case Else
MsgBox Err.Number & ": " & Err.Description
intKey = 0
End Select
Resume ExitHandler
End Function
|