Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 initialshite<atsympatico<dot<countryCode |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Retaining focus on a textbox / listbox item pair after validation = false. | Excel Programming | |||
How to tell what textbox got the focus | Excel Programming | |||
Retaining value of a string from a textbox on a form ?? | Excel Programming | |||
textbox focus | Excel Programming | |||
Retaining text formatting in TextBox | Excel Programming |