View Full Version : select a table with drop down box
I want to select a table with a drop down box, but when a person chooses a option in the box how i link it with the right table in the database?
For exemple:
<form method="POST" name="form1" action="displaygraficas.asp">
<p><select size="1" name=Dropdown1 onchange=form1.submit()>
<option value="placasgraficas">graphic card</option>
<option value="monitores">Monitores</option>
after i get the option i did this in the displaygraficas.asp file:
strSQL = "SELECT placasgraficas.placagrafica, placasgraficas.preco, placasgraficas.quantidade FROM Dropdown1;"
Dropdown1 is the variable that has the name of the table selected by the user.
I know that this is wrong but how can i put it work?
Thanks in advance!
zenweezil
12-05-2005, 08:50 PM
Your variable is actually - Request.Form("Dropdown1")
So you might just need to change your SQL string to:
strSQL = "SELECT placasgraficas.placagrafica, placasgraficas.preco, placasgraficas.quantidade FROM ' " & Request.Form("Dropdown1") & " ' ";
This is my html code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Escolher tabela</title>
</head>
<body>
<form method="POST" name="form1" action="Escolhertabela.asp">
<p><select LANGUAGE=javascript name=table onchange=form1.submit()>
<option value="placasgraficas">Placas Graficas</option>
<option value="monitores">Monitores</option>
</select>
</form>
</body>
</html>
This is the Escolhertabela.asp script:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Escolher tabela</title>
</head>
<body>
<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database
Dim table
'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")
'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("bd1.mdb")
'Set an active connection to the Connection object using DSN connection
'adoCon.Open "DSN=guestbook"
'Create an ADO recordset object
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT "Request.Form("table")".placagrafica, "Request.Form("table")".preco, "Request.Form("table")".quantidade FROM" & Request.Form("table");
Set rsGuestbook = adoCon.Execute(strSQL)
'Open the recordset with the SQL query
rsGuestbook.Open strSQL, adoCon
'Loop through the recordset
Do While not rsGuestbook.EOF
'Write the HTML to display the current record in the recordset
Response.Write ("<br>")
Response.Write (rsGuestbook("placagrafica"))
Response.Write ("<br>")
Response.Write (rsGuestbook("preco"))
Response.Write ("<br>")
Response.Write (rsGuestbook("quantidade"))
Response.Write ("<br>")
'Move to the next record in the recordset
rsGuestbook.MoveNext
Loop
'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>
When i run it and choose Placas Graficas it gives me this error:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause.
/displayproduto.asp, line 30
What im doing wrong??
Thanks in advance!
Brandoe85
12-06-2005, 01:16 AM
You have to make sure you don't have conflicting quotes, give this a go:
strSQL = "SELECT " & Request.Form("table") & ".placagrafica, " & Request.Form("table") & ".preco, " & Request.Form("table") & ".quantidade FROM " & Request.Form("table")
Good luck;
now it gives me this error:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/Escolhertabela.asp, line 31, column 43
strSQL = "SELECT " & Request.Form("table") ".placagrafica," &Request.Form("table") ".preco," & Request.Form("table") ".quantidade FROM " & Request.Form("table")
In c++ i know that i need something to finish the code but i dont know what it is in this case i tried ; but didnt work.
Thanks to all i solved the problem!!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.