Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

incrementing dates values using "-" or "+"

Status
Not open for further replies.

ind

Programmer
Mar 9, 2000
121
US
How can I incrementing dates values using "-" or "+"?
 
I don't think you can in Access. I think you can in Visual Basic by adding or subtracting 1 from the stored value that is used for the date (this is a floating point number where the integer part represents the number of days from 30/12/1899 and the decimal fraction part represents part of a day, e.g. '.5' = 12 hours). Then you would use the value in a formatted field to show as a normal date.

I don't think you can do this in Access - usually you have to use DateAdd to tell the system what you are adding (days, months, years etc.) and how many to add, e.g. DateAdd("m", 1, now()) would add one month to today's date.

If anyone knows otherwise, I will be happy to be corrected.

Have fun! :eek:)

Alex Middleton
 
try this

To test this create a form with one named textbox named txtDate, then add the following code to the code module behind the form. Ensure that the Form's KeyPreview Event is set to YES, and that the Form's KeyDown Event is set to [Event Procedure]. Then open the form, press the keys( + and - )

Option Compare Database
Option Explicit
Dim intPrevKey As Integer

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If Me.ActiveControl = txtDate Then
Select Case KeyCode
Case 107 ' Plus Key on KeyPad
KeyCode = 0
txtDate = DateAdd("d", 1, txtDate)
Case 109 ' Minus Key on KeyPad
KeyCode = 0
txtDate = DateAdd("d", -1, txtDate)
Case 189 ' Minus & Underline Key
If intPrevKey <> 16 And Shift = 0 Then 'Shift Key
KeyCode = 0
txtDate = DateAdd(&quot;d&quot;, -1, txtDate)
End If
Case 187 ' Equal & Plus Key
If intPrevKey = 16 Or Shift = 1 Then 'Shift Key
KeyCode = 0
txtDate = DateAdd(&quot;d&quot;, 1, txtDate)
End If
Case Else
'do nothing
End Select
intPrevKey = KeyCode
End If
End Sub


PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top