?
Solved

Changing properties of a form

Posted on 2012-08-20
4
Medium Priority
?
610 Views
Last Modified: 2012-08-20
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]
0
Comment
Question by:rick_danger
  • 2
4 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 664 total points
ID: 38313246
>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
 
LVL 61

Accepted Solution

by:
mbizup earned 668 total points
ID: 38313252
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
 
LVL 66

Expert Comment

by:Jim Horn
ID: 38313255
>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
 
LVL 49

Assisted Solution

by:Dale Fye
Dale Fye earned 668 total points
ID: 38313307
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

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question