On a form in my database there are several text boxes in which to enter dates. I have a keypress routine which, in part, goes as follows.
Private Sub txtDate_KeyPress(keyascii As Integer)
Select Case keyascii
Case 84, 116 ' "t" for today
keyascii = 0
ActiveControl = Format(Date, "dd/mm/yyyy")
Case 48 To 57 ' 0 to 9 allowed
keyascii = keyascii
Case 8
If keyascii = 8 Then Exit Sub
Case 47 ' backslash
keyascii = keyascii
End Select
End Sub
This works fine. If I type "t", I get today's date. If I type 22/2/2009, I get that date
To avoid having to put this routine in the keypress event for every textbox which is intended to contain a date, I would like to put the routine in a separate module like so:
Function KeyPressed(vDate, keyascii)
Select Case keyascii
Case 84, 116 ' "t" for today
keyascii = 0
vDate = Format(Date, "dd/mm/yyyy")
Case 48 To 57 ' 0 to 9 allowed
keyascii = keyascii
Case 8
If keyascii = 8 Then Exit Function
Case 47 ' backslash
keyascii = keyascii
End Select
KeyPressed = vDate
End Function
and call it like so:
Private Sub txtDate_KeyPress(keyascii As Integer)
ActiveControl = KeyPressed(ActiveControl, keyascii)
End sub
This works for "t", but doesn't work for "22/2/2009". If I type "2" followed by "2", all I get in the textbox is "2"-the textbox only ever shows the last character typed. I can't simply type in a date.
This method works for me in Excel but not in Access. I suspect it has something to do with null values but can't see what is wrong with my method.
Can anyone help?
Stephen