code entry as a date

K

KarenY

Hi, I have learned from here to use a code entry as a date. It works out
great and fast for the users to input the date on a form. I have set up for
a maximum of 8 days (8 keys), however some users may be out for a long
vacation, the date-field will be input manually. I don't know how to make
this field for both possiblities, i.e. it can be input through a key or
manually input the date.

Because of the following setting, the users can't input the date manually in
the field.
Please help what should I add the "extra" :

Private Sub MyField_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 '0
KeyAscii = 0
Screen.ActiveControl = Date

Case 49 '1
KeyAscii = 0
Screen.ActiveControl = Date - 1

End Select

I have set up up to the Case 55 for Date - 6, etc.
Please help to add an extra possibility to allow a manual date input in
MyField.

thanks
Karen
 
D

Duane Hookom

I use a generic function with a calendar type form and lots of keypress code
Option Compare Database
Option Explicit
Global datCalDate As Variant
Const KEY_PLUS = 43, KEY_EQUALS = 61
Const KEY_MINUS = 45, KEY_UNDERSCORE = 95
Const KEY_UM = 77, KEY_LM = 109
Const KEY_UW = 87, KEY_LW = 119
Const KEY_UY = 89, KEY_LY = 121
Const KEY_UT = 84, KEY_LT = 116
Const KEY_UC = 67, KEY_LC = 99
Const KEY_UH = 72, KEY_LH = 104
Const KEY_UD = 68, KEY_LD = 100
Global gdCalendarDate As Date

Function DateKeyDown(pintKeyAscii) As Integer
'to use this, put the following code in the KeyPress event
' of a date control NOT KeyDown as the name implies
'Private Sub cboStartDate_KeyPress(KeyAscii As Integer)
' KeyAscii = DateKeyDown(KeyAscii)
'End Sub
Dim ctlDate As Control
Dim datStart As Date
Set ctlDate = Screen.ActiveControl
DateKeyDown = 0
If Not IsDate(ctlDate.Text) Then
datStart = Date
Else
datStart = CVDate(ctlDate.Text)
End If
Select Case pintKeyAscii
Case KEY_EQUALS, KEY_PLUS, KEY_UD
ctlDate = datStart + 1
Case KEY_MINUS, KEY_UNDERSCORE, KEY_LD
ctlDate = datStart - 1
Case KEY_UT
ctlDate = datStart + 10
Case KEY_LT
ctlDate = datStart - 10
Case KEY_UW
ctlDate = datStart + 7
Case KEY_LW
ctlDate = datStart - 7
Case KEY_UM
ctlDate = DateAdd("m", 1, datStart)
Case KEY_LM
ctlDate = DateAdd("m", -1, datStart)
Case KEY_UY
ctlDate = DateAdd("YYYY", 1, datStart)
Case KEY_LY
ctlDate = DateAdd("YYYY", -1, datStart)
Case KEY_UC, KEY_LC
datCalDate = datStart
gdCalendarDate = datStart
'requires a form named "calendarsub"
DoCmd.OpenForm "Calendarsub", acNormal, , , , acDialog
'before the form closes, it sets the new datCalDate
ctlDate = datCalDate
ctlDate = gdCalendarDate
Case KEY_UH, KEY_LH
'CalHelp just opens a message box with options
Call CalHelp
Case 65 To 90, 97 To 122
'filter out remaining letter keys
' could filter others
Case Else
DateKeyDown = pintKeyAscii
End Select
End Function
 
D

Duane Hookom

My code was much the same as your code. I added more Case statements to
handle pressing the + or - keys, w to add or subtract a week, m to add or
subtract a month, y to add or subtract a year, t for ten days,...

I didn't want to repeat this same code for every date text box on every
form, so I added the function to a standard module and call it from the
KeyPress event of the control. Each control has the same one line of code
rather than a ton of code for each control.
 
K

KarenY

Thanks for your answer.
I am sorry this is far beyond my understanding. I will keey this in my
file and try it out. At this moment, I have to use a very simple one.

thanks again
karen
 
K

KarenY

I sure will try, Douane, thanks for the kind thoughts.

It's because I am out for vacation next week, I haven't got enough time to
test out for it. It's my own fault, running out of time. As I said, I have
already saved it in my file and from what you explained, it should be good.

Again, thanks for your help,
Karen
 
K

KarenY

I see. thanks for explaining. I am sure yours is a very good programming
stuff, it's just my problem that I am not good in the module, I couldn't
figure out how to handle that.
I am sure I will try this out.
Karen
 
D

Duane Hookom

Try to add more Case statements to handle other key press values. Try add and
test one at a time. If you have questions, come on back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top