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!

Change Date Values with + - keys

Status
Not open for further replies.

miletracker

Technical User
Apr 21, 2002
46
US
is it possible to change date value in text boxes with + - keys I have an older DOS record program where that is possible when the focus is on the date "box" pressing + 7 will add 7 days to the current date value
 
Miletracker,

Interesting concept.

Create a form with a single text field called SomeDate; add the following associated event code, and then adapt as you see fit. Note that the sample I'm providing does no error handling or data validation.


Dim strDateAdjust As String 'global variable

Private Sub SomeDate_GotFocus()
'--------------------------------------------------------
'On entry to date field, initialise it to an empty string
'--------------------------------------------------------
strDateAdjust = ""
End Sub


Private Sub SomeDate_KeyPress(KeyAscii As Integer)
'-----------------------------------------------------------
'Detect entry of plus, minus and numeric keys. Ignore others
'-----------------------------------------------------------
Select Case KeyAscii
Case "43" '+
strDateAdjust = "+"
Case "45" '-
strDateAdjust = "-"
Case Else
End Select

If strDateAdjust <> &quot;&quot; Then '+ or minus sequence started
Select Case KeyAscii 'so build delta string
Case &quot;48&quot; To &quot;57&quot; 'numbers 0 to 9
strDateAdjust = strDateAdjust & Chr(KeyAscii)
Case Else
End Select
DoCmd.CancelEvent 'to prevent sign and subsequent numbers from poluting date field
End If
End Sub


Private Sub SomeDate_LostFocus()
'-----------------------------------------------------------
'Do the adjustment to the date here, based on the + or minus
'delta entered.
'Replace msgbox line with something like:
'me.somedate = DateAdd(&quot;d&quot;, Val(strDateAdjust), SomeDate)
'-----------------------------------------------------------
If strDateAdjust <> &quot;&quot; Then
MsgBox &quot;The number entered is &quot; & strDateAdjust
MsgBox &quot;The new date could be set to &quot; & _
DateAdd(&quot;d&quot;, Val(strDateAdjust), SomeDate)

End If
End Sub


Personally, I'd prefer to use the + minus spinner buttons.

Cheers,
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top