Make it easier to see the current record on your Microsoft Access forms! To highlight the current record with a yellow background color,
use Conditional Formatting,
a control to keep track of the primary key value,
a control to change color,
and a little VBA. See how it works in this video:
While designing, I like to increase the height of the detail section,
create a shorter txtHighlight control below everything else on the ruler,
set all the properties, then move txt Highlight up,
close the height of the detail section, and then set the Height of txtHighlight to whatever the section turns out to be.
Once the controls are created, write VBA code and set Conditional Formatting.
MyPrimaryKey is the name of your primary key control such as ProductID, OrderID, ContactID, etc.
The expression in a Conditional Formatting rule can refer to control names or field names.
If you are using a datasheet form instead of a continuous form,
you can't have a control under all the others,
so set the conditional formatting rule on every control.
You can then select all the textboxes and set the rule.
To specify code for an event
select the object you want such as the form or a control
go to the Event tab of the property sheet
choose [Event Procedure] or pick it from the list -- or just type "[" to pick [Event Procedure]
Click the builder button [...], or press Ctrl+F2, to go to the Visual Basic Editor
Access will automatically create the Sub declaration and End Sub Statements.
Type what you want to happen between the procedure declaration and End statements.
Go to the Design View of the form
Select all controls in the Detail section that you want the highlight to appear behind, and set:
Back Style = Transparent
expand detail section to be taller to give yourself some working space
Grab bottom of detail section border and drag down
Create an unbound textbox control in Detail section, below other controls on the ruler (for now), to highlight the active record:
Name = txtHighlight
Left = 0
Enabled = No
Locked = Yes
TabStop = No
Width = width of form
-------------- set these later -------------
Top = 0
Height = height of section
send txtHighlight control to the back
right-click on it, choose: Position > Send to Back
set Top of txtHighlight, close space, and set Height
Top = 0 -- and then close space in Detail section
Height = height of section
Create an unbound textbox control in the form footer to keep track of the primary key.
Name = CurrentID
On the form Current event, which happens when a record changes, set CurrentID to the primary key (PK). If the PK doesn't yet have a value (new record), set it to be 0 (zero).
Private Sub Form_Current()
.CurrentID = Nz(.MyPrimaryKey, 0)
if PK is not numeric, change optional argument for NZ to "" for text.
on the GotFocus event for txtHighlight, make the active control go somewhere else so it doesn't come to the front and cover everything
Private Sub txtHighlight_GotFocus()
Compile the code. Save.
set Conditional Formatting for txtHighlight so the background color changes. Select txtHighlight and choose Conditional Formatting from Format tab on the ribbon.
click New Rule
choose: "Expression is"
enter rule: Nz( [MyPrimaryKey], 0) = [CurrentID]
set format such as yellow for background color
OK, then OK again to dismiss conditional formatting manager