-
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 :help ,
Dimitriy
-
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]
-
You could put the OP code in the BeforeUpdate event and have Cancel be True if the entry is improperly formatted.
-
Thanks, mikerickson! That did it!:clap: