Access Conditional Formatting Only Applied on Load
-
20-12-2019 - |
문제
I have a continuous form bound to a DAO recordsource. My conditional formatting rule looks like this:
Expression Is: [DateTimeDeleted] IS NOT NULL (fill textbox backgrounds with bright pink)
And then I have a delete button that shows on each record. The code for it is:
Private Sub cmdDelete_Click()
If Me.NewRecord = False Then
If IsNull(Me!DateTimeDeleted) = True Then
Me!DateTimeDeleted = Now()
DoCmd.RunCommand acCmdSaveRecord
Else
Me!DateTimeDeleted = Null
DoCmd.RunCommand acCmdSaveRecord
End If
Else
Me.Undo
End If
End Sub
I'm well aware that there's no code here to actually delete the record and I'm OK with that. My complaint is that the conditional formatting appears to be applied only when the form loads. Clicking the button above doesn't make the boxes turn pink for the record being "deleted". Is there some misunderstanding on my part here about how conditional formatting works?
FYI, I'm using MS Access 2013 32bit on Windows 8.1. Maybe that's the source of my problems. It isn't really my choice as the MS Design and Development Action Pack Subscription doesn't allow using Office 2010 anymore.
해결책
When dealing with issues like this, I like to use the Refresh
command. Your modified code would look like this.
Private Sub cmdDelete_Click()
If Me.NewRecord = False Then
If IsNull(Me!DateTimeDeleted) = True Then
Me!DateTimeDeleted = Now()
DoCmd.RunCommand acCmdSaveRecord
Else
Me!DateTimeDeleted = Null
DoCmd.RunCommand acCmdSaveRecord
End If
Me.Refresh 'This is the inserted code
Else
Me.Undo
End If
End Sub
다른 팁
You can also use Me.Recalc instead of Me.Refresh. Recalc does less overhead.