Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2004
    Location
    Grand Junction, Colorado
    Posts
    12

    Unanswered: Key Down adding/subtracting days to date

    I think I'm close but as usual I can't get something...

    When I'm in my field PtDate I want to push the "Plus Key" to add a day to the date and the "Minus Key" to subtract a day, which it is doing but you have to esc. to keep it from trying to save "-" or "+" in the field.

    Thanks for your help!
    John


    Code:
    Private Sub PtDate_KeyDown(KeyCode As Integer, Shift As Integer)
     Dim varNewValue As Date
    Dim varBaseValue As Date    ' Declare variables.
    Dim IntervalType As String
    Dim Number As Integer
    
    IntervalType = "d"    ' "d" specifies days as interval.
    varBaseValue = Me.PtDate.Value
    
     Select Case KeyCode
            Case vbKeySubtract
    
    Number = -1
    varNewValue = "" & DateAdd(IntervalType, Number, varBaseValue)
    
    Me.PtDate.Value = varNewValue
    
    Me.PtDate.SetFocus
            Case vbKeyAdd
            Number = 1
    varNewValue = "" & DateAdd(IntervalType, Number, varBaseValue)
    
    Me.PtDate.Value = varNewValue
    
    Me.PtDate.SetFocus
    
    
    End Select
    
    End Sub

  2. #2
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    Try (with SomeDate being the name of the control containing a date):
    Code:
    Private Sub SomeDate_KeyPress(KeyAscii As Integer)
    
        Dim x As Long
        
        If Not IsNull(Me.SomeDate) Then
            Select Case KeyAscii
                Case 43:    x = 1   ' Plus key
                Case 45:    x = -1  ' Minus key
            End Select
            If x <> 0 Then
                Me.SomeDate.value = DateAdd("d", x, Me.SomeDate.value)
                KeyAscii = 0
            End If
        End If
        
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Oct 2004
    Location
    Grand Junction, Colorado
    Posts
    12
    Sinndho,

    That worked perfect!

    I wish I understood why this worked and not the KeyDown...

    As always I appreciate the help!

    Thanks,
    John

  4. #4
    Join Date
    Mar 2009
    Posts
    5,441
    Provided Answers: 14
    Quote Originally Posted by drunkbus77 View Post
    I wish I understood why this worked and not the KeyDown...
    Mainly because you can set the received value to zero:
    Code:
    KeyAscii = 0
    wich prevents it from being transmitted further on.

    You're welcome!
    Have a nice day!

Posting Permissions

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