0

Scenario

I have a userform whereby it has a Combobox with some options. There is a Textbox as well in the same userform. When I select a certain option in the combobox, I need the text box to be disabled as well as change background colour.

My Codes

Following are my codes. poType is the combobox name and unitPrice is the textbox name

Public Sub poType_Change()    
    If mainPage.poType.Value = "FOC" Then
        disabling (unitPrice)
    Else
        enabling (unitPrice)
    End If
End Sub

Following is the subroutines disabling and enabling

Sub disabling(ByVal objectToDisable As Object)
    objectToDisable.Enabled = False
    objectToDisable.BackColor = &H80000003
End Sub

Sub enabling(ByVal objectToEnable As Object)
    objectToEnable.Enabled = True
    objectToEnable.BackColor = &H80000005
End Sub

However, it is showing runtime error (424 object required) when I am executing this code. Anyone knows the reason?

7
  • Why is poType_Change public? What is mainPage? Is that the form? Commented Aug 4, 2018 at 6:53
  • poType_Change is public. mainPage is the userform where the combobox and textbox placed Commented Aug 4, 2018 at 6:55
  • 1
    Don’t use parentheses when calling a Sub Commented Aug 4, 2018 at 6:56
  • 1
    Right, remove the "( ...)". It will evaluate the object to the default and that is the value of the textbox. You should also replace mainPage with Me. Commented Aug 4, 2018 at 6:57
  • @TimWilliams Thank you. It solved my problem with the above code Commented Aug 4, 2018 at 7:04

1 Answer 1

2

Able to find rootcause of this problem. The above problem can be solved in two ways

Method 1

Need to add call when calling a subroutine

Public Sub poType_Change()    
    If mainPage.poType.Value = "FOC" Then
        call disabling (unitPrice)
    Else
        call enabling (unitPrice)
    End If
End Sub

Sub disabling(ByVal objectToDisable As Object)
    objectToDisable.Enabled = False
    objectToDisable.BackColor = &H80000003
End Sub

Sub enabling(ByVal objectToEnable As Object)
    objectToEnable.Enabled = True
    objectToEnable.BackColor = &H80000005
End Sub

Method 2

Don't use parenthesis for arguments. But for this case, don't add call in front

Public Sub poType_Change()    
    If mainPage.poType.Value = "FOC" Then
        disabling unitPrice
    Else
        enabling unitPrice
    End If
End Sub

Sub disabling(ByVal objectToDisable As Object)
    objectToDisable.Enabled = False
    objectToDisable.BackColor = &H80000003
End Sub

Sub enabling(ByVal objectToEnable As Object)
    objectToEnable.Enabled = True
    objectToEnable.BackColor = &H80000005
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend to also replace mainPage with Me!

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.