PDA

View Full Version : Solved: VBA Form Problem



Dimitriy
11-19-2010, 03:25 PM
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:

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

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

shrivallabha
11-20-2010, 03:10 AM
Try something on the following lines and see if it helps
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

mikerickson
11-20-2010, 08:22 AM
You could put the OP code in the BeforeUpdate event and have Cancel be True if the entry is improperly formatted.

Dimitriy
11-20-2010, 05:59 PM
Thanks, mikerickson! That did it!:clap: