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!

help auto selecting a default value on drop down

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
My form submits to the same page. Once the form is submitted and the page reloads, I want the value selected in the drop-down list, before the form was submitted, to be automatically selected.

Here's how I'm trying it, but it's not working. BTW, the drop down is being dynamically populated from a db...that part does work.

Code:
strSQL = "SELECT specialty "
strSQL = strSQL & "FROM [Provider 2006] group by specialty"
oRs.Open strSQL, oConn, adOpenStatic, adLockReadOnly

WHILE NOT oRs.EOF 
	IF oRs("specialty") = sSpecialty THEN
	    prioritytypes = prioritytypes & "<option selected value=" & sSpecialty & ">" & sSpecialty & "</option>"
	ELSE
	    prioritytypes = prioritytypes & "<option value=" & oRs("specialty") & ">" & oRs("specialty") & "</option>"
	END IF
	oRs.MoveNext
WEND

oRs.Close
oConn.Close
...and in the form, I have: <select size="1" name="Specialty"><%=prioritytypes%></select>

Any help is much appreciated. Thanks!
 
It looks good except up above this code you need something to set the value of your variable [tt]sSpecialty[/tt]

Something like this:[tt]
sSpecialty = Request("Specialty")[/tt]


Also, if there is any change that the field in your database could possibly be null, it is best to make your [tt]IF/THEN[/tt] like this:[tt]
IF oRs("specialty")[red] & ""[/red] = sSpecialty THEN
[/tt]

By concatinating the value with an empty string, you ensure that any Null value is converted to a string value. This helps you avoid the troublesome "Invalid use of Null" errors that pop up occasionally when working with recordset values.
 
Sheco,

I'm actually already setting the value of the variable, I just didn't included that in my code post. It looks like this: sSpecialty = Request.QueryString("Specialty")

I added your empty string suggestion to. I'm still not getting it to auto-select the previously selected value. It just goes back to the first value in the list, which happens to be blank.

Here's the live page I'm talking about:

Any other suggestions?
 
Nevermind, I figured it out. The problem was that I wasn't surrounding the input value with quotes. Unfortunately for this situation, the input values aren't id numbers, they are text just like the displayed values.

It's working now.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top