Solved changing forecolor in certain sections (1 Viewer)

sinavere

New member
Local time
Today, 16:06
Joined
Jan 28, 2021
Messages
8
I'm working on a function that allows a user to pick custom colors, and that the colors they chose will be implemented on all different forms, reports, etc
For now I'm just focussing on the UserForms.
I have managed to let the user pick a background color for header and one for the detail section by using a colorpicker and storing the info in a table.
Now the harder part (?): changing the textcolor of the controls (labels, textboxes, buttons and the likes). I want to be able to use different colors in the different section of the form. I.e. all labels in the header section having white text, while all labels in detail will have darkgrey text)

I have managed this idea once (on buttons) by using tags. But I am wondering: is there an easier way to adress all controlstypes that are in a certain section only? (i.e. 'all buttons in header' or 'all textboxes in detail')

Code I used when working with tags:
Me.Painting = False

Dim MyControl As Control
Dim MyButton As CommandButton

For Each MyControl In Me.Controls
If MyControl.Tag = 99 Then
Set MyButton = MyControl
MyButton.Enabled = False
MyButton.BackColor = RGB(236, 236, 236)
Set MyButton = Nothing
End If
Next MyControl

Me.Painting = True
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:06
Joined
May 7, 2009
Messages
19,227
there are 9 pre-defined section (acSecion) in access.
depends if you have them in the form.

Code:
Dim iFormSection As Integer
Dim ctl As Control
On Error Resume Next
For iFormSection = 0 To 8
    For Each ctl In Me.Section(iFormSection)
        Select Case TypeName(ctl)
        Case "Textbox"
        Case "Combobox"
        Case "Label"
        Case "Checkbox"
        Case "Listbox"
        Case Else
        End Select
    Next
Next
 

sinavere

New member
Local time
Today, 16:06
Joined
Jan 28, 2021
Messages
8
that looks exactly like what I am after. Thanks, I'm going to give it a try.
 

sinavere

New member
Local time
Today, 16:06
Joined
Jan 28, 2021
Messages
8
Simply copy & paste didn't work but it certainly set me off in the right direction.
The way posted above, it jumped right over all the cases. But I found a way that works, with thanks to another example I found online.
The combination of the two worked for me.
If anyone comes across the same question, here's how I solved it:


Two different functions. Function 'SetColorsForm' is called in the formOpen Event of all forms in my project



Code:
Function SetColorsForm(Formname As String)
'BG
Forms(Formname).Section(acHeader).BackColor = DLookup("[Kleurnummer]", "[Inst_CustomColors]", "[ID] = 1") 'bg header
Forms(Formname).Section(acDetail).BackColor = DLookup("[Kleurnummer]", "[Inst_CustomColors]", "[ID] = 3") 'bg detail 

'controls
Dim iFormSection As Integer
Dim ctl As Control
On Error Resume Next
For iFormSection = 0 To 8
    For Each ctl In Forms(Formname).Section(iFormSection).Controls
        Call PaintText(Formname, ctl, TypeName(ctl), iFormSection)
    Next
Next
End Function
  



Function PaintText(Formname As String, ctrlName As Control, ctrlType As String, Section As Integer)

Dim MyControl As Control
Set MyControl = ctrlName
Dim StoredSlot As Long
StoredSlot = 1

Select Case Section
    Case 1 'form or report header
        If ctrlType = "Label" Then
            StoredSlot = 2
        ElseIf ctrlType = "Textbox" Then
            StoredSlot = 2
        End If
    Case 0 'detail
        If ctrlType = "Label" Then
            StoredSlot = 4
        ElseIf ctrlType = "textbox" Then
            StoredSlot = 5
        End If
End Select

Forms(Formname).Painting = False
MyControl.ForeColor = DLookup("[Kleurnummer]", "[Inst_CustomColors]", "[ID] = " & StoredSlot)
Set MyControl = Nothing
Forms(Formname).Painting = True
End Function
 
Last edited:

Users who are viewing this thread

Top Bottom