140 likes | 336 Views
BookMark. Recordset’s current record may change. Use bookmark to mark a specific record so that you can return to it later. Store the current record’s bookmark in a variant variable. Dim varBookMark as Variant The current location may be changed by methods such as MoveNext, etc.
E N D
BookMark • Recordset’s current record may change. • Use bookmark to mark a specific record so that you can return to it later. • Store the current record’s bookmark in a variant variable. • Dim varBookMark as Variant • The current location may be changed by methods such as MoveNext, etc. • To return to the bookmark you have stored: • adoRecordsetName.BookMark = varBookMark
AddNew • Using bound controls with ADO Data Control or Data Environment Command object: adoRecordsetName.AddNew • Using unbound controls: • A form with unbound textboxes is initialized for input. • After entering data, a Save/Add button is clicked and the event handler uses a pair of AddNew and Update methods to insert record.
Unbound Control Input Example With DataEnvironment1.rsCommand1 .AddNew .Fields("CID") = txtCID.Text !Cname = txtCNAME.Text !City = txtCITY.Text !Rating = txtRATING.Text .Update End With
Find and BookMark • Syntax: adoRecordsetName.Find criteria • Only accept simple comparison criteria. • Example: • Adodc1.recordset.Find “CID = ‘” & txtCID & “’” • Finding employees hired between 2/15/2000 and 5/15/2000: • Adodc1.recordset.Find “Hdate > #2/15/2000#” Then use IF statement in program to filter out those hired after 5/15/2000 If no match is found, the EOF of the recordset will be set to True.
Find Code Example Private Sub txtCID_Validate(Cancel As Boolean) DataEnvironment1.rsCommand1.MoveFirst DataEnvironment1.rsCommand1.Find "CID = '" & txtCID.Text & "'" If Not DataEnvironment1.rsCommand1.EOF Then txtCNAME.Text = DataEnvironment1.rsCommand1.Fields("Cname") txtCITY.Text = DataEnvironment1.rsCommand1.Fields("City") txtRATING.Text = DataEnvironment1.rsCommand1.Fields("Rating") MsgBox "Record already exist, Add operation cancelled" DataEnvironment1.rsCommand1.Cancel Command2.Enabled = False End If Demo
Hierarchical Recordsets • Data Shaping: A new ADO feature that retains a 1:M (Parent-Child) relationship by creating a hierarchical recordset. • Shape command syntax: • Shape {Select …..} As ParentName Append ( {Select ….} As ChildName Relate ParentFieldName TO ChildFieldName) As ChildName
Shape Command Example Shape {Select * From Customers} As Customers Append ({Select * From Orders} As Orders Relate CustomerID TO CustomerID) As Orders Note:As ParentName, As ChildName are optional. The Shape clause constructs a parent recordset. The Append clause creates child recordset using the fields specified in the Relate clause to organize the relationship.
DataGrid and Hierarchical FlexGrid (HFG) • Display recordset records in rows and columns. • DataGrid allows update, HFG is read only. • HFG supports hierarchical recordset. • Use MSDataShape provider to bind data to HFG. • Property Page: Columna & Rows • Property Window: DataSource • Demo
Referencing the Hierarchical Recordset • For applications that display 1:M relatonship in sync. • The parent recordset can be created by Data Environment, ADO Data Control, or recordset’s Open method. • The childname in the Shape command is treated as a field in the parent recordset. To access the child recordset for a particular parent, do: • Set childRecordsetName = parentRecordsetName.Fields(“childname”).Value • Demo
Shape Command Example shape {select CustomerID, CompanyName, City from Customers} As Customers Append ({Select OrderID, CustomerID, EmployeeID, OrderDate From Orders} As Orders Relate CustomerID to CustomerID) As Orders
1:M Code Example Dim rsParent As ADODB.Recordset Dim rsChild As ADODB.Recordset Private Sub Form_Load() Set rsParent = DataEnvironment1.rsCommand1 rsParent.Open Do Until rsParent.EOF List1.AddItem rsParent.Fields("CustomerID") rsParent.MoveNext Loop End Sub Private Sub List1_Click() rsParent.MoveFirst rsParent.Find "CustomerID = '" & List1.List(List1.ListIndex) & "'" Set rsChild = rsParent.Fields("Orders").Value Set MSHFlexGrid1.DataSource = rsChild End Sub
Using MSDataShape without ODBC DSN • Click Provider and choose MSDataShape • Click ALL • Edit Data Provider: For Access 2000, enter: • Microsoft.Jet.OleDB.4.0 • Edit Data Source: Enter the path to your Access database
Creating 1:M Relationship with Data Environment • Right-click on the command object and select Add Child Command • In the child command, select Relationship tab and check Relate to a Parent Command Object
Creating Grouping and Aggregates in a 1:M Relationship • Add a Child Command. • Set Relation. • Set Grouping • Add Aggregates