MACRO LIBRARY

Enable command button depending on combo and text box state

Free public access. No account or subscription required.

Files

Enable command button depending on combo and text box state

Code

Module: mMain

'This macro demonstrates how to enable or disable a command button depending on the current _
    values of text and combo boxes. This can be controlled from the user form module or from _
    a separate module. Modify the commenting depending on whether you want the control _
    properties to be controlled from UserForm1 or mModule.
    
'Preconditions: none
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    UserForm1.Show
End Sub

Module: mModule

Public Sub CommandEnabler()
    If UserForm1.cbo1.Value <> Empty And UserForm1.cbo2.Value <> Empty And UserForm1.txt1.Text <> Empty And UserForm1.txt2.Text <> Empty Then
        UserForm1.cmdRun.Enabled = True
    Else
        UserForm1.cmdRun.Enabled = False
    End If
End Sub

User Form: UserForm1

Private Sub cbo1_Change()
    Call mModule.CommandEnabler
    'Call CommandEnabler
End Sub
    
Private Sub cbo2_Change()
    Call mModule.CommandEnabler
    'Call CommandEnabler
End Sub
    
Private Sub cmdRun_Click()
    
End Sub
    
Private Sub txt1_Change()
    Call mModule.CommandEnabler
    'Call CommandEnabler
End Sub
    
Private Sub txt2_Change()
    Call mModule.CommandEnabler
    'Call CommandEnabler
End Sub
    
Private Sub UserForm_Initialize()
    cmdRun.Enabled = False
    cbo1.AddItem Empty
    cbo1.AddItem "text"
    cbo2.AddItem Empty
    cbo2.AddItem "text"
End Sub
    
'Private Sub CommandEnabler()
'    If cbo1.Value <> Empty And cbo2.Value <> Empty And txt1.Text <> Empty And txt2.Text <> Empty Then
'        cmdRun.Enabled = True
'    Else
'        cmdRun.Enabled = False
'    End If
'End Sub