Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to change Fore Color for designated table cells 1

Status
Not open for further replies.

teach314

Technical User
Jul 29, 2011
183
CA
hi to all

I have a table (tblTargetCells) with 2 long fields, col and row. There are typically about 100 records.

Code:
     col   row
 ========
 1     3
 1     12
 1     25
 2     4
 2     35
 3     17
... etc
 22    18
 22    50


There is a 2nd table (tblOutput) that has long values in, say, 23 fields (row, c1, c2, c3, ... c22).

Code:
     row     c1   c2   c3   ...   c22
 ==================================
 1       5    7    2    ...   45
 2       42   6    31   ...   21
 3       11   54   14   ...   99
... etc
 21      31   5    71   ...   38
 22      4    65   30   ...   20


A report,(rptOutput), presents the data exactly as it appears in tblOutput.

Here's my question. In the Report, I want Fore Color = 255 for ALL cells identified in tblTargetCells.
For example, the first record of tblTargetCells indicates that we need to change the Fore Color of the 11 found in column c1 and row 3 of the Report.


Could some kind soul sketch out VBA that would do this? Even a rough outline would really help.
Thanks in advance.


 
You would have to create a recordset from tblOutput and an array from tblTargetCells and loop through them.
 
Simply make a function
Code:
Public Function IsTarget(cellNumber As Long) As Boolean
  IsTarget = (DCount("*", "tblTargetCells", "cellValue = " & cellNumber) > 0)
End Function
Then use conditional formatting for the color you want.
Expression: isTarget([c1])
 
You can try code like this assuming your text boxes are named the same as the fields:

Code:
Option Compare Database
Dim rs As DAO.Recordset
Dim db As DAO.Database

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    Dim intRow As Integer
    Dim intCol As Integer
    intRow = Me.row
    With rs
        For intCol = 1 To 22
            .FindFirst "Row = " & intRow & " AND Col = " & intCol
            If .NoMatch Then
                Me("c" & intCol).ForeColor = vbBlack
             Else
                Me("c" & intCol).ForeColor = 255
            End If
        Next
    End With
End Sub

Private Sub Report_Close()
    rs.Close
    Set db = Nothing
End Sub

Private Sub Report_Open(Cancel As Integer)
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT Row, Col FROM tblTargetCells")
End Sub


Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Thanks to all who responded. I used Duane's solution, adding other properties like FontWeight as well.
Teach314
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top