Change colour of Detail on continuous form based on value for record

stephen81

Registered User.
Local time
Today, 08:50
Joined
Nov 27, 2002
Messages
198
I have a continuous form set up with text box (called text_1) on it which will either contain a 1 or a 0 (for simplicity sake). I want to change the background colour for the detail section of the form based on whether my text box contains a 1 or a 0. I have found a couple of posts for changing the colour of a text box based on the value in another text box which I've altered to this

Private Sub Form_Open(Cancel As Integer, FormatCount As Integer)

If Me.Checked = "-1" Then
Me.Detail.BackColor = vbRed

Else
Me.Detail.BackColor = vbBlack

End If

but unfortunately I had to guess at the Detail bit as I don't know how to refer to that in code (if it's possible). Can anyone help me out?



I've just had a thought as I've been writing this, I could always put a big text box covering the whole of the detail section behind my other boxes and use the code I've already found to change the colour of that based on the value, which I'll try shortly, but I'd still be interested to find out if my original idea is possible.

Thanks
 
stephen81 said:
Code:
Private Sub Form_Open(Cancel As Integer, FormatCount As Integer)

If Me.Checked = "-1" Then
      Me.Detail.BackColor = vbRed
      
Else
      Me.Detail.BackColor = vbBlack
      
End If

End Sub

Three things:

  • You can't add extra arguments to a form event;
  • A checkbox can have a boolean value that can also be represented by a numerical value (-1) - you have a text value ("-1");
  • The Form_Open event occurs before the recordset has been loaded into the form - you need the Form_Load event.

Might be easier just to say:

Code:
Private Sub Form_Load()

   If Me.Checked Then Me.Detail.BackColor = vbRed Else Me.Detail.BackColor = vbBlack

End Sub
 
Thanks.
That does work to change the colour of the detail background but it does it for all the records in the continuous form, not just for the one that is checked.

Any ideas?
 
Try it in the Form_Current event then.
 
It still changes all of the records in the continuous form.
It also only seems to work if the first record is checked, the others don't make any difference.


My mistake, it actually works for whichever record the cursor is currently on, but it still changes the detail background colour for all of the records on the form.
 
stephen81 said:
It actually works for whichever record the cursor is currently on, but it still changes the detail background colour for all of the records on the form.

That's because it is just the one form and, although it shows other records, it can still only represent one record visually at a time. Thus the form changes colour to represent the current record's value.
 
I've never worked with it, but you might want to consider using conditional formatting if you are working with Access 2000+.
 

Users who are viewing this thread

Back
Top Bottom