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

Populate a select control based on contents of another

Status
Not open for further replies.

jwalz

Programmer
Oct 31, 2000
78
US
Please Help!

I am trying to populate the options available in a <SELECT> control based on the user's selection of another <SELECT> control on the same form using the OnChange event. How do I either connect to my Access database query to pull a new list from within my VBScript subroutine or can I run an Active Server Page script from within the VBScript Sub? I know how to build the list, I just can't get at the data.

Here is my VBScript Sub on my .ASP page:
<script LANGUAGE=&quot;VBScript&quot;>
Sub Client_onchange()
i = 0
t_client=Document.addOpp.Client.value
'Get the new query list
'I now know that this is .ASP server side scripting, but don't know what to replace it for client side scripting
set con = Svr.CreateObject(&quot;ADODB.Connection&quot;)
con.Open(&quot;OppFilter&quot;)
set contacts2=con.Execute(&quot;qryClientContacts&quot;)
Do While Not contacts2.EOF
if (contacts2.Fields(0) = t_client) then
Document.addOpp.Contact.options.value=contact2.Fields(3)
Document.addOpp.Contact.options.text=contact2.Fields(1)
i = i + 1
end if
contacts2.MoveNext
Loop
End Sub
</script>

Thanks,
Janet
 
Here is how I did it on mine, syntax is different, but should help you out... I found it easier to just use the javascript onchange event directly from the form component. the checks are because mine is a recursive form, if you only need to do it once, you can get rid of them. And of course, the name of my form is form1 using the post method,.

varDepartment = server.HtmlEncode(Request.form(&quot;department1&quot;))
varCourse = server.HtmlEncode(Request.form(&quot;course1&quot;))
varSection = server.HtmlEncode(Request.form(&quot;section1&quot;))
rec = server.HtmlEncode(Request.form(&quot;rec1&quot;))

Set deplist = Server.CreateObject(&quot;ADODB.Connection&quot;)
deplist.Provider = &quot;Microsoft.Jet.OLEDB.4.0&quot;
deplist.ConnectionString = &quot;Data Source=&quot; & Server.MapPath (&quot;\data\deplist.mdb&quot;)
deplist.Open

if varDepartment <> &quot;&quot; then
response.write &quot;<select size=&quot;&quot;1&quot;&quot; name=&quot;&quot;Department1&quot;&quot; onChange=&quot;&quot;javascript:document.form1.submit()&quot;&quot;>&quot;
response.write &quot;<option value='&quot; & varDepartment & &quot;'>&quot;
response.write varDepartment
response.write &quot;</option>&quot;
response.write &quot;</select>&quot; & &quot;</td>&quot;
else
response.write &quot;<select size=&quot;&quot;1&quot;&quot; name=&quot;&quot;Department1&quot;&quot; onChange=&quot;&quot;javascript:document.form1.submit()&quot;&quot;>&quot;
response.write &quot;<option value=&quot;&quot;Choose&quot;&quot;>&quot;
response.write &quot;</option>&quot;
do while NOT rs.eof
response.write &quot;<option value='&quot; & rs(&quot;department&quot;) & &quot;'>&quot;
response.write rs(&quot;department&quot;)
response.write &quot;</option>&quot;
rs.MoveNext
loop
response.write &quot;</select>&quot; & &quot;</td>&quot;
end if
set rs=nothing
 
fergman,

I think that I see what you are doing, but where do you associate the vairable rs to deplist?

Thanks for your help.
Janet
 
I had option explicit turned off on this particular example (bad habit I know), so I didn't have it defined at all, but you can define it anywhere above, I used it as a global variable on another example with option explicit.
 
fergman,

Please forgive my ignorance, but how do you 1) determine the position on the form that the new control will be created and 2) remove the previous control?

Thanks,
Janet
 
It's not quite that easy unforunatly. You must first build the page like normal, and when the form is submitted, it will use the tables or stylesheet that you origionally created. if not it will just place them one right after the other. I suggest building the basics first see how it lays them out, then start building tables into them until you get a feel for it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top