Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Code to Select all objects on a form 1

Status
Not open for further replies.

wbwillson

Technical User
Oct 7, 2002
52
0
0
GB
Is there a way to write a code to select every object on the current form? I want to be able to select every object on my form so that I can make them invisible and just turn on the fields and labels that I want. I could do this by hand by coding every field/label name but this takes tons of time and I thought maybe just maybe there is an easier way? Thanks for any suggestions and Merry Christmas to everyone!

Bill
 
Try this

Private Sub Form_Current()
Dim dbl_temp As Double

For dbl_temp = 0 To Form.Controls.Count - 1
If Form.Controls(dbl_temp).Name = "Option1" Then
MsgBox "You found it", vbCritical, "Happy face"
End If
Next dbl_temp
End Sub
 
Try using the Tag property of your controls to control your controls.

Public Function ControlMyControls()
Dim ctl As Control
For Each ctl In Me.Controls
With ctl
Select Case .ControlType
Case acTextBox
If .Tag = "Lock" Then 'this can say anything
.Locked = True
End If
End Select
End With
Next ctl
End Function


The example above looks at all text boxes on a form, if they're Tagged "Lock", all text boxes on the current form will be locked. You can manipulate all of a Form's control's properties in this way. .Enabled, .Visible etc.

Try it on a form's on load event.
 
Bill

Both the methods posted are the way to go but I have added my own way of doing it. I decide which controls are going to be shown under which circumstances then I prefix the name of them with something like "set1_", "set2" etc.

Then I can say:

Dim ctl as Control

For Each ctl in Me.Controls
If Left(ctl.Name, 4) = "set1" Then
ctl.Visible = True
End If
Next

No different really to those already posted but I could have changed the forecolor of set2, locked set3. The list is endless and you do it all in one loop. You could put the property value in a table and look it up. Have fun.
 
Thanks for everyone's response..I really appreciate it! I'll have lots of fun trying them out!

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top