Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate date on key press

Status
Not open for further replies.

jeffshex

Technical User
Jun 30, 2005
208
US
Has anyone ever tried populating a date field on a form when a certain key is pressed. For example, if I press "t", then it puts in today's date.
I have a way to do it, but I don't like it and I'm looking for advice to better this.
Here is the code the form field calls:
Code:
Private Sub ProgramStartDate_KeyUp(KeyCode As Integer, Shift As Integer)
If IsNull(GetHotDate(KeyCode)) Or GetHotDate(KeyCode) = #12:00:00 AM# Then
    Exit Sub
Else
    Me.ProgramStartDate = GetHotDate(KeyCode)
End If
End Sub
Here is the public function in my module:
Code:
Public Function GetHotDate(intK As Integer) As Date
Select Case intK
    Case 84
        'todays date
        GetHotDate = Date
    Case 89
        'yesterdays date
        GetHotDate = Date - 1
    Case 87
        'tomorrows date
        GetHotDate = Date + 1
End Select
End Function

It would be nice to have this apply to all controls that have a date format, but that's beyond me.
I also have to watch out for only certain keys. That's why in the key up event I'm watching for null and that #12:00:00 AM# value. If I don't have that statement, any other key press will return back 12/30/1899.

Any advice or input would be fantastic!
Thanks!
 
In the Keydown event procedure of your control try this.

Dim strKey As String
strKey = Chr(KeyCode)
If KeyCode = 27 Then
KeyCode = 0
End If
Select Case KeyCode

Case vbKeyF2
Me.ControlName = VBA.Date
End Select

 
Can you help explain what's going on in that code?
Not sure what it is supposed to do and why you are declaring strKey and never using it.
Thanks again!
 
Actually I copied it from a control that the code did several things and only included this part.

Basically in your control if you click the F2 key the current date will be written.
 
I guess as I work on this I'm trying to make it a public funtion.
I would like to incorporate looping through the controls and check to see if they are a text box with a format property of short date like this:
Code:
Private Sub Form_Current()
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
    Case acTextBox
        If ctl.Format = "Short Date" Then
        ctl.BackColor = 16769734
        End If
    End Select
Next ctl
End Sub
As you can see, I'm doing that at the form current event.
Would it be possible to put this loop with my get date public funtion?

That way I can make it cleaner to call that function to all textboxes that have that format property.
Just curious.
Thanks!
 
As I think more about this I don't think I can include the two in one function.
It would be neat still though to have that functionality passed down to those controls.
Like Access 2007's date picker that only shows up on controls that have a date format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top