我们先依据一个表(表1)建立一个数据表形式的窗体,这个表里有一个ID字段(字段名称例如:货物ID)
在这个窗体中添加一个文本框,命名为:Rank,控件来源
如果货物ID字段是数值型: =DCount("*","表1","货物ID<=" & [货物ID] & "")
如果货物ID字段是文本型: =DCount("*","表1","货物ID<='" & [货物ID] & "'")
然后在本窗体的模块中写如下代码:
Private Sub Form_Current()
On Error Resume Next
Dim cuid As Long
cuid = Me.CurrentRecord
Dim objfrc As FormatCondition
Dim ctl As Control
For Each ctl In Me.Form.Controls
Set objfrc = ctl.FormatConditions(0).Modify(acExpression, , "[Rank] = " & cuid & "")
Next
End Sub
Private Sub Form_Load()
On Error Resume Next
Dim cuid As Long
cuid = Me.CurrentRecord
Dim objfrc As FormatCondition
Dim ctl As Control
For Each ctl In Me.Form.Controls
ctl.FormatConditions(0).Delete
Set objfrc = ctl.FormatConditions.Add(acExpression, , "[Rank] = " & cuid & "")
ctl.FormatConditions(0).BackColor = 255
Next
End Sub
以上代码即可实现当前行高亮显示
如果你还想实现行颜色交替显示,那么把代码改成如下:
Private Sub Form_Current()
On Error Resume Next
Dim cuid As Long
cuid = Me.CurrentRecord
Dim objfrc As FormatCondition
Dim ctl As Control
For Each ctl In Me.Form.Controls
Set objfrc = ctl.FormatConditions(0).Modify(acExpression, , "[Rank] Mod 2 = 0 And [Rank] <>" & cuid & "")
Set objfrc = ctl.FormatConditions(1).Modify(acExpression, , "[Rank] = " & cuid & "")
Next
End Sub
Private Sub Form_Load()
On Error Resume Next
Dim cuid As Long
cuid = Me.CurrentRecord
Dim objfrc As FormatCondition
Dim ctl As Control
For Each ctl In Me.Form.Controls
ctl.FormatConditions(0).Delete
ctl.FormatConditions(1).Delete
Set objfrc = ctl.FormatConditions.Add(acExpression, , "[Rank] Mod 2 = 0 And [Rank] <>" & cuid & "")
Set objfrc = ctl.FormatConditions.Add(acExpression, , "[Rank] = " & cuid & "")
ctl.FormatConditions(0).BackColor = 16777164
ctl.FormatConditions(1).BackColor = 255
Next
End Sub