Delete Method Example (VBScript)

Microsoft ActiveX Data Objects (ADO)

Delete Method Example (VBScript)

This example uses the Delete method to remove a specified record from a Recordset.

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 into Notepad or another text editor, and save it as AddNew.asp. You can view the result in any client browser.

To exercise the example, try using the AddNew example first to add some records. Then you can try to delete them. View the result in any client browser.

<%@ Language=VBScript %>
<!-- #Include file="ADOVBS.INC" -->
<HTML>
<HEAD>
<TITLE>ADO Delete Method</TITLE>
</HEAD>
<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>

<BODY> 
<H3>ADO Delete 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
%>
<!-- Move to designated record and delete it -->
<% 
If Not IsEmpty(Request.Form("WhichRecord")) Then
    'Get value to move from Form Post method
    Moves = Request.Form("WhichRecord")

    RsCustomerList.Move CInt(Moves)
    If Not RsCustomerList.EOF or RsCustomerList.BOF Then
        RsCustomerList.Delete 1
        RsCustomerList.MoveFirst
    Else
        Response.Write "Not a Valid Record Number"
        RsCustomerList.MoveFirst
    End If
End If

%>
<!-- BEGIN column header row for Customer Table-->

<TABLE COLSPAN=8 CELLPADDING=5 BORDER=0>
<TR>
    <TH>Rec. #</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 Loop through Recordset adding
One Row to HTML Table each pass-->
<% 
iCount = 0
Do While Not RsCustomerList.EOF %>
<TR>
  <TD> <%= CStr(iCount) %>
  <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 html table-->
<% 
  iCount = iCount + 1
  RScustomerList.MoveNext 
Loop 
%>
</TABLE>
<!-- Do Client side Input Data Validation Move to named record and Delete it -->

<Center>
<H4>Clicking Button Will Remove Designated Record</H4>
<H5>There are <%=RsCustomerList.RecordCount%> Records in this Set</H5>
<Form Method=Post Action="Delete.asp" Name=Form>
<Input Type=Text Name="WhichRecord" Size=3>
</Form>
<Input Type=Button Name=cmdDelete Value="Delete Record">

</BODY>

<Script Language = "VBScript">
Sub cmdDelete_OnClick
    If IsNumeric(Document.Form.WhichRecord.Value) Then
        Document.Form.WhichRecord.Value = CInt(Document.Form.WhichRecord.Value)
        Dim Response
        Response = MsgBox("Are You Sure About Deleting This Record?", vbYesNo,  "ADO-ASP Example")

        If Response = vbYes Then
            Document.Form.Submit
        End If
    Else
        MsgBox "You Must Enter a Valid Record Number",,"ADO-ASP Example"
    End If
End Sub
</Script>
</HTML>