Click to See Complete Forum and Search --> : adding records in database using vbscript in asp page


FundooGuy
November 25th, 2004, 04:58 AM
Hi

Ive some fields on asp page and i want to add records in my database by updating my form fields.
how can i do that

if u can provide some example that'll be great.

thnx

quinman
November 30th, 2004, 11:04 AM
Try this. All of this code is in one page. The form posts the fields to the same page, updates the database and then allows you to add another record.

When you first come into the page there will be no value in the form field "CMD" so the processing is skipped

<%
'first check to see if there is a new record to add


If request.form("cmd")="Add" then'create database connection

set rs=Server.CreateObject("ADODB.Recordset")

'open the database recordset
rs.open "[your table]", "[your database connection string]", 3, 2, 2

'create new record
rs.addnew

'set the values of the database fields from the submitted form
rs("[yourField_1]")=request.form("[yourFormField_1]")
rs("[yourField_n]")=request.form("[yourFormField_n]")

'etc...

'commit the record to the database
rs.Update

'close the recordset
rs.close

'destroy the database recordset object to conserve server resources
set rs=Nothing

End if


%>
<HTML>
<HEAD>
<!-- your head code goes here -->
</head>

<BODY>
<FORM method=post action="[thisPageName].asp">
<INPUT TYPE="HIDDEN" NAME="CMD" VALUE="ADD">
<INPUT type = text name="[yourFormField_1]">
<!--etc-->

</FORM>
</BODY>
</HTML>

If you omit the line
rs.Addnew
the current record (by default the first record in the recordset) will be updated.
__________________
Remote Control Cars at RC-Playtime.co.uk (http://www.rc-playtime.co.uk/)