keypress - "T" for 'today' in a date field and "N" for 'now' in time field

wiklendt

i recommend chocolate
Local time
Tomorrow, 10:39
Joined
Mar 10, 2008
Messages
1,746
keypress - "T" for 'today' in a date field and "N" for 'now' in time field

hey,

i want to make data entry more user friendly in my date and time fields. i want to create 'shortcut keys' to automatically add today's current date or the current time (depending on which field the user is in).

so i know access 2007 has a built-in calendar for date fields and there is a 'today' button on that pop-up calendar BUT i am maintaining a few databases that are being used in previous versions of access and so must WORK in previous versions of access.

i've tried a few things (see below) but my current code (in the control's "key down" event) looks like this:

Code:
Dim strToday As Date

strToday = Date

Select Case KeyCode
Case vbKeyT 'also tried (vbKeyN, 78) & (vbKeyT, 84) with KeyCode
    Me.txtPriceDate.Value = strToday
End Select
this has limited success. when i press "t", the current value is removed and "t" appears in the field rather than the current date. this of course triggers the 'value invalid' error as it's a date field.

any ideas? most of the posts on the forum relate to 'system' keys (like ENTER or SHIFT or CTRL or ALT) or complex combinations. i just want a regular letter to trigger an addition or update of data in a field. in the posts i've found with 'basic' keys capture like mine don't seem to have these errors mentioned...?

access help was unhelpful, of course. i think i may be having issues because i'm wanting to put a DATE into a DATE field using a TEXT key...?

of course, the fields must work to allow the user to add dates/times other than the current date/time. so, will this code (once i get it working) prevent the other keys from making any effect on the value?

appendix

i have tried these codes (on the control's keypress event) without success:

Code:
Private Sub txtPriceDate_KeyPress(KeyAscii As Integer) 'also tried KeyCode as Integer

Select Case KeyAscii
Case 78 'also tried vbKeyN & KeyCode
    Me.txtPriceDate.Value = Date
End Select

End Sub
Code:
Private Sub txtPriceDate_KeyPress(KeyAscii As Integer) 'also tried KeyCode as Integer

If KeyAscii = 78 Then 'also tried vbKeyN & KeyCode
    Me.txtPriceDate.Value = Date
Else
End If

End Sub
i have also tried with and without KeyPreview = Yes in the form properties.

but upon testing greeted with either these three scenarios:

1) the error message pictured below

attachment.php


2) "Compile error: End If without block If"

3) no error message, no desired effect in field BUT it does allow "n" to appear in the field, which of course triggers a "value entered isn't valid for this field" error as it's a date/time field.
 

Attachments

  • ExpressionError.jpg
    ExpressionError.jpg
    31.4 KB · Views: 755
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

I think this is already built into access - control colon inserts the current date if I'm not mistaken.
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

hm. thanks alisa, you're right, control colon inserts current date, however, if i already have a date in there, it does not remove old date first....

even so, i'd like to get this code to work as T and N are easier to remember than "CTRL + ;" (and just did an access help for what time might be, it's even more obscure, requiring "CTRL + SHIFT + :").

also, it's easier to instruct users to press just "t" or "n" than CTRL + SHIFT + : (you have no idea how many people don't realise that you need to PRESS AND HOLD these buttons IN THAT ORDER to make that work...).

pressing just "t" or just "n" would be much more user friendly, AND it reflects shortcuts in a system database we have at work already, so some people will be used to using those (T/N) shortcuts already.

in relation to a problem i mentioned was possible in my first post (i.e., will assigning an event to the keypress "T" prevent use of other keys), i believe this info MIGHT help.

but please don't stop with the suggestions!

thanks.
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

After you have detect the key "t", you have to set the KeyCode to 0 so not to raise conflict with date data type.
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

WIK,

i used hotkeys in a demo. it'll probably work for you too. try these steps:

1) put two buttons on the form. both visible=false
2) for the NOW() function, put the value &Now in the caption property of one button.
3) for TIME(), put &Time as the caption on the other button property.
4) here is example code to put behind the &Now button's click event:
Code:
me.textboxName = NULL
me.textboxName = NOW()
do the same thing for the other button, but use the TIME() function, and the other TBox in the code.

5) to change the NOW() TBox to show the actual value of the NOW() function, simply use ALT+N on your keyboard. Alternatively, ALT+T should work for the TIME() TBox.

that should do ya...

in relation to a problem i mentioned was possible in my first post (i.e., will assigning an event to the keypress "T" prevent use of other keys
No. why would it? you are just using ASCII to check the ID of the key that was pressed, that's all. that doesn't affect anything else
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

thanks adam, i'll try that out and let you know.
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

WIK,

i used hotkeys in a demo. it'll probably work for you too. try these steps:

1) put two buttons on the form. both visible=false
2) for the NOW() function, put the value &Now in the caption property of one button.
3) for TIME(), put &Time as the caption on the other button property.
4) here is example code to put behind the &Now button's click event:
Code:
me.textboxName = NULL
me.textboxName = NOW()
do the same thing for the other button, but use the TIME() function, and the other TBox in the code.

5) to change the NOW() TBox to show the actual value of the NOW() function, simply use ALT+N on your keyboard. Alternatively, ALT+T should work for the TIME() TBox.

that should do ya...

No. why would it? you are just using ASCII to check the ID of the key that was pressed, that's all. that doesn't affect anything else


wow! that's great :) nice work. i notice the ampersand in the caption is actually the letter that combines with ALT to trigger the command button - very clever!

what i've done is actually left the button visible, so that the user can choose to either use the mouse or keyboard :)
 
Re: keypress - "T" for 'today' in a date field and "N" for 'now' in time field

i'd like to add that i am fully aware that Access 2007 has a date picker for chosen date fields, but almost all of my db's at the moment are being used by non-2007 users, so it's very handy to have these little shortcuts for them :)

this command button version is VERY simple to implement, understand AND use - a winner for all! :)
 

Users who are viewing this thread

Back
Top Bottom