Textbox Validation not retaining focus

T

tdw

Hi There!

I've been beating my head against the wall trying to get this to work. If
you could help, I would greatly appreciate it.

The task seems simple enough - I would like to validate the text in a
textbox before allowing the user to move to another control. One would think
that, as indicated in the documentation, by setting the Cancel parameter to
True in either the Exit or BeforeUpdate event that the control would retain
the focus.

This works if the user presses tab to exit the textbox. It also works if the
user clicks on any another control on the form EXCEPT if he/she clicks on the
listbox on the form.

If the user clicks on the listbox, then the error message is displayed twice
and the focus transfers to the listbox. This begs the question why a single
click on the listbox would cause the BeforeUpdate event of the textbox to
fire twice? Further, why is the Cancel = True for the textbox being ignored?

I'm guessing that either (i) I'm not fully understanding the sequence of
events and therefore missing something, or (ii) there's an issue with the
Cancel parameter in this specific sequence that requires a workaround.
Unfortunately, I've not been able to find any indication to confirm either
assumption. I've also been unable to create an acceptable workaround.

The following is the code from my BeforeUpdate event. I originally had it in
the Exit event but changed it when I realized that BeforeUpdate fires first.
Unfortunately, it has the same result in either event. Further, I've
currently declared isValidTabName at the form/module level so that it can be
checked in other routines. For example, I tried checking it in the listbox's
Enter event to prevent the change in focus. It didn't make any difference.

Private Sub txtTabName_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Dim sErrorMsg As String

'If the user enters an invalid tab name, then we want the focus to remain
'in txtTabName until they have entered a valid name.
'Unfortunately, if the mouse is used to select a different item in the
'list box, then the fact that Cancel is set to True is ignored and the
'focus will still transfer. This results in the BeforeUpdate event firing
'twice and the error message being displayed twice. The same behaviour is
'observed in the Exit event.
Call ValidateTabName(txtTabName.Text, isValidTabName, sErrorMsg)
If Not isValidTabName Then
MsgBox sErrorMsg, vbCritical + vbOKOnly, "Invalid Tab Name"
Cancel = True
End If

End Sub


Thank you in advance for any direction, help, suggestions you may be able to
provide.

tdw

--
Timothy D. White
Contract Programmer
Ontario, Canada

<my initials>hite<at>sympatico<dot><countryCode>
 

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