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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional color formatting of field in a table 2

Status
Not open for further replies.

KingElvis

Programmer
Jan 28, 2004
39
IL
This is probably a simple question, but how does one format a table such that if a field is a certain value, then it will have a specific color.

For example, in the code below, if the specific field named "Year" is '2004' then I want it to appear in red, otherwise it should appear in black.

My code is:

<table>
<tr>
<th><p><b>Year</b></p></th>
<th><p><b>Surname</b></p></th>
<th><p><b>First Name</b></p></th>
<th><p><b>Sex</b></p></th>
</tr>

<% Do While Not rstSearch.EOF %>
<tr>
<td><%= rstSearch.Fields("year").Value %></td>
<td><%= rstSearch.Fields("surname").Value %</td>
<td><%= rstSearch.Fields("first").Value %></td>
<td><%= rstSearch.Fields("sex").Value %></td>
</tr>
<% rstSearch.MoveNext
Loop
%>

Many thanks,

J Scham
 
Give this a try...

<% Do While Not rstSearch.EOF %>
<tr>
<% if rstSearch.Fields("year").value = "2004" then
bgColor = "red"
else
bgcolor = white
end if
%>
<td bgcolor=<%=bgColor%>><%= rstSearch.Fields("year").Value %></td>
<td><%= rstSearch.Fields("surname").Value %</td>
<td><%= rstSearch.Fields("first").Value %></td>
<td><%= rstSearch.Fields("sex").Value %></td>
</tr>
<% rstSearch.MoveNext
Loop
%>
 
also, .fields and .value are a little overkill, you can just rstsearch("surname") and achieve the same results, the more you type the higher your chances for overlookable typos :)

also if YEAR is a numeric field, in ralph's example remove the quotes from <clip>= "2004" then</clip>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top