Below, we add a property to allow the user to get the value of the Customerid field, the corresponding sample code is as follows:
Public Property Get CustomerID () As String CustomerID = rs ( "CustomerID") End Property Public Property Let CustomerID (NewValue As String) rs ( "CustomerID") = NewValue End Property Obviously, Get operation of the property simply returns "CustomerID "The value of the field, the LET operation is set to set the" CustomerID "field.
In other words, there are two parts: "getting" and "letting", in fact there may be another "setting" action. But for different occasions, we always need GET and LET to make read and write operations.
It is noted here that the necessary detection should be performed during the above properties. For example, when calling a list attribute, the user may have the following:
Objectname.customerid = "HALFI"
After the LET property is operated, "Customerid" is equal to the new string "HALFI". But when you look at the Northwind database content, we will find the character length of the "Customerid" field that cannot exceed 5. If the user has this operation:
Objectname.customerid = "halfistore"
The database operation error will appear. Although it can be handled by the error handle, if you can detect NEWVALUE in your code? If this value exceeds 5 characters, we can ignore this new string to pop up an error prompt by cropping a total of 5 characters. But here, we use the latter measures.
Add the following code in our class:
Public Property Get CustomerID () As String CustomerID = rs ( "CustomerID") End Property Public Property Let CustomerID (NewValue As String) 'If the length of NewValue is greater than five If Len (NewValue)> 5 Then' ... then Raise an error to the program 'using this class err.raise vbobjectError 1, "Customerid", _ "Customer ID Can Only Be Up to Five" & _ "Characters Long!" Else' ... Otherwise, Change The Field Value RS ("Customerid") = NewValue End if End Property
Ok, we have spent a lot of time for the addition method before completing the following steps.
Add the following code in our class:
Public Sub Update () rs.Update End Sub
The UPDATE method simply invokes the UPDATE method of the record set object to update the record. Next, we will use a small sample program to test this property and method, and you will use specific techniques to track the operation of classes and programs during testing.