Code Snippets



Private Sub CommandButton1_Click() 
    
   lNewColor = RGB(12, 12, 12)
   
'The MSForms library will be automatically added to the Tools > References
   Dim mscontrol As MSForms.Control
   Dim mstextbox As MSForms.TextBox

   For Each mscontrol In Me.Controls
'The next line will check type of the control
'In this case it is only looking for textboxes.
      If (TypeOf mscontrol Is MSForms.TextBox) Then
'The next line takes the "mscontrol" variable which refers to a 'generic' control
'and copies it to the "mstextbox" variable which has the datatype "MSForms.TextBox
'the reason for doing this copying/assigning is to give you full intellisense
         Set mstextbox = mscontrol

         mstextbox.BackColor = lNewColor
      End If
   Next mscontrol
   
'Me. - the object "Me" is a way to refer to the Userform object
'It can only be used in code modules that live behind userforms
   
'Me.Controls - is a way of referring to all the controls that exist on a Userform
   
'The "For Each" allows you to loop through all these controls
'every time a control is identified it is temporarily assigned to the "mstextbox" variable
   
'When you type "mstextbox" followed by a full stop
'you will see all the properties and methods that are specific to a textbox control
   
End Sub


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrev