A common question that I am frequently asked is how to iterate through controls on a SalesLogix form. This is helpful to be able to generically manipulate all, or a set of controls on a form, to do things such as enable/disable controls at runtime etc without the need to specifically hard-code the names of the controls.
Here’s an example. This code iterates through all controls and sets the enabled property to false:
Dim i On Error Resume Next For i = 0 To ControlCount - 1 Controls(i).Enabled = False Next
Easy enough. The SalesLogix form exposed two properties that make this possible. First the ControlCount property will return the number of controls on the form. The Controls collection contains a reference to each of these controls. Note: SalesLogix collections don’t work well with For Each loops. Stick with a For loop instead and access collection items by index instead.
You can do anything you want with the control reference in the loop. Finding out which type of control you’re working with is easy with the TypeName method. If you pass to it a control reference it will pass back the type of control it is. Here’s an example that sets only checkboxes disabled:
Dim i On Error Resume Next For i = 0 To ControlCount - 1 If TypeName(Controls(i)) = "CheckBox" Then Controls(i).Enabled = False Next
Access whatever properties you need once you have the control reference. Often it is useful to use the Tag property to flag controls needed for some specific operation. However, be careful to not access a property while iterating through controls that might not exist on all controls (such as the Text property).