How to disable/enable a textbox, from Combobox

jhazziejhazz

New Member
Joined
Mar 11, 2021
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
I have this user form, with various textboxes, and my goal is to disable some these textbox.

I have a combobox with options "Inventory In" and "Inventory Out" - I want to disable some textboxes whenever I pick "inventory in".

Please check attached image.

Thanks you everyone.

Jhazzie
 

Attachments

  • 0.png
    0.png
    167 KB · Views: 59

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
try

VBA Code:
sub abc()
if combobox1.value = "Inventory In" then 
textbox1.enabled = false
textbox2.enabled = false
textbox3.enabled = false
....
....
end if
end sub

where combobox1 is your transaction type and textbox1, 2, 3, .... are your Inventory out textboxes.
Rename your controls with reference to your userform....



hth...
 
Upvote 0
May be
VBA Code:
Private Sub ComboBox1_Change()
    Select Case Me.ComboBox1.Value
    Case "Inventory In" 
        TextBox1.Enabled = False
    Case "Inventory Out" 
        TextBox1.Enabled = True
    End If
End Sub
 
Upvote 0
Hi again
a typo in the previous code
Correction
VBA Code:
Private Sub ComboBox1_Change()
    Select Case Me.ComboBox1.Value
    Case "Inventory In"
        TextBox1.Enabled = False
    Case "Inventory Out"
        TextBox1.Enabled = True
    End Select
End Sub
 
Upvote 0
try

VBA Code:
sub abc()
if combobox1.value = "Inventory In" then
textbox1.enabled = false
textbox2.enabled = false
textbox3.enabled = false
....
....
end if
end sub

where combobox1 is your transaction type and textbox1, 2, 3, .... are your Inventory out textboxes.
Rename your controls with reference to your userform....



hth...
Hi Mohadin,

Thank you for your quick response. The code did work just fine, just that when I pick inventory IN, it did disable the textboxes I want to disable (cmbCustName, txtSelling and the rest), however if I switch the transaction type to "Inventory Out" the disabled textboxes such as cbmCustName, txtSelling doesn not go back to being enable again. Is there any trick to fix this.
(please checked attached image)
Thanks again

Here are the codes I used.


Private Sub cmbTransType_Change()

If Me.cmbTransType.Value = "Inventory In" Then

Me.cmbCustName.Enabled = False
Me.txtSelling.Enabled = False
Me.txtQty.Enabled = False
Me.txtDate.Enabled = False
Me.txtProforma.Enabled = False
Me.txtPONo.Enabled = False

Me.cmbCustName.BackColor = 16777215
End If

If Me.cmbTransType.Value = "Inventory Out" Then

Me.txtRRNo.Enabled = False
Me.cmbOrderType.Enabled = False
Me.txtTransdate.Enabled = False
Me.txt_Qty.Enabled = False
Me.txt_Cost.Enabled = False
Me.txtSupplierInvoice.Enabled = False
End If

End Sub
 

Attachments

  • INVENTORY IN-OUT ERROR.png
    INVENTORY IN-OUT ERROR.png
    163.3 KB · Views: 9
  • INVENTORY IN-OUT ERROR1.png
    INVENTORY IN-OUT ERROR1.png
    175 KB · Views: 9
Upvote 0
Hi
you are addressing me and showing other code
Any way try
VBA Code:
Private Sub cmbTransType_Change()
    Select Case Me.cmbTransType.Value
    Case "Inventory In"
        Me.cmbCustName.Enabled = False
        Me.txtSelling.Enabled = False
        Me.txtQty.Enabled = False
        Me.txtDate.Enabled = False
        Me.txtProforma.Enabled = False
        Me.txtPONo.Enabled = False
        Me.cmbCustName.BackColor = 16777215
    Case "Inventory Out"
        Me.cmbCustName.Enabled = True
        Me.txtSelling.Enabled = True
        Me.txtQty.Enabled = True
        Me.txtDate.Enabled = True
        Me.txtProforma.Enabled = True
        Me.txtPONo.Enabled = True
        Me.cmbCustName.BackColor = &H80000005
    End Select
End Sub

