I use the KeyPress event of a form to modify the contents of an unbound textbox that contains a Date/Time value. Currently, I use +/- to modify the date and Shift +/- to modify the hour, but would like to extend the functionality into modifying the minutes. My first inclination was to use Ctrl +/- or Alt +/-, but both of these combinations appear to have dedicated meanings within Access. Ctrl appears to execute some record-navigation functions, and Alt launches the menu.
Is there any way to disable these system-level functions so I could hook into either one of them?
Here is the code that I use for the KeyPress:
Private Sub txtDeliveryTime_KeyPress(KeyAscii As Integer)
KeyAscii = DateHandler(KeyAscii, Me.ActiveControl)
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
Public Function DateHandler(KeyAscii As Integer, ctl As Control) As Integer
Dim sUnit As String
Dim nRet As Integer
' This routine adds or subtracts days, based on the
' key pressed, from a date value found in the control
' represented by the form's ActiveControl property
' (usually a TextBox). The routine can be altered to
' add and subtract months and years too.
On Error GoTo errorHandler
' Constants which represent the '+' & '-' keys.
Const KeyAdd = 43
Const KeySubtract = 45
' This constant is here because '+' & '=' are on the
' same key for most keyboards, but are sometimes inverted.
Const KeyEquals = 61
sUnit = "d" ' default to DAY arithmetic
If GetAsyncKeyState(vbKeyShift) Then
sUnit = "h"
' Determine the value of the key pressed, and
' take the necessary action.
Select Case KeyAscii
Case KeyAdd, KeyEquals
ctl.Text = DateAdd(sUnit, 1, ctl)
nRet = 0
ctl.Text = DateAdd(sUnit, -1, ctl)
nRet = 0
nRet = KeyAscii
' Move the start position to the end of the
' text for a cleaner look.
If nRet = 0 Then
ctl.SelStart = Len(ctl.Text)
' Return a new KeyAscii value.
DateHandler = nRet
DateHandler = 0