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!

How to get the intended selected value in a drop-down list ?

Status
Not open for further replies.

leifoet

Technical User
Jan 31, 2016
203
BE
Conditions
If the date of the activity is older than today's date => print the activity in black
If the date of the activity is younger than the date of today => print the activity in color
The selected activity date must indicate the nearest activity date in the future

The code below does not give the desired selected activity value - the first activity date is selected
Thanks for tips.


<select NAME="ActId">
<%Do While Not rs.EOF%>
<%if rs("ActDate")>date() then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#0000FF;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDate")=date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDatum")<date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDatum")<date() and rs("ActDatum")+1>date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%end if%>

<%rs.MoveNext
Loop%>
</select>
 
The code

<select NAME="ActId">
<%Do While Not rs.EOF%>
<%if rs("ActDate")>date() then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#0000FF;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDate")=date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDatum")<date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDatum")<date() and rs("ActDatum")+1>date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected alue="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%end if%>

<%rs.MoveNext
Loop%>
</select>
 
This, selected alue= probably doesn't help.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Is rs("ActDate") in the same format as the system date is?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Okay, I'll rephrase the question and add why I ask.

In ASP vbscript all variable are of type variant, they are not a specific type and values may NOT be what you expect.

So, the question now is;

When printed to the browser, does rs("ActDate") LOOK the same as date() does?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Exact the same look as the computer system date.
 
Code:
<%if rs("ActDate")>date() then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#0000FF;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDate")=date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDatum")<date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
[highlight #FCE94F]<%ElseIf rs("ActDatum")<date() and rs("ActDatum")+1>date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" selected alue="<%=rs("ActId")%>"><%=rs("Description")%></option>[/highlight]
<%end if%>

1) I'm assumung rs("ActDate") / rs("ActDatum") is another typo... not sure why you dont copy/paste actual code snippets, then you dont have to worry about typos
2) The highlighted code will never execute. If rs("ActDatum") is earlier than the current date, it gets caught in the "Elseif" above. You should reverse the order of those statements.
3) You have the colors off... the first "If" statement says "If the database date is older than the system date, show it in blue" which is not what you wanted in your OP.
4) ChrisHirst's points are valid. What I used to do when in doubt is use some response.write statements instead of generating the html code, so I can see what is happening. Make sure the dates are being evaluated correctly. Like below (not tested at all)

Code:
<%Do While Not rs.EOF

If rs("ActDate") > date() then
   response.write rs("ActDate") & ": is older than " & date()
ElseIf rs("ActDate") = date() Then
   response.write rs("ActDate") & ": is the same as " & date()
ElseIf rs("ActDatum") < date() Then
   response.write rs("ActDate") & ": is earlier than " & date()
End If

Loop%>

<%response.end%>
 
First, guitarzan, sorry for the typos and the invalid (yellow) if code.
I think I did not describe my problem properly and try again.

I am looking for the right code to select=display in a drop-down list (from database table) : the correct 'selected value'
from this options
(1) if rs("Actdate") is the same as date() => no problem => display rs("Actdate") as 'selected value'
(2) if rs("Actdate") is not the same as date() => the 'selected value' must display the nearest activity date in the future = nearest rs("Actdate") from today

an example/scheme of the drop-down list
- older activity (color is not important e.g. black)
- older activity
- selected activity =high lighted by the cursor - this activity will happen e.g. the day after tomorrow or ... next week or ...
- younger activity (color not important e.g. red)
- younger activity

How to 'select' the nearest activity date from [rs("Actdate")] in this drop-down list ?
Thanks for tips.

Another question (coming from FrontPage)
When do I use : loop
and when : rs.MoveNext Loop
at the end of Do While Not rs.EOF
 
To possibly answer your last question at least, there should always be a "rs.MoveNext" before the "Loop", otherwise the code loops endlessly. My sample missed the "rs.MoveNext", sorry about that.
 
By trial and error I finally found a solution that works.
Perhaps to improve or to refine ?
Thanks for tips.

<%
Do While rs("ActDate")<=date()
rs.MoveNext
selectiondate=rs("ActDate")
Loop
%>

<select NAME="ActId" style="font-size: 12px;width:600px;">

<%
rs.MoveFirst
Do While Not rs.EOF
%>

<%if rs("ActDate")<date() then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#000000;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDate")=selectiondate Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#0000FF;" selected value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%ElseIf rs("ActDate")>date() Then%>
<option style="font-family: Arial, Helvetica, sans-serif;color:#0000FF;" value="<%=rs("ActId")%>"><%=rs("Description")%></option>
<%End if%>

<%
rs.MoveNext
Loop
%>
</select>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top