Code Help

  • Thread starter Thread starter Froto
  • Start date Start date
F

Froto

I'm created a calandar in Microsoft access, and have a
checkbox in each sqaure that if selected it changes the
background color to red and if unselected it changes back
to white. Here is the code thats in the on current event
of the form.

Private Sub Form_Current()
If Me!Check1.Value = True Then
Me!ID1.BackColor = vbRed
Else
Me!ID1.BackColor = vbWhite
End If
End Sub

What I need is how to change the code so that it changes
the color for every box selected, the code above only
does 1 box, so if user selects box 1 and box 6 and box 10
it would only change the background for those boxes, I
have 31 boxes each with a checkbox.

Thanks for all the help
 
-----Original Message-----
I'm created a calandar in Microsoft access, and have a
checkbox in each sqaure that if selected it changes the
background color to red and if unselected it changes back
to white. Here is the code thats in the on current event
of the form.

Private Sub Form_Current()
If Me!Check1.Value = True Then
Me!ID1.BackColor = vbRed
Else
Me!ID1.BackColor = vbWhite
End If
End Sub

What I need is how to change the code so that it changes
the color for every box selected, the code above only
does 1 box, so if user selects box 1 and box 6 and box 10
it would only change the background for those boxes, I
have 31 boxes each with a checkbox.

Thanks for all the help
.
You are only looking at one check box (Me!Check1.Value).
In order to get all boxes the right color you need to
evaluate all of them. Here is the down and dirty way to do
that.
IIF(me.check1 = -1,me.Id1.Backcolor =
vbRed,me.Id1.Backcolor = vbWhite)

This is called an Inline If Statement. It evalutes the
first part "me.check1 = -1" and if it is true it will run
the code following the first comma otherwise it will run
the code following the second comma. It is better for
perfoming simple If then Else statements.

You will have to repeat the IIF for every check box on the
form.
There are better ways to do what you want but this should
get you where you want to be for now.
 
here's another solution. put the following procedure in your form's module:

Private Sub SetBackColor(ByVal ctlBox As Control)

If Screen.ActiveControl = True Then
ctlBox.BackColor = 255
Else
ctlBox.BackColor = 16777215
End If

End Sub

on each checkbox's AfterUpdate event, add the following procedure, as

Private Sub YourCheckBox_AfterUpdate()

SetBackColor Me!YourSquareControlName

End Sub

OR, if you prefer, you can add the "square" control's name to the
corresponding checkbox's Tag property, as

YourSquareControlName
type it just as you see it above, no equal sign or quotes, and no form
reference.

then on each checkbox's AfterUpdate event, add the following procedure
(instead of the previous one), as

Private Sub YourCheckbox_AfterUpdate()

SetBackColor Me(Screen.ActiveControl.Tag)

End Sub

hth
 
Back
Top