Important alert: (current site time 7/15/2013 9:59:33 PM EDT)
 

article

Building an Input Form Dynamically From a Database.

Email
Submitted on: 3/22/2001 1:46:23 AM
By: Joshua i. Lopez 
Level: Beginner
User Rating: By 35 Users
Compatibility: ASP (Active Server Pages)
Views: 77518
 
     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.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

This is the first of 2 Pages you will need...

<html>
<body>
<Form Name="Form" Type="Submit" Method="Post" Action="dynamic2.asp">


<%
<!--~----- Dim Your Variables -----~-->
Dim MyConn, SQL, RS, i

<!--~----- Create the Connection and Open the Connection to the Database -----~-->
<!--~----- This Will Connect to a MS-SQL Database -----~-->
<!--~----- To Connect to an Access Database Replace the Bottom 3 Lines of Code with either... -----~-->
<!--~----- MyConn.Open "Driver={Microsoft Access Driver (*.mdb)}; -----~--> 
<!--~----- DBQ=C:\Databases\demo.mdb" -----~-->
<!--~----- or -----~-->
<!--~----- MyConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data -----~--> 
<!--~----- Source=C:\Databases\demo.mdb" -----~-->
<!--~----- or -----~-->
<!--~----- MyConn.Open DSN_Name -----~-->
Set MyConn=Server.CreateObject("ADODB.Connection")
Myconn.ConnectionString = "Provider=SQLOLEDB; Data Source=
Your SQL Server; Initial Catalog=Your Database; User ID=Your Login; Password=Your Password"
Myconn.open

<!--~----- Wriye your SQL statement -----~-->
<!--~----- In this case we have 2 SQL statements because I have a different value for my form fields other than its name -----~-->
<!--~----- If your checkbox/text field will have the same name and value you can safely remove from "SQL1 = "Select" down -----~-->
SQL = "Select
Column Name(s) in your Database that holds values that will become the checkbox/textfield name From Your Table Name"
Set RS = MyConn.Execute(SQL)
SQL1 = "Select
Column Name in your Database that holds values that will become the checkbox/textfield value From Your Table Name"
Set RS1 = MyConn.Execute(SQL1)

<!--~----- Read All of the Records with EOF -----~-->
While not RS.EOF

<!--~----- Build Your Checkboxs -----~-->
<!--~----- You Can Also Build Text Fields by Modifying the Second Line to Read -----~-->
<!--~----- Response.Write rs.fields(intCol).Value & ": <input type=Text size=15 name=" & rs.fields(intCol).Value & "><br>" & vbcrlf -----~-->
<!--~----- If you want the checkboxs or text fields to display column names instead of data values replace any .Value with .Name -----~--> 
<!--~----- Example -----~-->
<!--~----- Response.Write rs.fields(intCol).Name & ": <input type=checkbox name=" & rs.fields(intCol).Name & " value=" & rs.fields(intCol).Name & "><br>" & vbcrlf -----~-->
<!--~----- Also, if your SQL select statement only had one line (same name/value for checkbox/textfields) the following lines of code would read... -----~--> 
<!--~----- For i = 0 to RS.fields.count - 1 -----~-->
<!--~----- Response.Write RS.fields(i).Value & ":<input type=checkbox name='" & RS.fields(i).Value & "' value=" & RS.fields(i).Value & "><br>" & vbcrlf -----~-->
<!--~----- RS.MoveNext -----~-->
For i = 0 to RS.fields.count and RS1.fields.count - 1
Response.Write RS.fields(i).Value & ":<input type=checkbox name='" & RS.fields(i).Value & "' value=" & RS1.fields(i).Value & "><br>" & vbcrlf
RS.MoveNext
RS1.MoveNext
Next
Wend


RS.Close

RS1.Close
MyConn.Close
Set RS = Nothing

Set RS1 = Nothing
Set MyConn = Nothing

%>

<br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

 

Name this page dynamic1.asp

 

 

Now we create the page that will insert our data into your database

 

<html>
<body>


<%

Dim MyConn, MySQL, rs, i

Set MyConn=Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
MyConn = "Provider=SQLOLEDB; Data Source=
Your SQL Server; Initial Catalog=Your Database; User ID=Your SQL Login; Password=Your Password"
MYSQL = "SELECT
Column Name you want data entered into FROM Your Table Name"
rs.Open MySQL, MyConn, 1, 3

