Validate Fields

Notice that as well as the properties taken from the database, which we use to store the customer information, we have three extra properties - EditMode, ItemsDataMember and ItemsRecordset -and one friend method, ValidateFields.

For each of the properties, we need to write two routines - one for reading the value of the property (Property Get) and one for setting it (Property Let). However, if the property contains an object (rather than, say, a string or a numeric value), Visual Basic requires us to use Property Set instead of Property Let.

Remember that we said that we would allow the attributes of the object to be accessed either through properties or through a recordset? That means we need a property which contains a Recordset object. The ItemsRecordset property holds an ADO recordset which contains this information. This recordset has a field for each of our Customer object's properties, so when we want to read or set one of these properties, we will actually access the appropriate field in this recordset. For example, the routine for getting the value of the Address property looks like this:

Public Property Get Address() As String On Error Resume Next

Address = ItemsRecordset.Fields(g_cstrFieldAddress) If Err.Number <> 0 Then

If Err.Number <> 3265 Then

Err.Raise Err.Number, "AddressGet", Err.Description End If End If End Property

The variable g_cstrFieldAddress is simply a constant (a variable whose value is set when the class is initialized and cannot be changed) which is set to the name of the Address field in the database (i.e. "Address"). The key line of this routine is therefore equivalent to:

Address = ItemsRecordset.Fields("Address")

So when the managing class attempts to read the value of the Address property, this routine will be executed and the value of the Address field in ItemsRecordset will be returned. The managing class can thus access the customer's address in two ways: by reading the property directly (that is, by calling obj Customer .Address) or by accessing all the customer information by calling obj Customer .ItemsRecordset. Similarly, when we wish to set the value of this property, we will set the value of the appropriate field in ItemsRecordset:

Public Property Let Address(ByVal v_strNewAddress As String) On Error GoTo AddressLetError If m_blnValidatingFieldChange = True Then

Exit Property End If

If m_eEditMode = adEditInProgress Or m_eEditMode = adEditAdd Then m_blnIgnoreFieldChange = True

ItemsRecordset.Fields(g_cstrFieldAddress) = v_strNewAddress ElseIf m_eEditMode = adEditNone Then

Err.Raise 1001, "Address Let:", GetErrorText(errChangeFieldNoEdit) End If

Exit Property AddressLetError:

Err.Raise Err.Number, "Address Let " & Err.Source, Err.Description End Property

This code is considerably more complex than that for the corresponding Property Get because of the need to ensure that we don't allow data conflicts if more than one user is accessing the same record. Firstly, we check whether a change to the property is still being validated; if so, we abandon execution of this routine. Then we check our EditMode property; this contains the value of an ADO constant which indicates whether a record is currently being edited, or whether it is being added or deleted. We can use this property to keep track of what operation is being performed, or to check whether a record is being edited by another user. If the record to be changed is being edited or added to the recordset, then we set the field in the recordset in order to set the property; otherwise, we raise an error.

The final property, ItemsDataMember, returns a reference to the set of data or 'data member' for the object's attributes, in case the data source is exposing more than one set of data.

The ValidateFields method is used to validate a field in our recordset. All of the validation of the fields is done in the properties. However, we can change a field either by using a property or by using the recordset, so we will also need a way to validate the fields when they have been changed using the recordset. ValidateFields will be called any time a field changes using the recordset. Since the code to validate a field is already in the properties, the ValidateFields method will pass the new field value into the property.

365 Days Of Motivation

365 Days Of Motivation

Stop Wasting Time And Learn How To Stay Motivated. Finally! Discover How To Stop Your Mind From Wandering, And Upgrade Your Motivation. You Can Hack Your Motivation Levels, Allowing You To Take Your Life To The Next Level.

Get My Free Ebook


Post a comment