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

Changing colours of objects on form open

Status
Not open for further replies.

mych

Programmer
May 20, 2004
248
0
0
GB
Hi,

I'm trying to add some bells an whistles to an app and allow users to choose their own colour scheme.
The user gets to choose 3 colours which are stored in a table and are loaded up into global variables during FrmSplash. This works great and I'm able to change my other forms to the users colour scheme as each form is loaded by listing each element separately.

I then came across some code in these forums that I adapted to try and loop through each element without having to list each one.

My code is:

Code:
Dim ctlX As Control
Dim frm As Form

            Set frm = Screen.ActiveForm
            FormHeader.BackColor = GlbHeaderBackColour
            FormFooter.BackColor = GlbHeaderBackColour
            Section(acDetail).BackColor = GlbDetailBackColour
            
            For Each ctlX In frm.Controls
                If Left$(ctlX.Name, 3) = "lbl" Then
                    ctlX.ForeColor = GlbHeaderForeColour
                Else
                    If Left$(ctlX.Name, 3) = "box" Then
                    ctlX.BackColor = GlbHeaderBackColour
                    End If
                End If
            Next
            Set ctlX = Nothing

This should go through each control and if the first three letters on the controls name is either lbl or box it will change the Fore or Back colour appropriately.

Unfortunately I get the following error....

Run-time error '2475'
You entered an expression that requires the form to be the active window.

I have tried the code in the forms;
On Open
On Load
On Active
On Current Events with the same error.

Any help would be appreciated.
 
I posted this nearly exact same question just yesterday....so check back on those posts. As for your code, (I don't know much) but I did notice you are missing a With statement. After your For Statement. So check back on those other posts and you'll probably find some help in that area. As for getting that error on an 'On Open' command on the local form. I know that if you just use me.controls instead of declaring a variable and assigning the variable the screen.activeform...it will work fine. This of course means you can't do it in a module but on EVERY form...but at least it works. That's how I've done it. But I too would be interested in seeing how to do it say in a module that I could call and the module code will apply it to any open form. That would rock.
 
Hi,

Try this code in a global module:

Code:
[COLOR=blue]
Function ChangeFormColours(strFormName As String)

    Dim ctlX As Control
    Dim frm As Form

    Set frm = Forms(strFormName)
    
    With frm
        .FormHeader.BackColor = GlbHeaderBackColour
        .FormFooter.BackColor = GlbHeaderBackColour
        .Section(acDetail).BackColor = GlbDetailBackColour
    
        For Each ctlX In frm.Controls
            If Left$(ctlX.Name, 3) = "lbl" Then
                ctlX.ForeColor = GlbHeaderForeColour
            Else
                If Left$(ctlX.Name, 3) = "box" Then
                    ctlX.BackColor = GlbHeaderBackColour
                End If
            End If
        Next
    End With
    
    Set ctlX = Nothing

End Function
[/color]

Use this as the OnOpen event of every form that you want to customise.
Code:
[COLOR=blue]
Private Sub Form_Open(Cancel As Integer)
    ChangeFormColours Me.Name
End Sub
[/color]
This will allow you to use the same bit of code over and over. Instead of repeating it in each form.

Need any more help - let me know.

Dean :)
 
Thanks Dean.

Good Idea to have it in a Global module. As I have only 3 forms FrmSplash, FrmOptions and FrmMain I did't neet to do this on this occation.

My code now is:
Code:
Dim ctlX As Control

            FormHeader.BackColor = GlbHeaderBackColour
            FormFooter.BackColor = GlbHeaderBackColour
            Section(acDetail).BackColor = GlbDetailBackColour
            
            
            For Each ctlX In Me.Controls
                If ctlX.ControlType = acLabel Then
                    ctlX.ForeColor = GlbHeaderForeColour
                ElseIf ctlX.ControlType = acRectangle Then
                    ctlX.BackColor = GlbHeaderBackColour
                ElseIf ctlX.ControlType = acTextBox Then
                    ctlX.ForeColor = GlbHeaderForeColour
                End If
            Next
            Set ctlX = Nothing

This is in the On Open Form Event. It colours all lables and textboxes on my form. I have also made all my cmd buttons transparant and have a rectangle behind them. This way the cmd buttons can appear in a different colour that the usual grey.

Thanks for everyones help.
 
Glad you sorted it.

What I do to allow users to customise forms is, I use system colours for everything. This way the forms will take on the colour scheme of the users Windows Theme. I know this isn't what you want in this case but it works quite well and allows users to totally customise the appearance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top