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

Javascript To Connect Onto an Access Database

Status
Not open for further replies.

jcpelejo

Programmer
Jul 29, 2001
70
US
Hello. I was wondering if there is a way of creating a web page that uses 3 drop down boxes to query an Access database. I am creating a website that when a user selects a year of a vehicle, it filters the vehicle make drop down box. After the make is selected, it filters the vehicle model drop down box and sends the user to a specific web page that is listed in the database. Please advise. Thank you. Life is too short to waste...
Julius Pelejo
jcpelejo@hotmail.com
 
Hi Julius,

If you're using Access, then you might actually do yourself a huge labor-saving favor and build a form that does what you want it to, then export the form as HTML.

It's not like having a custom-designed application, but it'll get you back in the race quickly.

Cheers,

[monkey] Edward [monkey] "Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
I found a way of using Javascript to filter the information but I had to use ASP to retrieve the information from the database. Here is the code.

<html>
<head>
<meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=iso-8859-1&quot;>
</head>

<body>

<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function clearCombo(elem)
{
for (i = elem.options.length; i >= 0; i--)
{
elem.options = null;
}
elem.selectedIndex = -1;
}

function selectCombo(elem, value)
{
for (i = 0; i <= elem.options.length; i++)
{
if (elem.options.value == value)
{
elem.selectedIndex = i;
}
}
}

function populateArea(elem)
{
for (var i = 0; i < a['0'].length; i= i + 2)
{
elem.options[elem.options.length] = new Option(a['0'][i + 1], a['0']);
}
}

function populateOffice(elem, index)
{
for (var i = 0; i < b[index].length; i= i + 2)
{
elem.options[elem.options.length] = new Option(b[index][i + 1], b[index]);
}
}

function populateDistrict(elem, index)
{
for (var i = 0; i < c[index].length; i= i + 2)
{
elem.options[elem.options.length] = new Option(c[index][i + 1], c[index]);
}
}

function clickArea(elem)
{
clearCombo(document.Form.office);
clearCombo(document.Form.district);
populateOffice(document.Form.office, elem[elem.selectedIndex].value);
populateDistrict(document.Form.district, document.Form.office[document.Form.office.selectedIndex].value);
return true;
}

function clickOffice(elem)
{
clearCombo(document.Form.district);
populateDistrict(document.Form.district, elem[elem.selectedIndex].value);
return true;
}

function resetControls()
{
populateArea(document.Form.area);
document.Form.area.selectedIndex = selectedArea;

clickArea(document.Form.area);
document.Form.office.selectedIndex = selectedOffice;

clickOffice(document.Form.office);
document.Form.district.selectedIndex = selectedDistrict;
}
//-->
</script>

<%
'Generate JavaScript arrays
Dim strOut, strOut2, strOut3
Dim selectedArea, selectedOffice, selectedDistrict
Dim dataData, cmdData, recArea, recOffice, recDistrict, strSQL
'To save form state, neat trick if you post form to itself like done here
Dim nJ, nK, nL

strOut = &quot;<script language=&quot;&quot;JavaScript&quot;&quot;>&quot; & vbCrLf
strOut = strOut & &quot;<!--&quot; & vbCrLf

strOut = strOut & &quot;var a = new Array();&quot; & vbCrLf
strOut = strOut & &quot;var b = new Array();&quot; & vbCrLf
strOut = strOut & &quot;var c = new Array();&quot; & vbCrLf
strOut = strOut & &quot;var d = new Array();&quot; & vbCrLf & vbCrLf

'Get all possible values for all combos
set dataData = server.createobject(&quot;ADODB.Connection&quot;)
connect = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; & Server.MapPath(&quot;linked_combos.mdb&quot;) & &quot;;Persist Security Info=False&quot;
dataData.Open connect

set cmdData = server.createobject(&quot;ADODB.Command&quot;)
cmdData.CommandType = 1
cmdData.ActiveConnection = dataData

'Get areas
strSQL = &quot;SELECT are_id, are_name FROM area &quot; & _
&quot;ORDER BY are_id&quot;

set recArea = server.createobject(&quot;ADODB.Recordset&quot;)
cmdData.CommandText = strSQL
recArea.Open cmdData,,1,1