And for your original code should be
VBA Code:
Private Sub cmbTransType_Change()
    If Me.cmbTransType.Value = "Inventory In" Then
        Me.cmbCustName.Enabled = False
        Me.txtSelling.Enabled = False
        Me.txtQty.Enabled = False
        Me.txtDate.Enabled = False
        Me.txtProforma.Enabled = False
        Me.txtPONo.Enabled = False
        Me.cmbCustName.BackColor = 16777215
    End If
    If Me.cmbTransType.Value = "Inventory Out" Then
        Me.txtRRNo.Enabled = True
        Me.cmbOrderType.Enabled = True
        Me.txtTransdate.Enabled = True
        Me.txt_Qty.Enabled = True
        Me.txt_Cost.Enabled = True
        Me.txtSupplierInvoice.Enabled = True
        Me.cmbCustName.BackColor = &HFFFFFF
    End If
End Sub
 
Upvote 0
Solution
I want to disable (cmbCustName, txtSelling and the rest), however if I switch the transaction type to "Inventory Out" the disabled textboxes such as cbmCustName, txtSelling doesn not go back to being enable again. Is there any trick to fix this.

Hi,
untested but try this update to your code & see if helps

VBA Code:
Private Sub cmbTransType_Change()
    Dim i       As Integer
    Dim State   As Boolean
    State = Me.cmbTransType.Value Like "*Out"
    For i = 1 To 6
        Choose(i, Me.cmbCustName, Me.txtSelling, Me.txtQty, Me.txtDate, Me.txtProforma, Me.txtPONo).Enabled = State
    Next i
    Me.cmbCustName.BackColor = IIf(State, &H80000005, 16777215)
End Sub

Dave
 
Upvote 0
Its working.
Thank you Mohadin.
You're amazing.
Hi
you are addressing me and showing other code
Any way try
VBA Code:
Private Sub cmbTransType_Change()
    Select Case Me.cmbTransType.Value
    Case "Inventory In"
        Me.cmbCustName.Enabled = False
        Me.txtSelling.Enabled = False
        Me.txtQty.Enabled = False
        Me.txtDate.Enabled = False
        Me.txtProforma.Enabled = False
        Me.txtPONo.Enabled = False
        Me.cmbCustName.BackColor = 16777215
    Case "Inventory Out"
        Me.cmbCustName.Enabled = True
        Me.txtSelling.Enabled = True
        Me.txtQty.Enabled = True
        Me.txtDate.Enabled = True
        Me.txtProforma.Enabled = True
        Me.txtPONo.Enabled = True
        Me.cmbCustName.BackColor = &H80000005
    End Select
End Sub

And for your original code should be
VBA Code:
Private Sub cmbTransType_Change()
    If Me.cmbTransType.Value = "Inventory In" Then
        Me.cmbCustName.Enabled = False
        Me.txtSelling.Enabled = False
        Me.txtQty.Enabled = False
        Me.txtDate.Enabled = False
        Me.txtProforma.Enabled = False
        Me.txtPONo.Enabled = False
        Me.cmbCustName.BackColor = 16777215
    End If
    If Me.cmbTransType.Value = "Inventory Out" Then
        Me.txtRRNo.Enabled = True
        Me.cmbOrderType.Enabled = True
        Me.txtTransdate.Enabled = True
        Me.txt_Qty.Enabled = True
        Me.txt_Cost.Enabled = True
        Me.txtSupplierInvoice.Enabled = True
        Me.cmbCustName.BackColor = &HFFFFFF
    End If
End Sub
 
Upvote 0
Hi,
untested but try this update to your code & see if helps

VBA Code:
Private Sub cmbTransType_Change()
    Dim i       As Integer
    Dim State   As Boolean
    State = Me.cmbTransType.Value Like "*Out"
    For i = 1 To 6
        Choose(i, Me.cmbCustName, Me.txtSelling, Me.txtQty, Me.txtDate, Me.txtProforma, Me.txtPONo).Enabled = State
    Next i
    Me.cmbCustName.BackColor = IIf(State, &H80000005, 16777215)
End Sub

Dave
Hi dave,

Thanks again for your reply.

The code is working too. You guys are so amazing.


Jhazzie
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top