[ Pobierz całość w formacie PDF ]
.Count = 0 ThenCall PopulateCollection()Properties 55VB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.PropertiesEnd IfCount = mcolAnyColl.CountEnd PropertyA major use of property procedures is to validate data.Without class modules andproperties, validation is typically performed at the form level; for example, theChange or LostFocus event handler of a TextBox control determines if the dataentered in the text box is acceptable.It often happens, though, that this value isreferenced on many different forms, so that each reference has to be accompa-nied by the data validation code.If a programmer gets the validation code wrong,or simply ignores or forgets the validation altogether, you risk accepting invaliddata into your database.If the validation rules change, you have to visit each formthat contains a reference to the data item and change the validation code.Now contrast this scenario with validation within a property procedure.The formcan attempt to assign a nonvalidated value to the property, and the propertyprocedure will validate the data and accept or reject it.The validation for this dataitem is thereby centralized.Any form can use the property, and you can be certainthat only validated data is accepted into the database.Furthermore, if a change tothe validation is required, you only have to change the validation logic in oneplace, thereby reducing the risk of error.The following snippet demonstrates using a Property procedure to implementbusiness rules to validate incoming data:Public Property Let ClaimDate(dVal as Date)'business rule: a claim cannot be more than 10 days oldIf dVal 0 TheniCharCode = Asc(sMyString)ElseMsgBox "Cannot process a zero-length string"End If" On platforms which don t support Unicode, the AscW function performsexactly the same as Asc." Surprisingly, although the VB Object Browser clearly shows that the data typeof the parameter passed to the Asc function is String, it can actually be anydata type.Evidently the Asc routine converts incoming values to strings beforeextracting their first character." Use Asc within your data validation routines to determine such conditions aswhether the first character is upper- or lowercase and whether it s alphabeticor numeric, as the following example demonstrates:Private Sub CommandButton1_Click()Dim sTest As StringDim iChar As IntegersTest = TextBox1.TextIf Len(sTest) > 0 TheniChar = Asc(sTest)If iChar >= 65 And iChar = 97 And iChar 0) Then _IsSoundSupported = TrueEnd Functionthen the procedurePrivate Sub Form_Load()Dim intCtr As IntegerHasSound = IsSoundSupported()If HasSound ThenCall PlaySound("c:\windows\media\tada.wav", 0, _SND_FILENAME Or SND_NODEFAULT)ElseFor intCtr = 0 To 3BeepNextEnd IfEnd SubBindingCollection Object (VB6)DescriptionThe BindingCollection object as the name suggests is a collection of Bindingobjects.The BindingCollection object plays a central role in the new data-bindingtechnology in VB6, allowing you to automatically map data fields to standard formcontrols and to specify formatting for those data.Your application can have anynumber of BindingCollection objects, each referring to a distinct data memberprovided by a data source such as an ADO recordset or a VB data source class.The Binding objects held within the class represent the individual mapping ofconsumer control property to data provider field, a relationship created using theBindingCollections Add method.The relationship between the various data binding elements is shown inFigure 7-1.For an overview of data-binding objects, including the library reference needed toaccess the object model, see the Data Binding Objects entry.CreateableYesBindingCollection PropertiesCountData Type: LongThe number of Binding objects in the collection.134 Chapter 7 The Language ReferenceVB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.BindingCollection Object (VB6)Data Consumer ControlPropertyDataSource BindingCollectionData Consumer ControlDataMemberBinding ObjectPropertyData Field Binding ObjectData Consumer ControlData Field Binding ObjectPropertyData FieldBinding ObjectData Consumer ControlData FieldPropertyData Consumer ControlDataMemberBindingCollection PropertyData FieldBinding ObjectData Consumer ControlData FieldPropertyBinding ObjectData FieldBinding ObjectData Field Data Consumer ControlPropertyBinding ObjectData Consumer ControlPropertyFigure 7-1: How data-binding elements relate to each otherDataMemberType: DataMemberAn optional string specifying the data member to use from the datasource.This is useful where the source provides more than one datamember.A DataSource application can provide many data members; youneed to create a BindingCollection object for each data member to whichyou wish to bind.DataSourceData Type: DataSourceA valid data source object such as an ADO recordset or a VB class objectset to vbDataSource.The DataSource is distinct from the DataMember;the DataSource is the application providing the data, whereas the Data-Member is a discrete recordset within the DataSource.ItemData Type: Binding ObjectReturns a Binding object.If you use a key string in the Add method, youcan also access the Binding object by key, as in:obcAuthors.Item("fname")UpdateModeType: UpdateMode enumerated constant (see below)Specifies at what stage the data source is updated.However, if you opena recordset as read-only, no update occurs.BindingCollection Object (VB6) 135VB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.The Language ReferenceUpdateMode ConstantsvbUpdateWhenPropertyChangesValue: 1Don t use this constant when dealing with VB class data sources.I foundit disabled the Binding object s DataChanged property.vbUpdateWhenRowChangesValue: 2The recordset is updated.vbUsePropertyAttributesValue: 0Not relevant to class data sources
[ Pobierz całość w formacie PDF ]