Full Version: Keypress
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
S. Williams
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
LPurvis
Rather than dipping into classes - you can turn the above on a tangent and not consider the return value you want to assign - the keypress alone on the form will handle that along with your earlier using of the ActiveControl method.

You could return the KeyAscii value from your function back to the calling event procedure.
When you want to overwrite the input then you return 0. Otherwise return the value you want to allow through.

i.e.

Private Sub txtDate_KeyPress(KeyAscii As Integer)

KeyAscii = KeyPressed(ActiveControl, KeyAscii)

End sub
S. Williams
Thank you for the reply. A quick attempt gets a "type mismatch" error. Further investigation required.
S. Williams
The type mismatch occurs when the function returns a value like "21/02/2009". keyascii can't be such a value.
Thanks anyway for trying to help.
Stephen
LPurvis
Just to explain (and expand upon) what I was meaning...
Your function would be..

CODE
Function KeyPressed(ByVal intKeyAscii As Integer)



    Select Case intKeyAscii

        Case 84, 116 ' "t" for today

            KeyPressed = 0

            Screen.ActiveControl = Format(Date, "dd/mm/yyyy")

        

        Case 8, 47, 48 To 57 ' 0 to 9 allowed

            KeyPressed = intKeyAscii

    

    End Select



End Function


Could be called such as...
CODE
Private Sub txtDate_KeyPress(KeyAscii As Integer)



    KeyAscii = KeyPressed(KeyAscii)

    

End Sub


Cheers.
S. Williams
That seems to work!

Thank you very much.

Stephen
LPurvis
No probs.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.