KeyPress to change date

B

Bob Vance

Trying to find a code to change the date in a text box , thought this might
work but it dosent!
Private Sub tbEndDate1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub
 
B

Beetle

How about;

Private Sub tbEndDate_1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Me.tbEndDate_1 = DateAdd("d", 1, [tbEndDate_1])
Case 45 ' Minus key
KeyAscii = 0
Me.tbEndDate_1 = DateAdd("d", -1, [tbEndDate_1])
End Select

End Sub
 
H

Hans Up

Bob said:
Trying to find a code to change the date in a text box , thought this might
work but it dosent!
Private Sub tbEndDate1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub

I just pasted your code into the KeyPress event for a text control bound
to a Date/Time field in a form's record source. It works here with
Access 2003, SP3.

What's happening when you try it? Do you get an error message?
 
P

(PeteCresswell)

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
 
A

Armen Stein

Trying to find a code to change the date in a text box , thought this might
work but it dosent!
Private Sub tbEndDate1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub

We have a free download called Cool Tools that demonstrates a reusable
"Quick Date" function, including incrementing weeks, months, and years
in addition to +/- for days.

It's at www.JStreetTech.com/Downloads.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
B

Bob Vance

Bettle, BRILLIANT thank you very much....Bob :)

Beetle said:
How about;

Private Sub tbEndDate_1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Me.tbEndDate_1 = DateAdd("d", 1, [tbEndDate_1])
Case 45 ' Minus key
KeyAscii = 0
Me.tbEndDate_1 = DateAdd("d", -1, [tbEndDate_1])
End Select

End Sub
--
_________

Sean Bailey


Bob Vance said:
Trying to find a code to change the date in a text box , thought this
might
work but it dosent!
Private Sub tbEndDate1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 ' Minus key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top