+ Reply to Thread
Results 1 to 2 of 2

Change date with KeyDown

  1. #1
    John Pierce
    Guest

    Change date with KeyDown

    The following code changes a date in a form text box like this:
    txtDate.Text = Format(Date, "mm/dd")
    with left and right arrows.
    Private Sub txtDate_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    ByVal Shift As Integer)
    Dim sDate As String
    Dim v As Integer, dte As Date
    Select Case KeyCode
    Case 37 ' Left
    v = -1
    Case 39 ' Right
    v = 1
    Case Else
    v = 0
    End Select
    sDate = txtDate.Text
    If InStr(sDate, ",") Then
    sDate = Trim(Right(sDate, Len(sDate) - InStr(sDate, ",")))
    End If
    If IsDate(sDate) And v <> 0 Then
    dte = CDate(sDate)
    dte = dte + v
    txtDate = Format(dte, "mm/dd")
    End If
    End Sub

    Can this be adapted to change a Time value formatted thus: txtTime.text
    = Format(Now(), "h:mm AM/PM") by 15 minute increments.


  2. #2
    Tom Ogilvy
    Guest

    Re: Change date with KeyDown

    This worked for me:

    Private Sub txtDate_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    ByVal Shift As Integer)
    Dim sDate As String
    Dim v As Integer, dte As Date
    Select Case KeyCode
    Case 37 ' Left
    v = -1
    Case 39 ' Right
    v = 1
    Case Else
    v = 0
    End Select
    sDate = txtDate.Text
    If InStr(sDate, ",") Then
    sDate = Trim(Right(sDate, Len(sDate) - InStr(sDate, ",")))
    End If
    If IsDate(sDate) And v <> 0 Then
    dte = CDate(sDate)
    dte = dte + v*TimeValue("00:15")
    txtDate = Format(dte, "hh:mm")
    End If
    End Sub

    --
    Regards,
    Tom Ogilvy


    "John Pierce" <johnscpierce@yahoo.com> wrote in message
    news:1131501851.801628.286740@g43g2000cwa.googlegroups.com...
    > The following code changes a date in a form text box like this:
    > txtDate.Text = Format(Date, "mm/dd")
    > with left and right arrows.
    > Private Sub txtDate_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
    > ByVal Shift As Integer)
    > Dim sDate As String
    > Dim v As Integer, dte As Date
    > Select Case KeyCode
    > Case 37 ' Left
    > v = -1
    > Case 39 ' Right
    > v = 1
    > Case Else
    > v = 0
    > End Select
    > sDate = txtDate.Text
    > If InStr(sDate, ",") Then
    > sDate = Trim(Right(sDate, Len(sDate) - InStr(sDate, ",")))
    > End If
    > If IsDate(sDate) And v <> 0 Then
    > dte = CDate(sDate)
    > dte = dte + v
    > txtDate = Format(dte, "mm/dd")
    > End If
    > End Sub
    >
    > Can this be adapted to change a Time value formatted thus: txtTime.text
    > = Format(Now(), "h:mm AM/PM") by 15 minute increments.
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1