SetFocus problem

C

cesw

Hi - we are using Excel 2000 VBA

When an error occurs, I want to put up a message box and then put th
cursor in the incorrect textbox on my form.
Here is the code - comments to the left indicate what happened when
stepped through the code with the debugger:

Private Sub tbEnd_AfterUpdate()
If tbEnd <> "" Then
If Not isValidDate(tbEnd) Then
MsgBox ("End Date Invalid") ' displays fine, use
clicks ok
ckbTTM.SetFocus ' pgm continues, an
focus is set here
ckbCustID.SetFocus ' pgm cont, and focu
is set here
tbEnd.SetFocus ' pgm cont, focu
stays ckbCustID
End If
End If
End Sub ' return t
form and focus is on OK button

Private Sub tbStart_AfterUpdate()
If tbStart = "" Then ' user enter
incorrect date and tabs
MsgBox ("Start Date Required") ' msgbox works fine
Me.tbStart.SetFocus ' focus is on next Ta
stop
Exit Sub
End If
If Not isValidDate(tbStart) Then
MsgBox ("Start Date Invalid")
tbStart.SetFocus ' same thing
End If
End Sub

Any thoughts?
Th
 
G

Gareth

Might I suggest a different approach. If you use the Exit event you can
just set cancel to True - this will prevent the user exiting the textbox
altogether - rather than having them leave and programmatically
returning them.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

If Textbox1 = "" then
cancel = true
end if

End Sub

HTH,
Gareth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top