%>

<%

for i = 1 to Request.form.count -1
rs.addnew
rs.Fields("
Column Name you want data entered into") = Request.form(i)
Next
rs.update
Response.Write "Thank you"

%>

</body>
</html>

 

Name this page dynamic2.asp

 

Vote for me if you find this useful, let me know if you need any help with the code!!!

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/22/2001 4:50:59 AMKayhan Tanriseven

very useful code you have!! thanks for submitting!
(If this comment was disrespectful, please report it.)

 
3/22/2001 6:32:30 AMRax

nice clean code at last :/
(If this comment was disrespectful, please report it.)

 
3/22/2001 8:06:04 PMMay

This is very useful. I'm an asp student. I couldn't get your demo.mdb. Also I did not understand all the code. How can I call this dynamic_form.asp, and how it works. Would you like tell me more about this function page.
(If this comment was disrespectful, please report it.)

 
3/23/2001 1:18:57 AMLorie Perrish

Excellent code, I have been looking for something that does this for a long time!!
(If this comment was disrespectful, please report it.)

 
3/23/2001 10:57:19 AMDoug Gaede

Do you have an example database for use with this code?
(If this comment was disrespectful, please report it.)

 
3/28/2001 12:51:04 PMwicked

what you mean ..
(If this comment was disrespectful, please report it.)

 
3/28/2001 12:51:28 PMWICKED

what you mean ..
wow can't believe this is not out there
already you can use this same coding for oracle ,sql or even in a email
I been offering fastemailer.asp for along time that does this.
wow just cant believe this wasn` t out there already.
anyway nice job for posting it for others.Guess I goes to show you can never realy be sure of whats posted.

(If this comment was disrespectful, please report it.)

 
4/3/2001 8:18:05 AMafrica

Hi,
Your code did wonders for me and I'd give it 5 out of 5 any day!
Please keep sending more of your new code and keep vbscript/asp alive!!!!
(If this comment was disrespectful, please report it.)

 
4/7/2001 8:13:11 PMJohn

Can you show me how to make your dynamic code work with 60 dropdown box... I am new to ASP.. Thanks
My e-mail: jhmai@eudoramail.com

(If this comment was disrespectful, please report it.)

 
8/15/2001 3:44:57 PMSVM

Hey, sorry to bother you, but is there a way to use this code to submit a form remotely?
(If this comment was disrespectful, please report it.)

 
10/11/2001 6:12:51 PMRLF

Where can you get the Demo.mdb. It was not in the download
(If this comment was disrespectful, please report it.)

 
10/31/2001 5:31:41 PMyogi

This code is very useful. But, can we add validations or business rules dynamically ?
(If this comment was disrespectful, please report it.)

 
11/13/2002 4:53:47 PM

The code is great, but how do you add multiple field values at once? Also, do you know of any other good tutorials for ADO 2.0?
(If this comment was disrespectful, please report it.)

 
5/22/2003 5:23:58 PM

I've got an HTML table that I'd like to make one column of editable, and update the database with info from all HTML columns, including the one that was an input text.
thanks
(If this comment was disrespectful, please report it.)

 
7/9/2003 10:43:07 PM

neat code man !!
but actually i m looking for a code that would insert rows in my db by picking data from Dynamically created page.!! Mail me back if u knw something like that . it would be a great help
(If this comment was disrespectful, please report it.)

 
1/13/2004 8:30:01 PM

am new to asp and been looking for some codes that may help me learn how ASP works... and this one surely do. tnx :)
(If this comment was disrespectful, please report it.)

 
2/25/2004 4:16:56 PM

Can someone please say where you get the database from. It was not in the download. Email: dj@engineroomdm.com

thanx
(If this comment was disrespectful, please report it.)

 
1/19/2007 12:58:24 AMPramod

I'm having a form developed in ASP and from that I have to prick 4 fields and have to display when the user submits the form. How should i do it,
The code is attached
(If this comment was disrespectful, please report it.)

 
9/27/2007 4:04:26 AM

it couldnot work can i send my code to you so you can see where the problame is
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.