--- title: "AddNew Method Example (VBScript) | Microsoft Docs" ms.prod: sql ms.prod_service: connectivity ms.technology: connectivity ms.custom: "" ms.date: "01/19/2017" ms.reviewer: "" ms.topic: conceptual dev_langs: - "VB" helpviewer_keywords: - "AddNew method [ADO], VBScript" ms.assetid: dcdcaf0a-b9b0-4d81-8728-43c38c4c853b author: MightyPen ms.author: genemi --- # AddNew Method Example (VBScript) This example uses the [AddNew](../../../ado/reference/ado-api/addnew-method-ado.md) 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 record in the HTML form. Click **Add New**. See the [Delete Method Example](../../../ado/reference/ado-api/delete-method-example-vbscript.md) to remove unwanted records. ``` <%@Language = VBScript %> <%' use this meta tag instead of adovbs.inc%> ADO AddNew Method (VBScript)

ADO AddNew Method (VBScript)

<% ' to integrate/test this code replace the ' Data Source value in the Connection string%> <% ' connection and recordset variables Dim Cnxn, strCnxn Dim rsCustomers, strSQLCustomers Dim fld, Err ' open connection Set Cnxn = Server.CreateObject("ADODB.Connection") strCnxn = "Provider='sqloledb';Data Source=" & _ Request.ServerVariables("SERVER_NAME") & ";" & _ "Integrated Security='SSPI';Initial Catalog='Northwind';" Cnxn.Open strCnxn ' create and open Recordset using object refs Set rsCustomers = Server.CreateObject("ADODB.Recordset") strSQLCustomers = "Customers" rsCustomers.ActiveConnection = Cnxn rsCustomers.CursorLocation = adUseClient rsCustomers.CursorType = adOpenKeyset rsCustomers.LockType = adLockOptimistic rsCustomers.Source = strSQLCustomers rsCustomers.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 rsCustomers.AddNew rsCustomers("CustomerID") = Request.Form("CompanyID") rsCustomers("CompanyName") = Request.Form("CompanyName") rsCustomers("ContactName") = Request.Form("FirstName") & _ " " & Request.Form("LastName") rsCustomers("Phone") = Request.Form("PhoneNumber") rsCustomers("City") = Request.Form("City") rsCustomers("Region") = Request.Form("State") rsCustomers.Update ' check for errors If Cnxn.Errors.Count > 0 Then For Each Err In Cnxn.Errors Response.Write("Error " & Err.SQLState & ": " & _ Err.Description & " | " & Err.NativeError) Next Cnxn.Errors.Clear rsCustomers.CancelUpdate End If 'On Error GoTo 0 rsCustomers.MoveFirst End If End If %> <% ' show the data Do Until rsCustomers.EOF Response.Write("") Response.Write("") Response.Write("") Response.Write("") Response.Write("") Response.Write("") Response.Write("") Response.Write("") rsCustomers.MoveNext Loop %>
Customer ID Company Name Contact Name Phone Number City State/Province
" & rsCustomers("CustomerID") & "" & rsCustomers("CompanyName")& "" & rsCustomers("ContactName") & "" & rsCustomers("Phone") & "" & rsCustomers("City") & "" & rsCustomers("Region") & "

Company ID:
Company Name:
Contact First Name:
Contact Last Name:
Contact Phone:
City:
State / Province:
<% ' Show connection. Response.Write("Following is the connection string:

") Response.Write(Cnxn) ' Clean up. If rsCustomers.State = adStateOpen then rsCustomers.Close End If If Cnxn.State = adStateOpen then Cnxn.Close End If Set rsCustomers=Nothing Set Cnxn=Nothing Set fld=Nothing %> ``` ## See Also [AddNew Method (ADO)](../../../ado/reference/ado-api/addnew-method-ado.md) [Recordset Object (ADO)](../../../ado/reference/ado-api/recordset-object-ado.md)