Help Referencing SubForm Controls in a Module (1 Viewer)

grendell2099

Registered User.
Local time
Today, 06:52
Joined
Aug 9, 2006
Messages
29
Hi all- I need some help with subforms and subform controls. I have been searching high and low without success.
What I am trying to do:
I am trying to make a procedure (in a module) that can be called from any form that will change the form backcolor, as well as the color of any text boxes & labels. I know how to do this.
However, I have some forms that have subforms on them and I have been unable to correctly reference the subforms or their controls to change the colors.I thought I could pass the name of the form on open to this module and loop through sub form controls if a sub form was present.

Why do I want to do this?
My application is distributed as an accde. The vast majority of my users prefer a bright form layout- lots of white. A few users would like a dark view.
I figured why not change the color scheme dynamically when a user opens the form. I have done some testing and there is no performance lag.
I suppose this falls into the category of doing it just to do it, but at the very least I hope to learn something. Below is my crack at the code- any help would be appreciated.


Sub ChangeFormColor(MyForm As String, MySub As String)

Dim frm As Form
Dim MyFormName As String
Dim ctl As Control
Dim MyView As String
Dim x
Dim Xname
'************************************

MyView = "DARK" 'test value
Select Case [MyForm]
Case Is = "frmBackup" 'skip this form
Case Else
Set frm = Forms(MyForm)
'--- CHANGE THE HEADER ---
If MyView = "DARK" Then
frm.Section(acHeader).BackColor = 16743697
frm.Section(acDetail).BackColor = 0
frm.Section(acDetail).AlternateBackColor = 0
Else
frm.Section(acHeader).BackColor = 3289650
frm.Section(acDetail).BackColor = 13754083
frm.Section(acDetail).AlternateBackColor = 13754083
End If
'--- NOW CHANGE THE HEADER CONTROLS ---
For Each ctl In frm.Section(acHeader).Controls
x = ctl.ControlType
Xname = ctl.Name
With ctl
If MyView = "Dark" Then
Select Case .ControlType
Case acCommandButton
Case acCheckBox
Case acTabCtl
Case acPage
case acSubForm
'this is where I am stuck, I cannot figure out the syntax
' if there is a subform, change the header and detail color
'loop through subform controls and change their forecolor, bordercolor, and backstyle
Case acLabel
.BackStyle = 0
.ForeColor = 0
.BorderColor = 0
Case Else
.BackStyle = 1
.ForeColor = 0
.BorderColor = 0
End Select
End If
End With
Next
'--- CHANGE THE DETAIL CONTROLS ---
For Each ctl In frm.Section(acDetail).Controls
'+++++++++++++
x = ctl.ControlType
Xname = ctl.Name
'+++++++++++++
With ctl
If MyView = "Dark" Then
Select Case .ControlType
Case acCommandButton
Case acCheckBox
Case acTabCtl
Case acPage
Case acSubform
Case acLabel
.BackStyle = 0
.ForeColor = 16743697
.BorderColor = 16743697
Case Else
.BackStyle = 1
.BackColor = 0
.ForeColor = 16743697
.BorderColor = 16743697
End Select
Else 'apply the light theme
Select Case .ControlType
Case acCommandButton
Case acCheckBox
Case acTabCtl
Case acPage
Case acSubform
Case acLabel
.BackStyle = 0
.ForeColor = 16777215
.BorderColor = 2031743
Case Else
.BackStyle = 1
.ForeColor = 16777215
.BorderColor = 2031743
End Select
End If
End With
Next
End Select
'------------------------------------------------------------------------------------
End Sub
 

MarkK

bit cruncher
Local time
Today, 06:52
Joined
Mar 17, 2004
Messages
8,199
What I would do is pass a reference to the form into the colour change routine, so...
Code:
Sub ChangeFormColour(frm as Access.Form)
[COLOR="Green"]  'don't try to get the form object from a string
  'get the calling code to pass in the form object[/COLOR]
  frm.Section(acHeader).BackColor = 3289650
  frm.Section(acDetail).BackColor = 13754083
  frm.Section(acDetail).AlternateBackColor = 13754083
  If MyView = "DARK" Then
    frm.Section(acHeader).BackColor = 16743697
    frm.Section(acDetail).BackColor = 0
    frm.Section(acDetail).AlternateBackColor = 0
  End If
[COLOR="Green"]  'then, when you get here...[/COLOR]
  Select Case ctl.ControlType
    Case acCommandButton
    Case acCheckBox
    Case acTabCtl
    Case acPage
    Case acSubForm
[COLOR="Green"]      '...simply recall this routine with the subform reference
      'which is contained by the Form property of the subform control
[/COLOR]      ChangeFormColour ctl.Form
[COLOR="Green"]      'this is called recursion and this routine is therefore called recursive[/COLOR]
  End Select
[COLOR="Green"]  'etc...[/COLOR]
End Sub
Passing objects is very efficient, since what is passed is actually only a reference to the object, so you can pass lots of very rich info to a subroutine when you pass the object instead of the simple datatypes.
Cheers,
Mark
 
Last edited:

songbird3

New member
Local time
Today, 09:52
Joined
May 11, 2011
Messages
5
Access recognizes TypeOf for controls.

Dim ctlData as Control
For Each ctlData In Me.Detail.Controls
If TypeOf ctlData is TextBox Or TypeOf ctlData Is CheckBox Or TypeOf _
ctlData is SubForm Or TypeOf ctlData is ComoboBox Then
***do stuff***
End If
Next
 

grendell2099

Registered User.
Local time
Today, 06:52
Joined
Aug 9, 2006
Messages
29
Lagbolt, Songbird, thank you very much for the assist. I went with Lagbolt's example and it seems to work like a charm! I had a little trouble passing the form object, but this seems to work:

Dim frm As Form
Set frm = Forms!frmsetup
Call ChangeFormColor(frm)
 

Users who are viewing this thread

Top Bottom