Per Bob Vance:
Trying to find a code to change the date in a text box ,
Here's what I've been using (if the text wrapping
is too bad, I can flip you a .txt file):
---------------------------------------------------------
Public Sub SpinDate(ByRef theControl As Control, ByRef theKeyCode
As Integer, ByVal theShiftStatus As Integer, ByVal
theNumberOfDays As Long, Optional theDateFormat As String)
DebugStackPush mModuleName & ": SpinDate"
On Error GoTo SpinDate_err
' PURPOSE: To increment/decrement date in the specified text box
if user has pressed
' one of the arrow keys AND if the control is enabled
' ACCEPTS: - Pointer to control in question (usually a TextBox,
sometimes a ComboBox)
' - Code of key pressed by user
' - Shift status of keyboard
' - Number of days to increment/decrement
' - Optional format for the date
' SETS: - Specified control's.Value to a new date
' - KeyCode to zero if date changed, effectively
swallowing the keystroke
'
' NOTES: 1) Intended to be called from a date field's keyDown
event.
' Code in the keyDown should look like this:
' +------------------------------------------------
' |
' | SpinDate Screen.ActiveControl, KeyCode, Shift, 1
' |
' +------------------------------------------------
' 2) We do not want to co-opt Alt/Ctrl key combinations
because that would
' effectively disable things like the user's pressing
Alt+S to save
' or Alt+M to commit, hence the altDown/ctrlDown
coding.
Dim altDown As Integer
Dim ctrlDown As Integer
Dim myValue As Variant
altDown = (theShiftStatus And acAltMask)
ctrlDown = (theShiftStatus And acCtrlMask)
If (theControl.Locked = False) And (theControl.Enabled = True)
Then
If (altDown = False) And (ctrlDown = False) Then
If theNumberOfDays = 30 Then 'Special case: taken to be
request to move by a full month
If IsDate(theControl.Value) Then
Select Case theKeyCode
Case vbKeyRight ', vbKeyUp ' We use
KeyUp/Down for walking the list
myValue = DateAdd("m", 1, theControl.Value)
Case vbKeyLeft
myValue = DateAdd("m", -1, theControl.Value)
End Select
Else
Select Case theKeyCode
Case vbKeyRight, vbKeyLeft ', vbKeyDown,
vbKeyUp ' We use KeyUp/Down for walking the list
myValue = VBA.Format$(Date, "mm/dd/yyyy")
End Select
End If
Else
If IsDate(theControl.Value) Then
Select Case theKeyCode
Case vbKeyRight ', vbKeyUp ' We use
KeyUp/Down for walking the list
myValue = DateAdd("d", theNumberOfDays,
theControl.Value)
Case vbKeyLeft
myValue = DateAdd("d", -1 * theNumberOfDays,
theControl.Value)
End Select
Else
Select Case theKeyCode
Case vbKeyRight, vbKeyLeft ', vbKeyDown,
vbKeyUp ' We use KeyUp/Down for walking the list
myValue = VBA.Format$(Date, "mm/dd/yyyy")
End Select
End If
End If
If Len(myValue & "") > 0 Then
theKeyCode = 0
If IsMissing(theDateFormat) Then
theControl.Value = myValue
Else
theControl.Value = Format$(myValue, theDateFormat)
End If
End If
End If
End If
SpinDate_xit:
DebugStackPop
On Error Resume Next
Exit Sub
SpinDate_err:
BugAlert True, ""
Resume SpinDate_xit
End Sub