Example of Linking Multiple Controls?

djr8yk

Board Regular
Joined
May 19, 2015
Messages
58
I asked a question yesterday that was similar, but it was too long winded and, I think, asked for too much. So today I come back again asking for less.

I want to be able to create a form that links multiple controls (>2). For instance, I want to have a CommandButton that will not enable until several CheckBoxes have been checked and a TextBox has been written in. I don't necessarily need this example exactly, but I would really appreciate if somebody could give me an example of linking multiple controls. The only thing I ask of this example is to include either a TextBox or a ComboBox, because I feel like that might be the root of my issue.

For my actual project, I'm trying to have a required ComboBox, TextBox, two CheckBoxes in order to click a CommandButton. But no need to discuss that now, just providing context.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Let's assume you have a CommandButton with the name "Btn_OK" and by default you have the Enabled property set to "False" at design time.

When I develop code like this I typically set up a routine that just scans every control and picks out the one's where I set a Tag property (in this case "Required") to see if some condition is true. This lets me add new controls to the form with a "Required" Tag value and the code picks it up. Granted some might say "not efficient!" but I'm operating under the assumption that my Userforms tend to have relatively few controls to step through.

So given that... the question is, which event procedure of the form would best suit the purpose here. I think, MouseMove might be a safe bet.

So the code always starts with the Boolean variable set to True, it then checks each control that is "Required", and based on the control type checks to see if a valid entry has been made. If any control has an invalid entry, the Boolean value is set to false and the OK_Btn is set to the Boolean value.

I stick in the Case Else line as a catchall in the event that I add a control type later which I have no set Case for. This would alert me to the situation and let me know the type control I should build a new case for in my code.

Code:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim BtnEnabled As Boolean
    BtnEnabled = True
    
    Dim ctrl As control
    For Each ctrl In Me.Controls
        If ctrl.Tag = "Required" Then
            Select Case TypeName(ctrl)
                Case "TextBox"
                    If BtnEnabled And ctrl.Value = "" Then BtnEnabled = False
                Case "CheckBox", "OptionButton"
                    If BtnEnabled And ctrl.Value = False Then BtnEnabled = False
                Case Else
                    MsgBox TypeName(ctrl)
            End Select
        End If
    Next ctrl
    
    Btn_OK.Enabled = BtnEnabled
    
End Sub
 
Upvote 0
Thank you so much, this is EXACTLY what I was looking for. I think I went a very roundabout way and ended up putting too much in my code. Also, the "required" tags are going to be incredibly useful for me in my specific project. I never knew how to use the "Tag" property, so that's very useful moving forward.
Thanks Aaron!
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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