By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,327 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,327 IT Pros & Developers. It's quick & easy.

Looping through controls and setting property

P: 14
Hi,

I have a form with 10 labels named (label1 to Label10). Is there a way where i can loop through these controls and set their background colors using vba?

It very lengthy to write 10 lines to set the colors each time a user selects a different color.

Thanks.
Aug 8 '07 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,392
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls
  4.    ...
  5. Next ctl
  6.  
Aug 8 '07 #2

missinglinq
Expert 2.5K+
P: 3,532
Rabbit has shown you the general code, but since you only want labels to be affected you have to tell Access that. Also, since we're dealing with labels, you have to select all of the labels and change their Back Style from the Default "Transparent" to "Normal." You can do this in one fell swoop by holding down the <Shift> key, clicking on each label, right clicking then clicking on Properties - Format and set the Back Style property. Then, in whatever event you like:

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each ctl In Me.Controls
  4.   If TypeOf ctl Is Label Then
  5.    ctl.BackColor = vbYellow  'or whatever color you want
  6.  End If
  7. Next
  8. End Sub
Welcome to TheScrips!

Linq ;0)>
Aug 8 '07 #3

P: 14
I should have been more clear in my initial question. Actual, my form has 3 tabs, and depending on which tab i have clicked the lables colors changes. so with your replies the code would have worked if all labels were on one tab.

thanks.
Aug 9 '07 #4

missinglinq
Expert 2.5K+
P: 3,532
I'm more fuzzy now than I was as to what you're doing, but if you're saying you want to do this for one tab at a time, I think this modification will work by substituting your page name for YourPageNameHere:

Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2.  
  3. For Each Ctrl In [YourPageNameHere].Controls
  4. If TypeOf ctl Is Label Then
  5. ctl.BackColor = vbYellow 'or whatever color you want
  6. End If
  7. Next
  8. End Sub
Linq ;0)>
Aug 9 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.