Changing properties of a form

I am using the code at the bottom to dynamically change certain properties of each form that is loaded, at the time of opening. The colours that are used get their values from hidden fields in Switchboard.

The problem is that I have to put this code into the open event of every form, which makes it difficult if I want to modify it. Is there a way of using a function that every form has to read which is held in one place?

Code:

       For Each ctl In Me.Controls
        Select Case ctl.ControlType
           Case acTextBox
            With ctl
                   .BackColor = Forms![Switchboard]![control_back_colour]
                   .ForeColor = Forms![Switchboard]!control_fore_colour
                   .FontSize = Forms![Switchboard]![font_size]
            End With
            End Select
            Next
            Me.FormHeader.BackColor = Forms![Switchboard]![header_back_colour]
            Me.Detail.BackColor = Forms![Switchboard]![detail_back_colour]
            Me.FormFooter.BackColor = Forms![Switchboard]![footer_back_colour]
rick_dangerAsked:
Who is Participating?
 
mbizupConnect With a Mentor Commented:
Revise the code like this and place it in a public module:

Public Sub ChangeColors(frm as form)
       For Each ctl In frm.Controls
        Select Case ctl.ControlType
           Case acTextBox
            With ctl
                   .BackColor = Forms![Switchboard]![control_back_colour]
                   .ForeColor = Forms![Switchboard]!control_fore_colour
                   .FontSize = Forms![Switchboard]![font_size]
            End With
            End Select
            Next
            frm.FormHeader.BackColor = Forms![Switchboard]![header_back_colour]
            frm.Detail.BackColor = Forms![Switchboard]![detail_back_colour]
            frm.FormFooter.BackColor = Forms![Switchboard]![footer_back_colour]
End Sub

Open in new window



And call it like this from your form's open events:


ChangeColors Me

Open in new window

0
 
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
>Is there a way of using a function that every form has to read which is held in one place?
You could make it a Public Sub or Function in a separate code module, with one parameter...

    Public Sub form_settings (frm as Form)

Then in every form's OnOpen event, call that function like this:

   Call form_settings(Me)

Good luck.
Jim
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>The problem is that I have to put this code into the open event of every form,
Ultimately you'll have to put at minimum a function call in each Form.OnOpen, as there is no global way to pull off 'every time a form is opened, do this...'

Also, add Me.Refresh at the end of the function, just to make sure that the VBA form changes are reflected.
0
 
Dale FyeConnect With a Mentor Commented:
Or, to take mbizup's post one step further.  You could modify her subroutine, make it a function so that you don't even need to pass the form to the procedure.  Something like:
Public Function fnChangeColors()

    Dim frm as form
    Set frm = Screen.ActiveForm

    For Each ctl In frm.Controls

Open in new window

Then, instead of using the Open event, I would use the Load event, and because it is a function, you could call it by just changing the On Load Event procedure in the forms property sheet (no need to actually create an event procedure).fnChangeColors
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.