Code to change background colour of textbox on doubleclick

S

Swannybuck

I have a requirement to have separate cells in a form (a timesheet)
highlighted when double clicked. I would like this formatting to stay with
each record for future viewing. If I decide to remove the highlighting, I
would like to do so by double-clicking again.

Please note I am a novice with code.
 
F

fredg

I have a requirement to have separate cells in a form (a timesheet)
highlighted when double clicked. I would like this formatting to stay with
each record for future viewing. If I decide to remove the highlighting, I
would like to do so by double-clicking again.

Please note I am a novice with code.

Code the Double-click event of EACH control:

If Me.ControlName.BackColor = vbWhite Then
Me.ControlName.BackColor = vbYellow
Else
Me.ControlName.BackColor = vbWhite
End If

Change the color values as wanted.
 
W

Wayne-I-M

Hi

Aircode (but should be OK - I hope?)


Private Sub SomeControl_DblClick(Cancel As Integer)
If Not ([SomeControl].BackColor = vbYellow) Then
Me.SomeControl.BackColor = vbYellow
Else
Me.SomeControl.BackColor = vbWhite
End If
End Sub



Change SomeContol to the real name of the control you want to highlight
 
C

Conan Kelly

Swannybuck,
highlighted when double clicked. I would like this formatting to stay
with
each record for future viewing. If I decide to remove the highlighting, I

Does that mean each record would have different "cells"/controls highlighted
compared to the other records? And you need them to be memorized? So,
record 1 could have controls 2 & 4 (out of 8) highlighted and record 2 could
have control 5 highlighted, and you would need them to be memorized so the
Access DB/application could be closed and reopened and those 2 records will
have the same controls highlighted, until you dbl-click to turn off
highlighting?

If that is the case, you would need some sort of field/column in the table
that stores info telling what controls need to be highlighted. Then in the
dbl-click event, you would need code to modify this column and in the on
current event for the form, you would need code that would read this column
and change the highlighting of the controls accordingly.

If you highlight one control, that one control will have that same
highlighting for every record you switch to.

HTH,

Conan
 
K

Ken Sheridan

As John Goddard has said, you will need to use conditional formatting to
achieve this. To be able to change the background colour of a control at
the whim of the user independently of the value in the control, however, you
will need to add another column to the table for each column you wish to
highlight. These extra columns can be of Boolean (Yes/No) data type so that
their values can be toggled between True and False, providing a basis for an
expression on which to base the conditional formatting.

Taking a hypothetical example lets say you have a column HoursAM which
records the morning hours worked by an employee. You'd add a Boolean column
to the table, blnHoursAM say. In the DblClick event procedure of the HoursAM
control on your form change the value of the blnHoursAM column in the current
row of the table with:

Me.blnHoursAM = Not Me.blnHoursAM

This simply toggles the value of blnHoursAM between False and True. You
don't need to include a control bound to the blnHoursAM column in the form;
it just has to be in the form's underlying table or query.

To conditionally format the HoursAM control on the basis of the value in the
blnHoursAM column, in the conditional formatting dialogue for the blnHoursAM
control select 'Expression is' from the combo box on the left, enter
[blnHoursAM] in the text box to its right (make sure you include the square
brackets around the column name), and set the back colour to a colour of your
choice.

Ken Sheridan
Stafford, England
 
S

Swannybuck

Ken,

Thanks for your response. I was afraid that would be the answer. This
timesheet is already quite involved. Perhaps I'll take this on at a later
date. Cheers.

Ken Sheridan said:
As John Goddard has said, you will need to use conditional formatting to
achieve this. To be able to change the background colour of a control at
the whim of the user independently of the value in the control, however, you
will need to add another column to the table for each column you wish to
highlight. These extra columns can be of Boolean (Yes/No) data type so that
their values can be toggled between True and False, providing a basis for an
expression on which to base the conditional formatting.

Taking a hypothetical example lets say you have a column HoursAM which
records the morning hours worked by an employee. You'd add a Boolean column
to the table, blnHoursAM say. In the DblClick event procedure of the HoursAM
control on your form change the value of the blnHoursAM column in the current
row of the table with:

Me.blnHoursAM = Not Me.blnHoursAM

This simply toggles the value of blnHoursAM between False and True. You
don't need to include a control bound to the blnHoursAM column in the form;
it just has to be in the form's underlying table or query.

To conditionally format the HoursAM control on the basis of the value in the
blnHoursAM column, in the conditional formatting dialogue for the blnHoursAM
control select 'Expression is' from the combo box on the left, enter
[blnHoursAM] in the text box to its right (make sure you include the square
brackets around the column name), and set the back colour to a colour of your
choice.

Ken Sheridan
Stafford, England

Swannybuck said:
I have a requirement to have separate cells in a form (a timesheet)
highlighted when double clicked. I would like this formatting to stay with
each record for future viewing. If I decide to remove the highlighting, I
would like to do so by double-clicking again.

Please note I am a novice with code.
 
Top