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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop thru formobjects(labels) changing fontcolor... 1

Status
Not open for further replies.

snayjay

Programmer
Oct 23, 2001
116
US
I'm dealing with 2 forms:

#1 - Forms!CHANGEQUERYfrm!CHANGEQUERYSUBfrm
#2 - Forms!BGfrm

#1 is where the labels are that I want to change the fontcolor for
#2 is where the forecolor value is for all my forms in my database.

The field with the forecolor value is FONTCOLOR.value

Currently I assign the value on each form individually for each label. i.e...
Code:
  Sub Form_Load()
     LABEL1.Forecolor = Forms!BGfrm!FONTCOLOR.Value
     LABEL1.Forecolor = Forms!BGfrm!FONTCOLOR.Value
  End sub
But now I have a form with over 150 labels on it. I wanted to find a way I could Loop thru all the Label(objects) on my form changing them based on the value on the #2 form.

I thought I could name all labels numerically LABEL1,LABEL2,LABEL3,LABEL4,etc., and use a string in a loop to change it. Something like

Code:
  Sub Form_Load()
    dim lblname 
    dim x as byte
    x = 1
    lblname = "LABEL" & x
    Do Until x = 150
       lblname.Forecolor = Forms!BGfrm!FONTCOLOR.Value
       x = x + 1
    Loop
  End Sub

Or something like that. Well I tried and I don't know why it didn't work. I think it took lblname as a literal and not an object. Can anyone fix this for me. I'm sure it's an easy fix.
 
Hey thanks for that...

I searched this website for the TypeOf function and found code that worked.

Code:
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is Label Then
            With ctrl
                .ForeColor = Forms!BGfrm!FONTCOLOR.Value
            End With
        End If
    Next ctrl

This has made all my forms that much easier to view the code. Thanks alot for the search advice.

~Snay
 
I see the challenge is now solved, but you could have used the initial approach (concatenation of string and number)

[tt] dim idx as long
For idx = 1 to 150
me("LABEL" & idx).forecolor =Forms!BGfrm!FONTCOLOR.Value
Next idx[/tt]

Then there's also the .controltype property:

[tt] Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.controltype = acLabel Then
ctrl.ForeColor = Forms!BGfrm!FONTCOLOR.Value
End If
Next ctrl[/tt]

Some more looping samples in faq702-5010

Roy-Vidar
 
Thanks Roy... there's a few of you here that make it look so easy. Thanks for helping us novices.
 

What if I wanted to change the detail.backcolor. It's not a control but a section. I tried replacing control with section and that didn't work. I read that faq and didn't see any reference to this so I thought I'd ask. Again I'm putting the changecolor code on form #1 and it's affecting form #2.
 
I don't think the sections counts as controls, but sections, they can be reached both through their name or through index, but I'm not sure you can loop them dynamicly (I think you've only got acPageHeader, acHeader, acDetail, acFooter, acPageFooter)

[tt]me.sections("Detail").backcolor = <somecolour> ' name
me.sections(acDetail).backcolor = <somecolour>[/tt]

Roy-Vidar
 
I tried this and it says Method or Data Member not found and it highlights the...

me.sections("Detail").backcolor = <somecolour>

Any thoughts?
 
Argh - section - singular, sorry - and if you're going to use the name, be sure it is the correct name (section properties, other tab, name). Local versions of Access may use local names (Detalj where I live), rather use the acDetail constant.

Roy-Vidar
 
works now!

I deleted the plural "s" on sections

me.section("Detail").backcolor....

Thanks for helping out, I really appreciate it.

I've been working on this project of mine for almost 5 months. I'm nearly done and I'm just doing some customizing and code optimizing. Plus some reports. I am stationed here in Korea and have been so for 1 year. I've been here remote without my wife and 2 young girls. I leave in 2 weeks and I've been busting hump to finish it before I leave. With all the help I'm getting here I'm sure I'll finish it.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top