Dan Waterloo
Well-known Member
- Joined
- Jan 4, 2007
- Messages
- 876
Here is a simplified version of my situation on an Excel VBA form. I have a checkbox and three text boxes. Let's call them Checkbox, Textbox1, Textbox2, and Textbox3. This is also their tab order.
Checkbox1 and TextBox1 are related by code:
1. When Checkbox1 is checked then Textbox1 is enabled by setting its background to opaque and the focus is placed on it.
2. When Checkbox1 is unchecked then the contents of Textbox1 are set to blank and is is disabled and its background set to transparent.
3. If the user exits Textbox1 while leaving it blank then Checkbox1 gets unchecked (and so the logic in the 2. above gets executed.)
The problem that I am having is that it seems to matter whether I exit TextBox1 by mouseclick or by <Tab> or <Enter>. <Tab> and <Enter> both behave the same way so I'll just refer to both as tab.
Here is the behavior I see:
1. If I go to Checkbox1, leave it unchecked, and press tab, the cursor goes to Textbox2, as expected.
2. If I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter non-blank input and exit the textbox by mouseclick or tab, then Textbox1 will have the valid value in it. If I mouseclicked out then the control I clicked on will have focus. But if I tabbed out then TextBox2 will have focus. This is all as expected.
3. If I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter blank input or do nothing at all and thereby leave it blank and then exit by mouseclick, then Checkbox1 becomes unclicked, Textbox1 has its background changed to opaque, and the focus goes to Textbox3. This is what I expect to happen.
3. However, if I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter blank input or do nothing at all and thereby leave it blank and then exit by TAB, then Checkbox1 becomes unclicked, Textbox1 has its background changed to opaque, and the focus goes to Textbox4. This is NOT what I expect to happen.
Help would be appreciated. Code for the two controls follows below.
Checkbox1 and TextBox1 are related by code:
1. When Checkbox1 is checked then Textbox1 is enabled by setting its background to opaque and the focus is placed on it.
2. When Checkbox1 is unchecked then the contents of Textbox1 are set to blank and is is disabled and its background set to transparent.
3. If the user exits Textbox1 while leaving it blank then Checkbox1 gets unchecked (and so the logic in the 2. above gets executed.)
The problem that I am having is that it seems to matter whether I exit TextBox1 by mouseclick or by <Tab> or <Enter>. <Tab> and <Enter> both behave the same way so I'll just refer to both as tab.
Here is the behavior I see:
1. If I go to Checkbox1, leave it unchecked, and press tab, the cursor goes to Textbox2, as expected.
2. If I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter non-blank input and exit the textbox by mouseclick or tab, then Textbox1 will have the valid value in it. If I mouseclicked out then the control I clicked on will have focus. But if I tabbed out then TextBox2 will have focus. This is all as expected.
3. If I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter blank input or do nothing at all and thereby leave it blank and then exit by mouseclick, then Checkbox1 becomes unclicked, Textbox1 has its background changed to opaque, and the focus goes to Textbox3. This is what I expect to happen.
3. However, if I go to Checkbox 1 and check it then I end up in Textbox 1 as expected. If I now enter blank input or do nothing at all and thereby leave it blank and then exit by TAB, then Checkbox1 becomes unclicked, Textbox1 has its background changed to opaque, and the focus goes to Textbox4. This is NOT what I expect to happen.
Help would be appreciated. Code for the two controls follows below.
VBA Code:
Private Sub CHECKBOX1_Change()
Dim isChecked As Boolean
With myForm
isChecked = .CHECKBOX1.Value
.LABEL_TEXT1.Enabled = isChecked
.TEXT_TEXT1.Enabled = isChecked
If isChecked Then
.TEXT1.BackStyle = fmBackStyleOpaque
.TEXT1.SetFocus
Else
.TEXT1.BackStyle = fmBackStyleTransparent
.TEXT1.Value = ""
End With
End Sub
VBA Code:
Private Sub TEXT1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(TEXT1) = "" Then CHECKBOX1.Value = False
End Sub