Change background color on all controls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I change the background color to say, white if it currently is yellow
and to blue if it currently is grey on all of one databases forms.
Thanks for your help!
 
This is the function that I use to change the colour of all forms and
controls, it is set up for the style that I use but you should be able to pick
out the bits that you want and adapt them to your needs.

Public Sub FormColourAll(NewColour As Long)
On Error GoTo Err_FormColourAll
Dim Thisdb As Database, Ctr As Container, Doc As Document, ThisForm As Form
Dim ThisControl As Control, SECTIONNUMBER As Integer, OldColour As Long
If NewColour = 0 Then
DoCmd.OpenForm "MenuMain", acDesign
NewColour = Forms!menumain.Section(acDetail).BackColor
DoCmd.Close
End If
Set Thisdb = CurrentDb
Set Ctr = Thisdb.Containers!Forms
For Each Doc In Ctr.Documents
DoCmd.OpenForm Doc.Name, acDesign
Set ThisForm = Forms(Doc.Name)
With ThisForm
For SECTIONNUMBER = 0 To 4
OldColour = .Section(SECTIONNUMBER).BackColor
If OldColour > 0 Then
.Section(SECTIONNUMBER).BackColor = NewColour
For Each ThisControl In .Controls
With ThisControl
Select Case .ControlType
Case acTextBox, acLabel
If .BackColor = 12632256 And .SpecialEffect =
acEffectRaised Then
.BackColor = vbButtonFace
ElseIf .BackColor = OldColour Or .BackColor = 16748800 Or
..BackColor = 8421376 Then
.BackColor = NewColour
End If
If .ControlType = acTextBox Then .AllowAutoCorrect = False
Case acRectangle, acOptionGroup, acListBox, acComboBox
If .BackColor = OldColour Or .BackColor = 16748800 Then
.BackColor = NewColour
End If
If .ControlType = acComboBox Then .AllowAutoCorrect = False
End Select
End With
Next ThisControl
End If
Next SECTIONNUMBER
End With
DoCmd.Close , , acSaveYes
Next Doc
Set Doc = Nothing
Set Ctr = Nothing
Set ThisControl = Nothing
Set ThisForm = Nothing
Set Thisdb = Nothing

Exit_FormColourAll:
Exit Sub

Err_FormColourAll:
If Err = 2462 Then
Resume Next
Else
msgbox "FormColourAll Error " & Err & " - " & Error$
Resume Exit_FormColourAll
End If
End Sub

HTH
John
##################################
Don't Print - Save trees
 
Thank you very much!

John Smith said:
This is the function that I use to change the colour of all forms and
controls, it is set up for the style that I use but you should be able to pick
out the bits that you want and adapt them to your needs.

Public Sub FormColourAll(NewColour As Long)
On Error GoTo Err_FormColourAll
Dim Thisdb As Database, Ctr As Container, Doc As Document, ThisForm As Form
Dim ThisControl As Control, SECTIONNUMBER As Integer, OldColour As Long
If NewColour = 0 Then
DoCmd.OpenForm "MenuMain", acDesign
NewColour = Forms!menumain.Section(acDetail).BackColor
DoCmd.Close
End If
Set Thisdb = CurrentDb
Set Ctr = Thisdb.Containers!Forms
For Each Doc In Ctr.Documents
DoCmd.OpenForm Doc.Name, acDesign
Set ThisForm = Forms(Doc.Name)
With ThisForm
For SECTIONNUMBER = 0 To 4
OldColour = .Section(SECTIONNUMBER).BackColor
If OldColour > 0 Then
.Section(SECTIONNUMBER).BackColor = NewColour
For Each ThisControl In .Controls
With ThisControl
Select Case .ControlType
Case acTextBox, acLabel
If .BackColor = 12632256 And .SpecialEffect =
acEffectRaised Then
.BackColor = vbButtonFace
ElseIf .BackColor = OldColour Or .BackColor = 16748800 Or
..BackColor = 8421376 Then
.BackColor = NewColour
End If
If .ControlType = acTextBox Then .AllowAutoCorrect = False
Case acRectangle, acOptionGroup, acListBox, acComboBox
If .BackColor = OldColour Or .BackColor = 16748800 Then
.BackColor = NewColour
End If
If .ControlType = acComboBox Then .AllowAutoCorrect = False
End Select
End With
Next ThisControl
End If
Next SECTIONNUMBER
End With
DoCmd.Close , , acSaveYes
Next Doc
Set Doc = Nothing
Set Ctr = Nothing
Set ThisControl = Nothing
Set ThisForm = Nothing
Set Thisdb = Nothing

Exit_FormColourAll:
Exit Sub

Err_FormColourAll:
If Err = 2462 Then
Resume Next
Else
msgbox "FormColourAll Error " & Err & " - " & Error$
Resume Exit_FormColourAll
End If
End Sub

HTH
John
##################################
Don't Print - Save trees
 
Mary,

Dim ctrl As Control

For Each ctlr In Me.Controls
Select Case ctrl.ControlType
Case acComboBox, acTextBox, acLabel
If ctrl.BackColor = vbYellow Then ctrl.BackColor = vbWhite
If ctrl.BackColor = vbGreen Then ctrl.BackColor = vbBlue
Case Else
'No action
End Select
Next ctrl

I know I used vbGreen in the above example, but that's because there isn't a
standard enum element for grey. You'd have to test for the exact color code
for the shade of grey you're using. In any case, the above example
demonstrates how to do it.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top