Consulting

Results 1 to 4 of 4

Thread: Solved: VBA Form Problem

  1. #1
    VBAX Regular
    Joined
    Jul 2009
    Posts
    37
    Location

    Solved: VBA Form Problem

    Hey guys,

    I am trying to write a code that will identify an invalid date format in the textbox, give the user a message and then go back to the date textbox and highlight the whole text. Here is my code:

    [VBA]Private Sub txtTransactionDateExp_AfterUpdate()
    With txtTransactionDate
    If IsDate(.Text) Then
    .Text = Format(.Text, "Short Date")
    Else
    MsgBox "Transaction Date format is incorrect. Standard format is mm/dd/yy."

    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
    End If
    End With
    End Sub[/VBA]

    My problem is that after the message box, the cursor just goes on to the next field (does not go back and highlight the date field).

    Please help ,
    Dimitriy

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    Try something on the following lines and see if it helps
    [VBA]Private Sub txtTransactionDate_Change()
    If Len(txtTransactionDate) > 7 Then
    With txtTransactionDate
    If IsDate(.Text) Then
    .Text = Format(.Text, "Short Date")
    Else
    MsgBox "Transaction Date format is incorrect. Standard format is mm/dd/yy."

    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
    End If
    End With
    End If
    End Sub[/VBA]
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You could put the OP code in the BeforeUpdate event and have Cancel be True if the entry is improperly formatted.

  4. #4
    VBAX Regular
    Joined
    Jul 2009
    Posts
    37
    Location
    Thanks, mikerickson! That did it!

Posting Permissions

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