INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

VBA to change Fore Color for designated table cells

VBA to change Fore Color for designated table cells

(OP)
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.


RE: VBA to change Fore Color for designated table cells

You would have to create a recordset from tblOutput and an array from tblTargetCells and loop through them.

RE: VBA to change Fore Color for designated table cells

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])

RE: VBA to change Fore Color for designated table cells

You can try code like this assuming your text boxes are named the same as the fields:

CODE --> vba

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

RE: VBA to change Fore Color for designated table cells

(OP)
Thanks to all who responded. I used Duane's solution, adding other properties like FontWeight as well.
Teach314

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close