vba 如何更改 Access 中特定字段中部分文本的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17440902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:02:21  来源:igfitidea点击:

How to change color for part of the text in a particular field in Access?

vbams-accessaccess-vba

提问by Nexus

In Access, I have a table which contain a field like this:

在 Access 中,我有一个包含如下字段的表:

Part Number

零件号

A/B/C

A/B/C

A/B/R

A/B/R

T/Y/V

时间/年/时间

D/A/I

D/A/I

I want to change the color of the all the third part to red. So in this case C,R,V,I will be colored red. But I can't do change the color of part of the text field in Access 2007. If I use Font Change under Home tab it change the Font of entire Table. I'm so disappointed about Microsoft. Is there any way to change the color would be great :D You can recommend VBA , Macro in Form, Query, Report ... P/S: I use Access 2007

我想将所有第三部分的颜色更改为红色。所以在这种情况下,C,R,V,I 将被涂成红色。但是我无法在 Access 2007 中更改部分文本字段的颜色。如果我在“主页”选项卡下使用“字体更改”,它会更改整个表格的字体。我对微软很失望。有什么方法可以改变颜色会很棒:D 你可以推荐 VBA , 窗体中的宏, 查询, 报告... P/S: 我使用 Access 2007

采纳答案by Nexus

ok I think the only way is to export automatically to Excel. Finally I can do this

好的,我认为唯一的方法是自动导出到 Excel。最后我可以做到这一点

Private Sub CommandExport_Click()
Dim db As Database
Dim rec1 As Recordset
Dim xlFile As Object
Dim xlWorkBook As Object
Dim xlActiveWkb As Object
Dim xlActiveSheet As Object
Dim iCols, iRows, flag As Integer

Set db = CurrentDb
Set xlFile = CreateObject("Excel.Application")
Set xlWorkBook = xlFile.Workbooks.Add
Set xlActiveWkb = xlFile.Application.ActiveWorkBook

xlFile.Visible = True
xlActiveWkb.Sheets.Add
xlActiveWkb.Worksheets(1).Name = "My_Report"

Set xlActiveSheet = xlActiveWkb.Worksheets("My_Report")
Set rec1 = db.OpenRecordset("Report")

For iCols = 0 To rec1.Fields.Count - 1
    xlActiveSheet.Cells(1, iCols + 1).Value = rec1.Fields(iCols).Name
    If rec1.Fields(iCols).Name = "FS Number" Then
        flag = iCols
    End If
Next

xlActiveSheet.Range(xlActiveSheet.Cells(1, 1), xlActiveSheet.Cells(1, rec1.Fields.Count)).Font.Bold = True
xlActiveSheet.Range(xlActiveSheet.Cells(1, 1), xlActiveSheet.Cells(1, rec1.Fields.Count)).Interior.ColorIndex = 15
xlActiveSheet.Cells(2, 1).CopyFromRecordset rec1
xlActiveSheet.Columns("A:AD").EntireColumn.AutoFit

iRows = 1
rec1.MoveFirst
While Not rec1.EOF
    xlActiveSheet.Cells(iRows + 1, flag + 1).Characters(InStr(rec1![FS Number], "*")).Font.ColorIndex = 3
    iRows = iRows + 1
    rec1.MoveNext
Wend

Set xlSheet = Nothing
Set xlWorkBook = Nothing
Set xlActiveWkb = Nothing
rec1.Close
db.Close
Set rec1 = Nothing
Set db = Nothing

End Sub

结束子

The magic is here

魔法就在这里

xlActiveSheet.Cells(iRows + 1, flag + 1).Characters(InStr(rec1![FS Number], "*")).Font.ColorIndex = 3

回答by Glen Little

if you can use an Access report, you can add a TextBox to the report. In the textbox, you can have a formula like this:

如果您可以使用 Access 报表,则可以向报表添加一个文本框。在文本框中,您可以使用如下公式:

="<font color=""blue"">" & [ColumnA] & "</font> <u>" & [ColumnB] & "</u>"

See Access Rich-Text: Which subset of HTML is supported?for more details.

请参阅访问富文本:支持哪个 HTML 子集?更多细节。