Presentation is loading. Please wait.

Presentation is loading. Please wait.

Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,

Similar presentations


Presentation on theme: "Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,"— Presentation transcript:

1 Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name, Connection Object, CursorType, LockType, adCmdStoredProc Source = string rsObject.Open SQLstring, Connection Object, CursorType, LockType, adCmdText Opening a RecordSet

2 Requery Method Updates the data in a Recordset object by re-executing the query on which the object is based. Syntax recordset.Requery Options Use the Requery method to refresh the entire contents of a Recordset object from the data source by reissuing the original command and retrieving the data a second time. Calling this method is equivalent to calling the Close and Open methods in succession. If you are editing the current record or adding a new record, an error occurs. *Note - Use after inserting, deleting, and updating Update Method Saves any changes you make to the current row of a Recordset object RecordSet

3 The Command Object Use a Command object to execute queries. Queries that return records Queries that return no records (Insert, delete etc) Important Propeties and Methods CommandText -Indicates the text of a command to be issued against a provider. CommandType -Indicates the type of a Command object. adCmdUnspecified adCmdText –use for SQL adCmdTable adCmdStoredProc CommandTimeout - Indicates how long to wait while executing a command before terminating the attempt and generating an error. ActiveConnection -Indicates to which Connection object the specified Command currently belongs Execute

4 The Command Object Execute -Executes the query, SQL statement, or stored procedure specified in the CommandText property. For a Recordset-returning Command: Set recordset = command.Execute( RecordsAffected, Parameters, Options ) For a non–recordset-returning Command: command.Execute RecordsAffected, Parameters, Options Usually command.Execute

5 The Command Object Dim Cnxn As ADODB.Connection Dim cmdChange As ADODB.Command Dim strSQLChange As String Dim strCnxn As String strSQLChange = "UPDATE Titles SET Type = 'self_help' WHERE Type = 'psychology'“ strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Pubs;User Id=sa;Password=; " Set Cnxn = New ADODB.Connection Cnxn.Open strCnxn Set cmdChange = New ADODB.Command Set cmdChange.ActiveConnection = Cnxn cmdChange.CommandText = strSQLChange cmd.Execute

6 Common Database Operations Navigation Use MoveFirst, MoveLast, MoveNext, and MovePrevious methods of recordset. recordset.MoveNext Adding a Record Dim Cnxn As ADODB.Connection Dim rstEmployees As ADODB.Recordset Dim strCnxn As String Dim strSQL As String Dim strID As String Dim strFirstName As String Dim strLastName As String Dim blnRecordAdded As Boolean ' Open a connection Set Cnxn = New ADODB.Connection strCnxn = "Provider=sqloledb;Data Source=MyServer;Initial Catalog=Northwind;User Id=sa;Password=;“ Cnxn.Open strCnxn

7 ' Open Employees Table with a cursor that allows updates Set rstEmployees = New ADODB.Recordset strSQL = "Employees" rstEmployees.Open strSQL, strCnxn, adOpenKeyset, adLockOptimistic, adCmdTable ' Get data from the user strFirstName = Trim(InputBox("Enter first name:")) strLastName = Trim(InputBox("Enter last name:")) If strFirstName <> "" And strLastName <> "" Then rstEmployees.AddNew rstEmployees!FirstName = strFirstName rstEmployees!LastName = strLastName rstEmployees.Update

8 ' Show the newly added data MsgBox "New record: " & rstEmployees!EmployeeID & " " & _ rstEmployees!FirstName & " " & rstEmployees!LastName Else MsgBox "Please enter a first name and last name." End If ' clean up rstEmployees.Close Cnxn.Close Set rstEmployees = Nothing Set Cnxn = Nothing

9 Common Database Operations Adding a Record - alternate way Dim cmdEmp As ADODB.Command Set cmdEmp = New ADODB.Command cmdEmp.ActiveConnection = Cnxn cmdEmp.CommandType = adCmdText If strFirstName <> "" And strLastName <> "" Then cmdEmp.CommandText = "Insert into Employees(FirstName, LastName) values(‘" & strFirstName & “’, ‘" & strLastName & ‘")" cmdEmp.Execute rstEmployees.Requery

10 Deleting a Record Deleting a record Dim cmdEmp As ADODB.Command Set cmdEmp = New ADODB.Command cmdEmp.ActiveConnection = Cnxn cmdEmp.CommandType = adCmdText cmdEmp.CommandText = "DELETE FROM Employees WHERE EmployeeID = '" & strID & "'" cmdEmp.Execute rstEmployees.Requery

11 Menu Choices File – Exit Select – By LastName, By FirstName, By All Patients Sort – By Patient Number, By Patient Name

12


Download ppt "Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,"

Similar presentations


Ads by Google