Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date Field Hot Keys? (A2k (9.0.3821) SR-1)

    Trying to implement Use the KeyPress Event to Implement Hot Keys as discussed in Access-VB-SQL Advisor Tips September 2002 page 50

    Private Sub dtm1stInspDate_KeyPress(KeyAscii As Integer)
    KeyAscii = PickNewDate(KeyAscii, Me.RequiredDate)
    End Sub

    Getting "Run-time Error 91 Object Variable or with block variable not set" message with above line highlighted:

    RequiredDate is my date field.

    See attached db, frmOrders, tab through Receive Date.

    Any clues?

    Thanks, John
    -----------------

    ADVISOR TIPS
    Use Microsoft Access' KeyPress Event to Implement Hot Keys

    This clever solution to a common Access programming problem gives users simple shortcuts for entering and modifying date values.
    By Paul Overway

    Many applications provide simple shortcuts for entering and modifying date values. To keep users happy with my Microsoft Access applications, I provide the same thing.
    For controls that accept a date as input, I use the KeyPress event to implement hotkeys. For example, pressing "T" or "t" inserts today's date, and "+" or "-" increments and decrements the date. Pressing "H" increments the hour value, and "h" decrements it. Pressing ">" increments the minute value, and "<" decrements it. You can use almost any keyboard character as a hotkey to provide the same functionality.

    Here's the code:

    Public Function PickNewDate( _
    KeyAscii As Integer, ActiveControl As Control) _
    As Control
    'Parameters:
    'KeyAscii: Integer, the Ascii value of the
    'key pressed
    'ActiveControl: Control, control that will
    'receive the new value
    'Return value:
    'Integer; 0 if the control was assigned a new date,
    'the value passed for KeyAscii if not.

    On Error Resume Next

    'Check for value in control
    If IsNull(ActiveControl) Then
    Select Case KeyAscii
    Case 116, 84, 43, 45, 72, 104, 62, 60
    ActiveControl = Date
    PickNewDate = 0
    Case Else
    PickNewDate = KeyAscii
    End Select
    ElseIf IsDate(ActiveControl) Then
    'Has value
    'Set date and time based on which special key
    'was pressed
    Select Case KeyAscii
    Case 116, 84 'T/t
    ActiveControl = Date
    PickNewDate = 0
    Case 43 '+
    ActiveControl = ActiveControl + 1
    PickNewDate = 0
    Case 45 '-
    ActiveControl = ActiveControl - 1
    PickNewDate = 0
    Case 72 'H
    ActiveControl = ActiveControl + (1 / 24)
    PickNewDate = 0
    Case 104 'h
    ActiveControl = ActiveControl - (1 / 24)
    PickNewDate = 0
    Case 62 '>
    ActiveControl = ActiveControl + (1 / 1440)
    PickNewDate = 0
    Case 60 '<
    ActiveControl = ActiveControl - (1 / 1440)
    PickNewDate = 0
    Case Else
    PickNewDate = KeyAscii
    End Select
    Else
    PickNewDate = KeyAscii
    End If
    End Function

    To call the procedure, I add code like this to the control's KeyPress event:

    Private Sub Text0_KeyPress(KeyAscii As Integer)
    KeyAscii = PickNewDate(KeyAscii, Me.Text0)
    End Sub

    -- Paul Overway, Holland, Michigan
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Date Field Hot Keys? (A2k (9.0.3821) SR-1)

    John,

    The return value of the function should be of type Integer, not of type Control:

    Public Function PickNewDate(KeyAscii As Integer, ActiveControl As Control) As Integer

    instead of ... As Control.

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Date Field Hot Keys? (A2k (9.0.3821) SR-1)

    Hi Hans

    Great job!

    Thanks, John

    PS: I will let Ken Getz know you proof read the article

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •