Change Dates With Plus and Minus Keys

This simple piece of code saves a lot of headaches when it comes to altering dates. It allows you to use the plus and minus keys to increment and decrement date values easily. This example assumes there is a textbox named Text1 on a standard VB form:

 Private Sub Form_Load()	' Make sure that there is a valid date in Text1.	Text1.Text = DateEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)	' Pass handling to generic routine.	KeyAscii = DateHandler(KeyAscii)End SubPrivate Function DateHandler(KeyAscii As _	Integer) As Integer	Dim nRet As Integer	' This routine adds or subtracts days, based on the 	' key pressed, from a date value found in the control 	' represented by the form's ActiveControl property 	' (usually a TextBox). The routine can be altered to 	' add and subtract months and years too.	On Error GoTo ErrorHandler	' Constants which represent the '+' & '-' keys.	Const KeyAdd = 43	Const KeySubtract = 45	' This constant is here because '+' & '=' are on the 	' same key for most keyboards, but are sometimes inverted.	Const KeyEquals = 61' Determine the value of the key pressed, and 	' take the necessary action.	Select Case KeyAscii		Case KeyAdd, KeyEquals			Me.ActiveControl.Text = DateAdd("d", _				1, Me.ActiveControl)			nRet = 0		Case KeySubtract			Me.ActiveControl.Text = DateAdd("d", _				-1, Me.ActiveControl)			nRet = 0		Case Else			nRet = KeyAscii	End Select	' Move the start position to the end of the 	' text for a cleaner look.	If nRet = 0 Then		Me.ActiveControl.SelStart = Len(Text1.Text)	End If	' Return a new KeyAscii value.	DateHandler = nRet	Exit FunctionErrorHandler:	DateHandler = 0	Exit FunctionEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS