Building an Input Form Dynamically From a Database.
Updated 3/23/2001 11:17 PM !!!!!!!!!!!!!!!!!!!!! This code will allow you to build an input form "on the fly" from a database. Instead of creating and hard coding the same old input form you can automatically build your form based on column names or values in your database! Just added is another page that will submit your data to your database "on the fly", truely making the process of creating web based entry forms DYNAMIC!!!!!!!! Now whenever you have to alter your input forms you just have to change your SELECT statement, no more HTML!! Please vote for this code if you find it useful, and feel free to send me any questions you have regarding the code. Thank you.
Ringkasan AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
<html>
<body>
<p>This is the first of 2 Pages you will need...</p>
<p><font face="Arial" size="1"><font color="#000080"><html><br>
<body><br>
<Form Name="Form" Type="Submit" Method="Post" Action="dynamic2.asp"></font><br>
<br>
<font color="#FF0000"><%</font><br>
<font SIZE="1" color="#808000"><!--~----- Dim Your Variables -----~--></font><br>
<font color="#000080"><b>Dim</b></font> MyConn, SQL, RS, i<br>
</font></p>
<p><font face="Arial" size="1"><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">Create the Connection and Open the Connection to the Database
</font><font SIZE="1" color="#808000">-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">This Will Connect to a MS-SQL Database</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">To Connect to an Access Database Replace the Bottom 3 Lines of Code with either...</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">MyConn.Open "Driver={Microsoft Access Driver
(*.mdb)};</font><font SIZE="1" color="#808000"> -----~--></font><font color="#808000"> <br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">DBQ=C:\Databases\demo.mdb"
</font><font SIZE="1" color="#808000">-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">or</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"> <br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">Source=C:\Databases\demo.mdb"</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">or</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">MyConn.Open DSN_Name</font><font SIZE="1" color="#808000">
-----~--></font><br>
<font color="#000080"><b>Set</b></font> MyConn=Server.<b>CreateObject</b>("<font color="#008080">ADODB.Connection</font>")<br>
Myconn.<b>ConnectionString</b> = "<font color="#008080">Provider=SQLOLEDB; Data
Source=</font></font><font face="Arial" size="2" color="#800080">Your SQL Server</font><font color="#008080"><font face="Arial" size="1">; Initial Catalog=</font></font><font face="Arial" size="2" color="#800080">Your
Database</font><font face="Arial" size="1"><font color="#008080">; User ID=</font></font><font color="#800080" face="Arial" size="2">Your
Login</font><font face="Arial" size="1"><font color="#008080">; Password=</font></font><font color="#800080" face="Arial" size="2">Your
Password</font><font face="Arial" size="1">"<br>
Myconn.<b>open</b><br>
</font></p>
<p><font face="Arial" size="1"><font SIZE="1" color="#808000"><!--~-----
Wriye your SQL statement</font><font color="#808000"> </font><font SIZE="1" color="#808000">-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- In this case we have 2 SQL
statements because I have a different value for my form fields other than its
name -----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- If your checkbox/text field
will have the same name and value you can safely remove from "</font>SQL1 =
"<font color="#008080">Select" down</font><font SIZE="1" color="#808000">
-----~--></font><br>
SQL = "<font color="#008080">Select </font></font><font face="Arial" size="2" color="#800080">Column
Name(s) in your Database that holds values that will become the checkbox/textfield
name </font><font face="Arial" size="1"><font color="#008080">From </font></font><font color="#800080" face="Arial" size="2">Your
Table Name</font><font face="Arial" size="1">"<br>
<font color="#000080"><b>Set</b></font> RS = MyConn.<b>Execute</b>(SQL)<br>
SQL1 = "<font color="#008080">Select </font></font><font face="Arial" size="2" color="#800080">Column
Name in your Database that holds values that will become the checkbox/textfield
value</font><font face="Arial" size="1"><font color="#008080"> From </font></font><font color="#800080" face="Arial" size="2">Your
Table Name</font><font face="Arial" size="1">"<br>
<font color="#000080"><b>Set</b></font> RS1 = MyConn.<b>Execute</b>(SQL1)<br>
</font></p>
<p><font face="Arial" size="1"><font SIZE="1" color="#808000"><!--~----- </font><font color="#808000">Read All of the Records with EOF
</font><font SIZE="1" color="#808000">-----~--></font><br>
<font color="#000080"><b>While not</b></font> RS.EOF</font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font face="Arial" size="1"><font SIZE="1" color="#808000"><!--~-----
</font>Build Your Checkboxs<font color="#808000"> </font><font SIZE="1" color="#808000">-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font>You Can Also Build Text Fields by Modifying the Second Line to Read<font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font>Response.Write rs.fields(intCol).Value & ": <input type=Text size=15 name=" & rs.fields(intCol).Value & "><br>" & vbcrlf<font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font>If you want the checkboxs or text fields to display column names instead of data values replace any .Value with .Name<font SIZE="1" color="#808000">
-----~--></font><font color="#808000"> <br>
</font><font SIZE="1" color="#808000"><!--~----- </font>Example<font color="#808000">
</font><font SIZE="1" color="#808000">-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#0000FF">Response</font>.Write rs.fields(intCol).Name & ": <input type=checkbox name=" & rs.fields(intCol).Name & " value=" & rs.fields(intCol).Name & "><br>" & vbcrlf<font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- Also, if your SQL select
statement only had one line (same name/value for checkbox/textfields) the
following lines of code would read... -----~--></font><font color="#808000"> <br>
</font><font SIZE="1" color="#808000"><!--~----- </font><font color="#000080"><b>For</b></font> i = 0 to RS.fields.count
<font color="#000080"> -</font> <font color="#008080"> 1</font><font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font>Response.<b>Write</b>
RS.<b>fields<font color="#000080">(</font></b>i<font color="#000080"><b>)</b></font>.<b>Value</b>
<font color="#000080"><b> &</b></font> <font color="#008080"> ":<input type=checkbox name='"</font>
<font color="#000080"><b> &</b></font> RS.<b>fields</b><font color="#000080"><b>(</b></font>i<font color="#000080"><b>)</b></font>.<b>Value</b>
<font color="#000080"><b> &</b></font> <font color="#008080"> "' value="</font><font color="#000000">
</font><font color="#000080"><b> &</b></font><font color="#000000"> RS.<b>fields</b></font><font color="#000080"><b>(</b></font><font color="#000000">i</font><font color="#000080"><b>)</b></font><font color="#000000">.<b>Value</b>
</font><font color="#000080"><b> &</b></font><font color="#000000"> </font><font color="#008080"> "><br>"</font>
<font color="#000080"><b> &</b></font> vbcrlf<font SIZE="1" color="#808000">
-----~--></font><font color="#808000"><br>
</font><font SIZE="1" color="#808000"><!--~----- </font>RS.<b>MoveNext</b><font SIZE="1" color="#808000">
-----~--></font><br>
<font color="#000080"><b>For</b></font> i <font color="#000080"><b> =</b></font>
<font color="#008080"> 0</font> to RS.fields.count <font color="#000080"><b> and</b></font> RS1.fields.count
<font color="#000080"><b> -</b></font> <font color="#008080"> 1</font><br>
<font color="#0000FF">Response</font>.<b>Write</b> RS.<b>fields<font color="#000080">(</font></b>i<font color="#000080"><b>)</b></font>.<b>Value</b>
<font color="#000080"><b> &</b></font> <font color="#008080"> ":<input type=checkbox name='"</font>
<font color="#000080"><b> &</b></font> RS.<b>fields</b><font color="#000080"><b>(</b></font>i<font color="#000080"><b>)</b></font>.<b>Value</b>
<font color="#000080"><b> &</b></font> <font color="#008080"> "' value="</font><font color="#000000">
</font><font color="#000080"><b> &</b></font><font color="#000000"> RS1.<b>fields</b></font><font color="#000080"><b>(</b></font><font color="#000000">i</font><font color="#000080"><b>)</b></font><font color="#000000">.<b>Value</b>
</font><font color="#000080"><b> &</b></font><font color="#000000"> </font><font color="#008080"> "><br>"</font>
<font color="#000080"><b> &</b></font> vbcrlf<br>
RS.<b>MoveNext</b><br>
RS1.<b>MoveNext</b><br>
<font color="#000080"><b>Next<br>
Wend</b></font><br>
<br>
RS.<b>Close</b></font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font face="Arial" size="1">RS1.<b>Close</b></font><font face="Arial" size="1"><br>
MyConn.<b>Close</b><br>
<font color="#000080"><b>Set</b></font> RS = <font color="#000080"><b> Nothing</b></font></font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font face="Arial" size="1"><font color="#000080"><b>Set</b></font> RS1 =
<font color="#000080"><b> Nothing</b></font><br>
<font color="#000080"><b>Set</b></font> MyConn = <font color="#000080"><b> Nothing</b></font><br>
<br>
<font color="#FF0000">%></font><br>
<br>
<font color="#000080"><br><br>
<input type="submit" name="Submit" value="Submit"><br>
</form><br>
</body><br>
</html></font></font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font color="#000000">Name
this page dynamic1.asp</font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font color="#000000">Now
we create the page that will insert our data into your database</font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font size="1" face="Arial"><font color="#000080"><html><br>
<body></font><br>
<br>
<font color="#FF0000"><%</font><br>
<br>
<font color="#000080"><b>Dim</b></font> MyConn, MySQL, rs, i<br>
<br>
<font color="#000080"><b>Set</b></font> MyConn=<font color="#0000FF">Server</font>.<b>CreateObject</b>(<font color="#008080">"ADODB.Connection"</font>)<br>
<font color="#000080"><b>Set</b></font> rs = <font color="#0000FF">Server</font>.<b>CreateObject</b>(<font color="#008080">"ADODB.Recordset"</font>)<br>
MyConn = <font color="#008080"> "Provider=SQLOLEDB; Data Source=</font></font><font face="Arial" size="2" color="#800080">Your
SQL Server</font><font size="1" face="Arial" color="#008080">; Initial Catalog=</font><font face="Arial" size="2" color="#800080">Your
</font><font face="Arial" size="2" color="#800080">Database</font><font size="1" face="Arial" color="#008080">; User ID=</font><font face="Arial" size="2" color="#800080">Your
SQL Login</font><font size="1" face="Arial" color="#008080">; Password=</font><font face="Arial" size="2" color="#800080">Your
</font><font face="Arial" size="2" color="#800080">Password</font><font size="1" face="Arial"><font color="#008080">"</font><br>
MYSQL = "<font color="#008080">SELECT</font> </font><font face="Arial" size="2" color="#800080">Column
Name you want data entered into</font><font size="1" face="Arial"> <font color="#008080">FROM</font>
</font><font face="Arial" size="2" color="#800080">Your Table Name</font><font size="1" face="Arial">"<br>
rs.<b>Open</b> MySQL, MyConn, 1, 3<br>
<br>
<font color="#FF0000">%></font><br>
<br>
<font color="#FF0000"><%</font><br>
<br>
<font color="#000080"><b>for</b></font> i<font color="#000080"><b> = </b></font><font color="#008080"> 1</font> to
<font color="#0000FF">Request</font>.<b>form.count</b> <font color="#000080"><b>-</b></font><font color="#008080">1</font><br>
rs.<b>addnew</b><br>
rs.<b>Fields</b>("</font><font face="Arial" size="2" color="#800080">Column
Name you want data entered into</font><font size="1" face="Arial">") = <font color="#0000FF">Request</font>.<b>form</b>(i)<br>
<font color="#000080"><b> Next</b></font><br>
rs.<b>update</b><br>
<font color="#0000FF">Response</font>.<b>Write</b> <font color="#008080">"Thank
you"</font><br>
<br>
<font color="#FF0000">%></font><br>
<br>
<font color="#000080"></body><br>
</html></font></font></p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0">Name this page
dynamic2.asp</p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"> </p>
<p style="word-spacing: 0; margin-top: 0; margin-bottom: 0"><font size="4">Vote
for me if you find this useful, let me know if you need any help with the
code!!!</font></p>
</body>
</html>