OK Frank, I warned you this is hairy
I have a continuous form CalendarNames
One of the controls is text Box - CalendarColour bound to a field in a table
which is the colour, so If I wanted the text box to be red then the field
woud vontain 255.
Here is the code for the form
Private Sub Form_Current()
CalendarColour.BackStyle = 1 ' Normal
Call SetFormatCond(Me, "CalendarColour", "CalendarColour", True)
CalendarColour.BackColor = Nz(CalendarColour)
End Sub
Simple
Now the hairy bit
Sub SetFormatCond(Frm As Form, FldToColour As String, _
FldColourInfo Info As String, ChangeForeColour As Boolean, Optional Criteria
As String) ' Conditional formatting
' Frm is target form,
' FldToColour is the field to which to apply the conditional formatting
' FldColourInfo holds the RGB colour code (Long)
' ChangeForeColour, if true, tries to give a contrasting ForeColor
Dim FormatCond As FormatCondition
Dim i As Long
On Error GoTo SetFormatCond_Err
'Remove existing format conditions but due to an Access bug, if you have
' less than 4 formats, the FormatConditions.Add wont work.
' There is a limit of 3 formats otherwise
Do Until Frm(FldToColour).FormatConditions.Count = 4
Frm(FldToColour).FormatConditions(1).Delete
Loop
' Add the new formats, starting at number 4
With Frm.RecordsetClone
If Nz(Criteria) > "" Then ' Criteria passed
.FindFirst Criteria
Do Until .NoMatch
Set FormatCond = Frm(FldToColour).FormatConditions.Add _
(acExpression, , "[" & FldColourInfo & "] = " _
& Nz(Frm.RecordsetClone(FldColourInfo)))
.FindNext Criteria
Loop
Else
.MoveFirst
Do Until .EOF
Set FormatCond = Frm(FldToColour).FormatConditions.Add _
(acExpression, , "[" & FldColourInfo & "] = " _
& Nz(Frm.RecordsetClone(FldColourInfo)))
.MoveNext
Loop
End If
.Close
End With
i = 4 ' Ignore the first 4 formats
With Frm.RecordsetClone
If Nz(Criteria) > "" Then ' Criteria passed
.FindFirst Criteria
Do Until .NoMatch
If Not IsNull(Frm.RecordsetClone(FldToColour)) Then
Frm(FldToColour).FormatConditions(i).BackColor = _
Nz(Frm.RecordsetClone(FldColourInfo))
If If ChangeForeColour = True Then ' Contrasting ForColor
Frm(FldToColour).FormatConditions(i).ForeColor = _
Contrast(Nz(Frm.RecordsetClone(FldColourInfo)))
End If
Else
Frm(FldToColour).FormatConditions(i).BackColor = _
Frm(FldToColour).FormatConditions(1).BackColor
If If ChangeForeColour = True Then ' Contrasting ForColor
Frm(FldToColour).FormatConditions(i).ForeColor = _
Contrast(Frm(FldToColour).FormatConditions(1).BackColor)
rmatConditions(1).BackColor) End If
End If
i = i + 1
.FindNext Criteria
Loop
Else
.MoveFirst
Do Until .EOF
If Not IsNull(Frm.RecordsetClone(FldToColour)) Then
Frm(FldToColour).FormatConditions(i).BackColor = _
Nz(Frm.RecordsetClone(FldColourInfo))
If If ChangeForeColour = True Then ' Contrasting ForColor
Frm(FldToColour).FormatConditions(i).ForeColor = _
Contrast(Nz(Frm.RecordsetClone(FldColourInfo)))
End If
Else
Frm(FldToColour).FormatConditions(i).BackColor = _
Frm(FldToColour).FormatConditions(1).BackColor
If If ChangeForeColour = True Then ' Contrasting ForColor
Frm(FldToColour).FormatConditions(i).ForeColor = _
Contrast(Frm(FldToColour).FormatConditions(1).BackColor)
rmatConditions(1).BackColor) End If
End If
i = i + 1
.MoveNext
Loop
End If
.Close
End With
Exit Sub
SetFormatCond_Err:
MsgBox gBox "Set Format Conditions error Number: " & Err.Number & " " &
Err.Description
End Sub
unction Contrast(ColIn As Long) As Long
Dim iHue As Long
Dim iSat As Long
Dim iLum As Long
Dim iRed As Long
Dim iBlue As Long
Dim iGreen As Long
iHue = GetHSL(ColIn, 1)
iSat = GetHSL(ColIn, 3) ' Note Order
iLum = GetHSL(ColIn, 2) ' Note Order
iRed = GetRGB(ColIn, 1)
iGreen = GetRGB(ColIn, 2)
iBlue = GetRGB(ColIn, 3)
' Special situations
If iBlue > 250 Then
If iRed < 150 And iGreen < 150 Then
Contrast = vbYellow
Exit Function
End If
End If
If iSat < 100 Then ' Greyish
Contrast = vbBlack
Exit Function
End If
Contrast rast = ((ColIn Xor &HFFFFFF) And &HFFFFFF) Or (ColIn) And (Not
&HFFFFFF)
End Function
Function GetRGB(RGBval As Long, Num As Integer) As Integer
' Check if Num, RGBval are valid.
If Num > 0 And Num < 4 And RGBval > -1 And RGBval < 16777216 Then
GetRGB = RGBval \ 256 ^ (Num - 1) And 255
Else
' Return True (-1) if Num or RGBval are invalid.
GetRGB = True
End If
End Function
MOST IMPORTANT
Due to a Bug, you need to manually set up 3 conditional formats for your
textbox. Doesn't matter what they are as they will be ignored.
Hope this helps, and everything is there. If not, we'll have to try something
else
Phil