ADO Samples

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). 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 AddNewVBS.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.

<!-- BeginAddNewVBS -->
<%@Language = VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML>
<HEAD>
   <TITLE>ADO AddNew Method (VBScript)</TITLE>
   <STYLE>
   <!--
   body {
      font-family: 'Verdana','Arial','Helvetica',sans-serif;
      BACKGROUND-COLOR:white;
      COLOR:black;
       }
   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> 

<H1>ADO AddNew Method (VBScript)</H1>

<!-- ADO Connection Object used to create recordset-->
<% 
   strCnn = "Provider=sqloledb;Data Source=" & _
      Request.ServerVariables("SERVER_NAME") & _
      ";Initial Catalog=Northwind;User Id=sa;Password=;"

   'Create and Open Connection Object
   Set OBJdbConn = Server.CreateObject("ADODB.Connection")
   OBJdbConn.Open strCnn
      
   'Create and Open Recordset Object
   Set RsCustomerList = Server.CreateObject("ADODB.Recordset")
   RsCustomerList.ActiveConnection = OBJdbConn
   RsCustomerList.CursorLocation = adUseClient
   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("CustomerID") = Request.Form("CompanyID")
         RsCustomerList("CompanyName") = Request.Form("CompanyName")
         RsCustomerList("ContactName") = Request.Form("FirstName") & _
            " " & Request.Form("LastName")
         RsCustomerList("Phone") = Request.Form("PhoneNumber")
         RsCustomerList("City") = Request.Form("City")
         RsCustomerList("Region") = Request.Form("State")
         On Error Resume Next
         RsCustomerList.Update
         If OBJdbConn.Errors.Count > 0 Then
            For Each objError in OBJdbConn.Errors
               Response.Write("Error " & objError.SQLState & ": " & _
                  objError.Description & " | " & objError.NativeError)
            Next
            OBJdbConn.Errors.Clear
            RsCustomerList.CancelUpdate
         End If
         On Error GoTo 0
         RsCustomerList.MoveFirst
      End If
   End If
%>

<TABLE COLSPAN="8" CELLPADDING=5 BORDER=1 ALIGN="center"><!-- BEGIN column header row for Customer Table-->
   <TR>
      <TH>Customer ID</TH>
      <TH>Company Name</TH>
      <TH>Contact Name</TH>
      <TH>Phone Number</TH>
      <TH>City</TH>
      <TH>State/Province</TH>
      </TR>
      
      <!--
         Display ADO Data from Customer Table 
         one row on each pass through recordset
      -->
   <%
      Do While Not RsCustomerList.EOF
         Response.Write("<TR>")
         Response.Write("<TD>" & RSCustomerList("CustomerID") & "</TD>")
         Response.Write("<TD>" & RSCustomerList("CompanyName")& "</TD>")
         Response.Write("<TD>" & RScustomerList("ContactName") & "</TD>")
         Response.Write("<TD>" & RScustomerList("Phone") & "</TD>")
         Response.Write("<TD>" & RScustomerList("City") & "</TD>")
         Response.Write("<TD>" & RScustomerList("Region") & "</TD>")
         Response.Write("</TR>")
         RScustomerList.MoveNext 
      Loop 
   %>
</TABLE> 

<HR>

<!--
   Form to enter new record posts variables
   back to this page
-->
<FORM Method=post Action="AddNewVbs.asp" Name=Form>
   <TABLE>
      <TR>
         <TD>Company ID:</TD>
         <TD><INPUT Size="5" Name="CompanyID" maxLength=5  ></TD>
      </TR>
      <TR>
         <TD>Company Name:</TD>
         <TD><INPUT Size="50" Name="CompanyName" ></TD>
      </TR>
      <TR>
         <TD>Contact First Name:</TD>
         <TD><INPUT Size="50" Name="FirstName" ></TD>
      </TR>
      <TR>
         <TD>Contact Last Name:</TD>
         <TD><INPUT Size="50" Name="LastName" ></TD>
      </TR>
      <TR>
         <TD>Contact Phone:</TD>
         <TD><INPUT Size="50" Name="PhoneNumber" ></TD>
      </TR>
      <TR>
         <TD>City:</TD>
         <TD><INPUT Size="50" Name="City" ></TD>
      </TR>
      <TR>
         <TD>State / Province:</TD>
         <TD><INPUT Size="5" Name="State" ></TD>
      </TR>
      <TR>
         <TD Align="right"><INPUT Type="submit" Value="Add New"></TD>
         <TD Align="left"><INPUT Type="reset" Value="Reset Form"></TD>
      </TR>
   </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>
<!-- EndAddNewVBS -->

See Also

AddNew Method | Recordset Object