Delete Method Example

This example uses the Delete method to remove a specified record from a Recordset. The DeleteRecord procedure is required for this procedure to run

Sub DeleteX()

	Dim rstEmployees As ADODB.Recordset
	Dim strCnn As String
	Dim strID As String

	' Open employee table.
	strCnn = "driver={SQL Server};server=srv;" & _
		"uid=sa;pwd=;database=pubs"
	Set rstEmployees = New ADODB.Recordset
	rstEmployees.CursorType = adOpenDynamic
	rstEmployees.LockType = adLockPessimistic
	rstEmployees.Open "employee", strCnn

	' Add temporary record to be deleted.
	With rstEmployees
		.AddNew
		!fName = "Janelle"
		!lName = "Tebbs"
		!emp_id = "J-T12345F"
		.Update
		strID = !emp_id
	End With

	' Delete the employee record with the specified ID
	' number.
	DeleteRecord rstEmployees, strID

	rstEmployees.Close

End Sub

Sub DeleteRecord(rstTemp As ADODB.Recordset, _
	strSeek As String)

	With rstTemp
		.Filter "emp_id = '" & strSeek & "'"
		If .RecordCount = 0 Then
			MsgBox "No employee #" & strSeek & " in file!"
		Else
			.Delete
			MsgBox "Record for employee #" & strSeek & _
				" deleted!"
		End If
	End With

End Sub

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.