1

I have a combobox control in a form made for an Excel application, using VBA. Let's suppose that the combobox is called cmb010. Next to it there are 2 other controls called txt011, cmb012 and txt013. These controls are inside the third page of a multipage control (which is the only such control in the whole form).

The control was generated dynamically in the Initialize event of the form, and was assigned the values "YES" and "NO" during its initialization. The other controls that I mention to you were generated in the same way.

What I want to do is, that if the value "NO" is selected in control cmb010, the controls txt011 and cmb012 must be disabled and the control txt013 must receive the focus.

I generated this code to perform the above actions:

Private Sub cmb010_Change()
 Dim response As String
 response = cmb010.Value
 With Me.MultiPage1.Pages(2) ' Page 3, index 2
 If response = "NO" Then
.Controls("txt011"). Enabled = False
.Controls("cmb012").Enabled = False
.Controls("txt013").SetFocus
 Else
.Controls("txt011").Enabled = True
.Controls("cmb012").Enabled = True
 End If
 End With
End Sub

Running this code does not give the response I expect. I click on the combobox called cmb010, select the item NO, but the controls are not disabled.

I have also tried other alternatives to find a solution, without success.

  • Change the Change() event to Click() event.
  • When initializing the ComboBox activate the Style property with the value fmStyleDropDownList.

I want to know what causes this behavior and how can I solve this problem. Thanks for your attention.

5
  • Have you debugged to make sure the code you posted is getting run, and that it runs in the way you expect? Commented Jul 24 at 21:46
  • 5
    If the control was generated dynamically in the Initialize event of the form, then you need to tie the event to it else it will not run. Here is an example Assign code to a button created dynamically Commented Jul 25 at 6:12
  • 3
    @SiddharthRout That's it. Carlos, use CheckBox instead of ComboBox if the binary value is in use. Commented Jul 25 at 6:46
  • Is txt013 a TextBox or a Label ? Because I guess that a Label doesn't accept a focus but I should have caused an error. Commented Jul 25 at 22:59
  • @rotabor Sorry, I can´t change controls. Due to design issues, the control must be a combobox. Commented Jul 29 at 22:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.