简体   繁体   English

更改 Access 报告中选定记录的颜色

[英]Change the color of a selected record in an Access REPORT

My Access REPORT has a text box with the Record ID that looks like a button with an on click event to go to a form for that specific record.我的访问报告有一个带有记录 ID 的文本框,它看起来像一个带有点击事件的按钮,用于转到该特定记录的表单。 This works great, but when I return to the report I cannot see which record was clicked.这很好用,但是当我返回报告时,我看不到点击了哪条记录。 I want to temporarily change ONLY the record that was clicked until another record is selected.我只想暂时更改被点击的记录,直到选择另一条记录。

The reason I want this on a report and not a form is because I want the user to have a quick way to proof read in the format needed to print, and make a change or check a detail if necessary, then update the report AFTER all proof reading and updates are completed and before final print.我希望在报告而不是表单上使用它的原因是因为我希望用户能够快速以打印所需的格式校对阅读,并在必要时进行更改或检查详细信息,然后在全部更新后更新报告校对和更新在最终印刷之前完成。 But with many records on the screen it is easy to lose track of which record you were checking when returning from the form.但是由于屏幕上有许多记录,当您从表单返回时,很容易忘记您正在检查的记录。

I tried:我试过了:

Private Sub btn_txt_GoToTransaction_Click()
  Dim vColor
  vColor = RGB(51, 204, 51) 'green
  Me.btn_txt_GoToTransaction.BackColor = vColor
  DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub

But this does not work because every button turns color not just the selected record.但这不起作用,因为每个按钮都会变色,而不仅仅是选定的记录。

Any suggestions?有什么建议么? Thanks.谢谢。

This is a great question because there are many benefits to highlighting a row or item in an Access Report.这是一个很好的问题,因为在访问报告中突出显示行或项目有很多好处。 But first, lets discuss the quick answer:但首先,让我们讨论一下快速答案:

QUICK ANSWER:快速回答:
In order to just highlight one row or one piece of data in a report, we need to use the "FormatConditions" property.为了突出显示报表中的一行或一条数据,我们需要使用“FormatConditions”属性。 This is the same as Conditional Formating from the MS Access design interface but we are going to change it programmatically on the fly.这与 MS Access 设计界面中的条件格式相同,但我们将在运行中以编程方式对其进行更改。 You can't do this with a button or label - it needs to be a text box with unique data, such as your TransactionID.您不能使用按钮或标签来执行此操作 - 它必须是具有唯一数据的文本框,例如您的 TransactionID。

STEP 1) I recommend that you add a text box to your report that stretches from the left to the right, set the Back Color and Fore Color to White, set the Control Source to TransactionID, and set the Name to TransactionID.第 1 步)我建议您在报告中添加一个从左到右延伸的文本框,将背景颜色和前景色设置为白色,将控制源设置为 TransactionID,并将名称设置为 TransactionID。 Then right click on this text box and select Position > Send To Back.然后右键单击此文本框并选择位置 > 发送到后面。 This works best if the other text boxes and labels on the report have a transparent background.如果报表上的其他文本框和标签具有透明背景,则此方法效果最佳。

STEP 2) Add this code:步骤 2)添加此代码:

Private Sub HightlightRow(intRowID As Integer)
    With Me.TransactionID.FormatConditions
        .Delete
        With .Add(acFieldValue, acEqual, intRowID)
            .BackColor = vbGreen
            .ForeColor = vbGreen
        End With
    End With
End Sub

STEP 3) Also change your button code to call this subroutine like this:步骤 3)还要更改您的按钮代码以调用此子例程,如下所示:

Private Sub btn_txt_GoToTransaction_Click()
  HightlightRow Me.TransactionID.Value
  DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub

OK, that was the quick answer, but keep reading if you would like more information.好的,这是快速回答,但如果您想了解更多信息,请继续阅读。

I like to set it up so if the user clicks anywhere in the row, it will pop up with a modal with more detail regarding that row.我喜欢设置它,因此如果用户单击行中的任何位置,它将弹出一个模式,其中包含有关该行的更多详细信息。 Also, the user can't make any changes to the data in the Report View, so I use the pop up modal to allow changes.此外,用户无法对报表视图中的数据进行任何更改,因此我使用弹出模式来允许更改。 To accomplish this, I do a couple more things:为了实现这一点,我还做了几件事:

First , we need to add the code to the OnClick event for every control in that row.首先,我们需要将代码添加到该行中每个控件的 OnClick 事件中。 Ofcourse, each OnClick event will simply can that subroutine HightlightRow Me.TransactionID.Value当然,每个 OnClick 事件都可以简单地使用该子程序HightlightRow Me.TransactionID.Value

Second , if the user clicks on a Text Box, the Text Box gets the focus and hides the highlight.其次,如果用户单击文本框,文本框将获得焦点并隐藏突出显示。 Therefore, I like to set the focus to something else.因此,我喜欢把重点放在别的事情上。 In your case, you could set the focus to the button by adding this line to the end of the HighlightRow subroutine: btn_txt_GoToTransaction.SetFocus在您的情况下,您可以通过将此行添加到 HighlightRow 子例程的末尾来设置按钮的焦点: btn_txt_GoToTransaction.SetFocus
In my case, I am not using a button, so I set up a tiny Text Box with = " " (just an equal sign a space in quotation marks) as the Control Source.在我的例子中,我没有使用按钮,所以我设置了一个带有= " "的小文本框(只是等号和引号中的空格)作为控制源。 Then I position this tiny Text Box to the far right.然后我把这个小文本框放在最右边。 And in the HighlightRow subroutine, I set the focus to this textbox.在 HighlightRow 子例程中,我将焦点设置到此文本框。


You may also want a button or method of removing the highlight.您可能还需要一个按钮或删除突出显示的方法。 To do that simply have the code run this line:为此,只需让代码运行以下行:

Me.TransactionID.FormatConditions.Delete

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM