Add Subtract Date Using Plus and Minus Keys

D

Dave Elliott

I have a control on my form where I input a date, Where could I use the code
below so pressing the Plus Key or Minus Keyincreases or decreases the date?
Control name is WorkDateI use the tab key only to navigate my forms.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
 
A

AlCamp

Dave,
If the code even works at all, it would probably be on the KeyPress
event, which means that the code fires on every single keystroke. Also,
using this method, you'd be attempting to increase or decrease your Date
field when a plus/minus key was pressed... in any field on the form, at any
time.
Try this instead...

Place 2 small buttons abutting the date field (my ex. DateWorked)... one
with a small plus and one with a small minus.
[DateWorked] [+][ -]

One the click event for each...

Private Sub cmdAdd1Day_Click()
DateWorked = DateAdd("d", 1, DateWorked)
End Sub

Private Sub cmdSubtract1Day_Click()
DateWorked = DateAdd("d", -1, DateWorked)
End Sub

hth
Al Camp
 
J

Jamie Richards

Just as a side note Dave, the code that you posted would not have worked if
the control was unbound, a non-numeric/non-date value, or the value was
NULL. For an unbound control you would need to trap the "YourDate.text"
property to find what the user typed, and always test for the correct data
type. In this case, by using something like IsDate(YourDate), or
IsDate(YourDate.text).

I think it is always better to explicitly pass the control name to a
function to handle these types of things, using Screen.ActiveControl is a
bit of a loose canon (for the reasons Al pointed out).

Jamie

Server side anti spam rules are enforced and ALL unsolicited email is
deleted.

AlCamp said:
Dave,
If the code even works at all, it would probably be on the KeyPress
event, which means that the code fires on every single keystroke. Also,
using this method, you'd be attempting to increase or decrease your Date
field when a plus/minus key was pressed... in any field on the form, at
any
time.
Try this instead...

Place 2 small buttons abutting the date field (my ex. DateWorked)... one
with a small plus and one with a small minus.
[DateWorked] [+][ -]

One the click event for each...

Private Sub cmdAdd1Day_Click()
DateWorked = DateAdd("d", 1, DateWorked)
End Sub

Private Sub cmdSubtract1Day_Click()
DateWorked = DateAdd("d", -1, DateWorked)
End Sub

hth
Al Camp

Dave Elliott said:
I have a control on my form where I input a date, Where could I use the code
below so pressing the Plus Key or Minus Keyincreases or decreases the date?
Control name is WorkDateI use the tab key only to navigate my
forms.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
 
S

Stephen Lebans

Arvin has an example on his site here:
http://www.datastrat.com/Code/Increment.txt


Function fPlusMinus(KeyAscii As Integer, _
Optional dblAmt As Double = 1)
'********************************************************************
' Name: fPlusMinus
' Purpose: Increment or decrement a textbox using the + and - keys
'
' Inputs: KeyAscii As Integer
' dblAmt As Double
'
' Author: Arvin Meyer
' Date: August 18, 2002
' Comment: Can be used for dates or any numbers as long as
' the correct datatype is used
'
' 1 minute = 6.94444444444444E-04
' 5 minutes = 3.47222222222222E-03
' 15 minutes = 1.04166666666667E-02
' 1 day = 1

' Usage:
' Set form control's KeyPress event to: [Event Procedure]
' n is the number to increment
' 1 is the default, if unused, leave comma out:
' Call fPlusMinus(KeyAscii)
' Sample Code:
' Sub txtYourControl_KeyPress(KeyAscii As Integer)
' Call fPlusMinus(KeyAscii, n)
' End Sub
'
'********************************************************************

On Error GoTo ErrHandler

Select Case KeyAscii
Case Asc("-"), Asc("_")
Screen.ActiveControl = Screen.ActiveControl - dblAmt
KeyAscii = 0
Case Asc("="), Asc("+")
Screen.ActiveControl = Screen.ActiveControl + dblAmt
KeyAscii = 0
End Select

ExitHere:
Exit Function
ErrHandler:
Select Case Err
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
End Function






--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 

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