1st Drop-down box w/ ASP & Access

AnakinVB
07-28-2002, 10:09 AM
Hello,
I am now attempting to add more capability to the ASP page that Rezner helped me create. I want my "AnakinCars.asp" page to enable users to select a style of car from my car selection Drop-down box. And when they press submit, their selection will appear in my one-line text box.

I have included the code & framework that I've written so far. I just do not know how to intergrate the ASP code into the form.
Can someone help me put these pieces together?

I will try to upload my *.mdb file (my uploads don't always work on this forum)
Here is my code so far:
-----------------------------------------------
<%

Set AnakinadoCon = Server.CreateObject("ADODB.Connection")

AnakinadoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("AnakinCars.mdb") & ";" & _
"PeRSist Security Info=False"

Set RS = Server.CreateObject("ADODB.Recordset")

RS.Open "SELECT carName FROM Cars ORDER BY carName", AnakinadoCon, 3, 3, 1

While NOT RS.EOF
response.write RS("carName") & "<BR>"

RS.Movenext
Wend

'Close and destroy ADODB Recordset object
RS.Close
Set RS = Nothing

'Close and destroy ADODB Connection object
AnakinadoCon.Close
Set AnakinadoCon = Nothing


%>

<p>
<h3> Anakin's Car Selection </h3>

<!-- i obviously hardcoded below, I want to retrieve this list from my
access file - AnakinCars.mdb -->

<FORM Method=Post ACTION = mailto:evilanakin@aol.com>
Pick your set of wheels: <SELECT NAME = Vehicle>
<OPTION = Cadillac > Cadillac
<OPTION = Jaguar > Jaguar
<OPTION = Mercedes > Mercedes
<OPTION = Porsche 911 > Porsche 911
</SELECT>
<p>
<INPUT TYPE = SUBMIT VALUE = "Submit Selection">
</p>

<p>
Your car selection: <INPUT NAME = vehicle SIZE = 40>
</p>
</form>
-----------------------------------------------
thx,
Anakin

Rezner
07-28-2002, 12:08 PM
This should generate a combobox with the car names. Note that I used an "ID" field to refer to the car. I usually give every record an autonumbered numerical field as the primary key:<%

Set AnakinadoCon = Server.CreateObject("ADODB.Connection")

AnakinadoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("AnakinCars.mdb") & ";" & _
"PeRSist Security Info=False"

Set RS = Server.CreateObject("ADODB.Recordset")

RS.Open "SELECT ID, carName FROM Cars ORDER BY carName", AnakinadoCon, 3, 3, 1

response.write "<SELECT>"
While NOT RS.EOF
response.write "<OPTION VALUE=" & RS("ID") & ">" & RS("carName") & "</OPTION>"

RS.Movenext
Wend
response.write "</SELECT>"

'Close and destroy ADODB Recordset object
RS.Close
Set RS = Nothing

'Close and destroy ADODB Connection object
AnakinadoCon.Close
Set AnakinadoCon = Nothing

%>

AnakinVB
07-28-2002, 02:36 PM
That's cool and I see how it works. Inside my "cars" table I do use an autonumbered key - "carID."
Now, if this were VB6, I could enable the user to make a selection, press a "Select" button and have the list item show up in a text box. BUT, I don't really know where to start here. Can Rezner or another expert show me how?
Was I in the ballpark with the form I started when I began this thread?
thx

Rezner
07-28-2002, 03:58 PM
Use a javascript event in your combo box:
onClick="yourFormName.yourTextbox.value=yourFormName.yourCombo.value";
(Note, if you set the VALUE property to be an ID number, then that is what well show up. So, you should probably set it to be the actual car name.

AnakinVB
07-28-2002, 05:35 PM
Ok, before I attempt this, I have one major question that will help me understand the differences between ASP and regular VB: When I set up my one line text box, can I just code it in the page like I did when I started this thread? Or, do I have to "respons.write" every single object of a form?
thx

Rezner
07-28-2002, 06:12 PM
Really, the only things you should "response.write" are the dynamic elements of your page (red below). The static elements (in blue below) should be typed normally outside of the script blocks.

Example: If you know that you will always have a certain combobox, but don't know its exact contents, then it would look something like this:
<SELECT NAME="cboMyCombo">

<%
While Not RS.EOF
response.write "<OPTION>" & RS("carName") & "</OPTION>"

RS.MoveNext
Wend
%>
</SELECT>
Here, the script does not know the contents of the combo until run time -- the dynamic contents (in red above) are extracted. Response.write is used for this.

AnakinVB
07-29-2002, 07:40 AM
I see your logic and it makes sense to me, but in practice, my asp page crashes right at:
<SELECT NAME="cboMyCombo">
------------------------------

Here it the error I get:

Microsoft VBScript compilation error '800a0400'

Expected statement

/audit/anakincars.asp, line 21

<SELECT NAME="cboMyCombo">

--------------------------------------------
And here is the latest version of my code that you helped me write:

<%

Set AnakinadoCon = Server.CreateObject("ADODB.Connection")

AnakinadoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("AnakinCars.mdb") & ";" & _
"PeRSist Security Info=False"

Set RS = Server.CreateObject("ADODB.Recordset")

RS.Open "SELECT carID, carName FROM Cars ORDER BY carName", AnakinadoCon, 3, 3, 1

'response.write "<SELECT>"
'While NOT RS.EOF
'response.write "<OPTION VALUE=" & RS("carID") & ">" & RS("carName") & "</OPTION>"

'RS.Movenext
'Wend
'response.write "</SELECT>"

<SELECT NAME = "cboMyCombo">

<%
While Not RS.EOF
response.write "<OPTION>" & RS("carID") & RS("carName") & "</OPTION>"

RS.MoveNext
Wend
%>
</SELECT>


'Close and destroy ADODB Recordset object
RS.Close
Set RS = Nothing

'Close and destroy ADODB Connection object
AnakinadoCon.Close
Set AnakinadoCon = Nothing

%>
------------------------

Any idea why I'm erroring out right where I start the combo box?
thx,
Anakin

Thinker
07-29-2002, 07:56 AM
You need to insert a %>

...
'RS.Movenext
'Wend
'response.write "</SELECT>"
'Add the following line
%>
<SELECT NAME = "cboMyCombo">

<%
...

AnakinVB
07-29-2002, 08:46 AM
Thx but, it still crashes on that same line:


Microsoft VBScript compilation error '800a0400'

Expected statement

/audit/anakincars.asp, line 23

<SELECT NAME = "cboMyCombo">
^



I have tried what you recommended:

'RS.Movenext
'Wend
'response.write "</SELECT>"

%>
<%
<SELECT NAME = "cboMyCombo">

%>
<%
While Not RS.EOF
response.write "<OPTION>" & RS("carID") & RS("carName") & "</OPTION>"

RS.MoveNext
Wend

</SELECT>

%>

No luck.


And I have tried:

<%
<SELECT NAME = "cboMyCombo">


While Not RS.EOF
response.write "<OPTION>" & RS("carID") & RS("carName") & "</OPTION>"

RS.MoveNext
Wend

</SELECT>

%>


Neither works - they both error out at :

<SELECT NAME = "cboMyCombo">


** If I can get just one of these working, I'll probably understand it. But this mix of VB code and forms seems very tricky right now. Do you have any more ideas for my example? I'll try anything at this point.
thx,
Anakin

Rezner
07-29-2002, 08:50 AM
You need to understand, conceptually, what goes inside the script tags and what goes outside the script tags. Don't just do it by memorization or copy and pasting. It should look like this:
<%


Set AnakinadoCon = Server.CreateObject("ADODB.Connection")

AnakinadoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("AnakinCars.mdb") & ";" & _
"PeRSist Security Info=False"

Set RS = Server.CreateObject("ADODB.Recordset")

RS.Open "SELECT carID, carName " & _
"FROM Cars ORDER BY carName", _
AnakinadoCon, 3, 3, 1

response.write "<SELECT>"

While NOT RS.EOF
response.write _
"<OPTION VALUE=" & RS("carID") & ">" & RS("carName") & "</OPTION>"

RS.Movenext
Wend
response.write "</SELECT>"
%>

<SELECT NAME = "cboMyCombo">

<%

While Not RS.EOF
response.write "<OPTION>" & RS("carID") & RS("carName") & "</OPTION>"

RS.MoveNext
Wend
%>

</SELECT>

<%
'Close and destroy ADODB Recordset object
RS.Close
Set RS = Nothing

'Close and destroy ADODB Connection object
AnakinadoCon.Close
Set AnakinadoCon = Nothing

%>
If you want something to appear in HTML code and it's between the script tags, then you must use Response.Write. If it's HTML and it's not between a script tag, then you can just write it directly.

AnakinVB
07-29-2002, 09:18 AM
OK thanks, I see that you are setting up the controls with just HTML and dynamically constructing the components (like option). In the code above, your list box does not become populated with the selection. I am going to spend the next hour or so attempting to integrate the javascript you provided above so that my program will populate a text box with the selection . Wish me luck!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum