AddNew Method Example (VBScript)
This example uses the AddNew method to create a new record with the specified name.
Use the following example in an Active Server Page (ASP). To view this fully functional example, you must have the data source AdvWorks.mdb (installed with the SDK) located at C:\mssdk\samples\dataaccess\rds. This is a Microsoft Access database file.
Use Find to locate the file Adovbs.inc and place it in the directory you plan to use. Cut and paste the following code to Notepad or another text editor, and save it as AddNew.asp. You can view the result in any client browser.
To exercise the example, add a new fictional record in the HTML form. Click Add New. See the Delete Method Example to remove unwanted records.
<%@Language = VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML>
<HEAD>
<TITLE>ADO Open Method</TITLE>
<STYLE>
<!--
TH {
background-color: #008080;
font-family: 'Arial Narrow','Arial',sans-serif;
font-size: xx-small;
color: white;
}
TD {
text-align: center;
background-color: #f7efde;
font-family: 'Arial Narrow','Arial',sans-serif;
font-size: xx-small;
}
-->
</STYLE>
</HEAD>
<BODY>
<FONT FACE="MS SANS SERIF" SIZE=2>
<Center><H3>ADO AddNew Method</H3>
<!-- ADO Connection Object used to create recordset-->
<%
src = "C:\mssdk\samples\dataaccess\rds\advworks.mdb"
sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & src
'Create and Open Connection Object
Set OBJdbConn = Server.CreateObject("ADODB.Connection")
OBJdbConn.Open sConnStr
'Create and Open Recordset Object
Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
RsCustomerList.ActiveConnection = OBJdbConn
RsCustomerList.CursorType = adOpenKeyset
RsCustomerList.LockType = adLockOptimistic
RsCustomerList.Source = "Customers"
RsCustomerList.Open
%>
<!—
If this is first time page is open, Form collection will be empty when data is entered.
run AddNew method
-->
<% If Not IsEmpty(Request.Form) Then
If Not Request.Form("CompanyName") = "" Then
RsCustomerList.AddNew
RsCustomerList("CompanyName") = Request.Form("CompanyName")
RsCustomerList("ContactLastName") = Request.Form("LastName")
RsCustomerList("ContactFirstName") = Request.Form("FirstName")
RsCustomerList("PhoneNumber") = Request.Form("PhoneNumber")
RsCustomerList("City") = Request.Form("City")
RsCustomerList("StateOrProvince") = Request.Form("State")
RsCustomerList.Update
RsCustomerList.MoveFirst
End If
End If
%>
<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<!-- BEGIN column header row for Customer Table-->
<TR>
<TD>Company Name</TD>
<TD>Contact Name</TD>
<TD>Phone Number</TD>
<TD>City</TD>
<TD>State/Province</TD>
</TR>
<!--Display ADO Data from Customer Table
one row on each pass through recordset-->
<% Do While Not RsCustomerList.EOF %>
<TR>
<TD> <%= RSCustomerList("CompanyName")%> </TD>
<TD> <%= RScustomerList("ContactLastName") & ", " %>
<%= RScustomerList("ContactFirstName") %> </TD>
<TD> <%= RScustomerList("PhoneNumber")%> </TD>
<TD> <%= RScustomerList("City")%> </TD>
<TD> <%= RScustomerList("StateOrProvince")%> </TD>
</TR>
<!-- Next Row = Record Loop and add to row html table-->
<%
RScustomerList.MoveNext
Loop
%>
</TABLE>
<HR>
<!-- Form to enter new record posts variables back to this page -->
<Form Method=Post Action="AddNew.asp" Name=Form>
<TABLE>
<TR>
<TD>Company Name:</TD>
<TD><Input Type="Text" Size="50" Name="CompanyName" Value = ""></TD>
<TR>
<TD>Contact First Name:</TD>
<TD><Input Type="Text" Size="50" Name="FirstName" Value = ""></TD>
<TR>
<TD>Contact Last Name:</TD>
<TD><Input Type="Text" Size="50" Name="LastName" Value = ""></TD>
<TR>
<TD>Contact Phone:</TD>
<TD><Input Type="Text" Size="50" Name="PhoneNumber" Value = ""></TD>
<TR>
<TD>City:</TD>
<TD><Input Type="Text" Size="50" Name="City" Value = ""></TD>
<TR>
<TD>State / Province:</TD>
<TD><Input Type="Text" Size="5" Name="State" Value = ""></TD>
<TR>
<TD><Input Type="Submit" Value="Add New">
<Input Type="Reset" Value="Reset Form">
</TABLE>
</Form>
<%'Show location of data source
Response.Write(OBJdbConn)
%>
<Script Language = "VBScript">
Sub Form_OnSubmit
MsgBox "Sending New Record to Server",,"ADO-ASP _Example"
End Sub
</Script>
</BODY>
</HTML>