strOut = strOut & &quot;a[&quot; & &quot;'0'&quot; & &quot;] = new Array(&quot; & vbCrLf
nJ = 0
while not recArea.EOF
strOut = strOut & vbTab & &quot;'&quot; & recArea(&quot;are_name&quot;) & &quot;','&quot; & recArea(&quot;are_name&quot;) & &quot;',&quot; & vbCrLf

'Get offices
strSQL = &quot;SELECT off_id, off_name FROM office &quot; & _
&quot;WHERE off_area = &quot; & recArea(&quot;are_id&quot;) & &quot; &quot; & _
&quot;ORDER BY off_id&quot;

set recOffice = server.createobject(&quot;ADODB.Recordset&quot;)
cmdData.CommandText = strSQL
recOffice.Open cmdData,,1,1

strOut2 = strOut2 & &quot;b['&quot; & recArea(&quot;are_name&quot;) & &quot;'] = new Array(&quot; & vbCrLf
nK = 0
while not recOffice.EOF
strOut2 = strOut2 & vbTab & &quot;'&quot; & recOffice(&quot;off_name&quot;) & &quot;','&quot; & recOffice(&quot;off_name&quot;) & &quot;',&quot; & vbCrLf

'Get districts
strSQL = &quot;SELECT dis_id, dis_name, url FROM district &quot; & _
&quot;WHERE dis_office = &quot; & recOffice(&quot;off_id&quot;) & &quot; &quot; & _
&quot;ORDER BY dis_id&quot;

set recDistrict = server.createobject(&quot;ADODB.Recordset&quot;)
cmdData.CommandText = strSQL
recDistrict.Open cmdData,,1,1

strOut3 = strOut3 & &quot;c['&quot; & recOffice(&quot;off_name&quot;) & &quot;'] = new Array(&quot; & vbCrLf
nL = 0
while not recDistrict.EOF
strOut3 = strOut3 & vbTab & &quot;'&quot; & recDistrict(&quot;dis_name&quot;) & &quot;','&quot; & recDistrict(&quot;dis_name&quot;) & &quot;',&quot; & vbCrLf

if recDistrict(&quot;dis_name&quot;) = Request.Form(&quot;district&quot;) then
selectedDistrict = nL
end if
nL = nL + 1
recDistrict.MoveNext
wend
strOut3 = Left(strOut3,Len(strOut3)-3) & &quot;);&quot; & vbCrLf

recDistrict.Close

if recOffice(&quot;off_name&quot;) = Request.Form(&quot;office&quot;) then
selectedOffice = nK
end if
nK = nK + 1
recOffice.MoveNext
wend
strOut2 = Left(strOut2,Len(strOut2)-3) & &quot;);&quot; & vbCrLf

recOffice.Close

if recArea(&quot;are_name&quot;) = Request.Form(&quot;area&quot;) then
selectedArea = nJ
end if
nJ = nJ + 1
recArea.MoveNext
wend
strOut = Left(strOut,Len(strOut)-3) & &quot;);&quot; & vbCrLf

recArea.Close

Response.Write strOut & strOut2 & strOut3 & &quot;//-->&quot; & vbCrLf & &quot;</script>&quot; & vbCrLf & vbCrLf
%>

<form ACTION=&quot;<%=Request.ServerVariables(&quot;SCRIPT_NAME&quot;)%>&quot; METHOD=&quot;post&quot; name=&quot;Form&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;action&quot; VALUE=&quot;Save&quot;>
<table cellpadding=&quot;3&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;>
<tr>
<td>
&nbsp;<br>
<font style=&quot;FONT-FAMILY:Verdana;FONT-SIZE:9pt;FONT-WEIGHT:bold;&quot;>Choose </font>
<span style=&quot;font-family: Verdana; font-weight: 700; font-size: 9pt&quot;>Vehicle</span>
</td>
<td>
<b>Year:</b><BR>
<select NAME=&quot;area&quot; SIZE=&quot;1&quot; onchange=&quot;clickArea(this);&quot;>
</select>
</td>
<td>
<b>Make:</b><BR>
<select NAME=&quot;office&quot; size=1 onchange=&quot;clickOffice(this);&quot;>
</select>
</td>
<td>
<b>Model:</b><BR>
<select NAME=&quot;district&quot; size=1>
</select>
</td>
<td>
&nbsp;<br>
<input type=&quot;submit&quot; value=&quot;Show&quot; name=&quot;Submit&quot;>
</td>
</tr>
</table>
</form>

<%
strOut = &quot;<script language=&quot;&quot;JavaScript&quot;&quot;>&quot; & vbCrLf
strOut = strOut & &quot;var selectedArea, selectedOffice, selectedDistrict;&quot; & vbCrLf & vbCrLf
if Request.Form <> vbNullString then
strOut = strOut & &quot;selectedArea = &quot; & selectedArea & &quot;;&quot; & vbCrLf
strOut = strOut & &quot;selectedOffice = &quot; & selectedOffice & &quot;;&quot; & vbCrLf
strOut = strOut & &quot;selectedDistrict = &quot; & selectedDistrict & &quot;;&quot; & vbCrLf
else
strOut = strOut & &quot;selectedArea = 0;&quot; & vbCrLf
strOut = strOut & &quot;selectedOffice = 0;&quot; & vbCrLf
strOut = strOut & &quot;selectedDistrict = 0;&quot; & vbCrLf
end if
strOut = strOut & &quot;onload = resetControls;&quot; & vbCrLf
strOut = strOut & &quot;</script>&quot; & vbCrLf

Response.Write strOut
%>

</body>
</html>
Life is too short to waste...
Julius Pelejo
jcpelejo@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top