On dirty backcolor

C

chancer

I have a form for maintaining address and contact details of a bunch of
people and have used VBA to change the background color of any cells that
have been edited.

When the changes are saved or undone the background color is returned to
white


Below is how I did it and it seems very longwinded - is there a more elegant
way of achieving the same result?


thanks

chancer

---

Private Sub txtName_Dirty(Cancel As Integer)
Me.txtName.BackColor = vbYellow
End Sub
Private Sub txtShortName_Dirty(Cancel As Integer)
Me.txtShortName.BackColor = vbYellow
End Sub
Private Sub txtAddress1_Dirty(Cancel As Integer)
Me.txtAddress1.BackColor = vbYellow
End Sub
Private Sub txtAddress2_Dirty(Cancel As Integer)
Me.txtAddress2.BackColor = vbYellow
End Sub

etc

---
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Do you want to save the changes?", vbYesNo, "Confirm Change")
= vbNo Then
Cancel = True
Me.Undo
Me.txtName.BackColor = vbWhite
Me.txtShortName.BackColor = vbWhite
Me.txtAddress1.BackColor = vbWhite
Me.txtAddress2.BackColor = vbWhite
etc
end if
End sub

---

Private Sub Form_AfterUpdate()
Me.txtName.BackColor = vbWhite
Me.txtShortName.BackColor = vbWhite
Me.txtAddress1.BackColor = vbWhite
Me.txtAddress2.BackColor = vbWhite
etc
End sub
 
A

Allen Browne

You could use the Dirty event of the form, rather than the event of each
control.

It would be good to use the form's Undo event too.

You could write a single routine that sets the colors, and call it from the
different events.
 
S

Stefan Hoffmann

hi,
I have a form for maintaining address and contact details of a bunch of
people and have used VBA to change the background color of any cells that
have been edited.
Below is how I did it and it seems very longwinded - is there a more elegant
way of achieving the same result?
Untested:

Public Function Colorize(AForm As Access.Form) As Boolean

On Local Error Resume Next

Dim ct As Access.Control

For Each ct In AForm.Controls
If ct.Value <> ct.OldValue Then
ct.BaclColor = vbYellow
else
ct.BackColor = vbWhite
End If
Next ct

Colorize = True

End Sub

And instead of an OnDirty event use "=Colorize([Form])" for your controls.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top