假如愿意,仍然可以按F9高亮显示"Test.CustomerID =" 这条语句,然后按F8单步运行来查看其工作情况。
至此,我们已经成功地创建并测试一个简单的基于数据库的类。但是,还没有对customerID的字符串长度作测试,如果其长度超过5个字符,看看会发生什么?
下一步,我们将扩充并改进这个数据库类。
首先添加类的几个特征:其他的属性、一些方法甚至一两个事件。 其相应的代码如下:
需要说明的是:迄今为止,我们仅仅是在一个类中添加代码。当然,也可以选择"Project"->"Add Class"菜单来向工程添加多个类,而且还可利用"collections"使这些类工作在一起。但是在这里,我们仍然想用一个类来处理一个数据表。
将上述类的代码复制并粘贴到自己的类中,下一节将讨论该程序的编译。[@more@]
至此,我们已经成功地创建并测试一个简单的基于数据库的类。但是,还没有对customerID的字符串长度作测试,如果其长度超过5个字符,看看会发生什么?
下一步,我们将扩充并改进这个数据库类。
首先添加类的几个特征:其他的属性、一些方法甚至一两个事件。 其相应的代码如下:
Dim WithEvents rs As Recordset Public Event RecordsetMove() Private Sub Class_Initialize() Set rs = New Recordset rs.ActiveConnection = "Provider=Microsoft." & _"Jet.OLEDB.4.0;Data Source=C:Program Files" & _"Microsoft Visual StudioVB98Nwind.mdb;" & _"Persist Security Info=False" rs.Open "select * from customers", , adOpenKeyset, adLockOptimistic End Sub Private Sub Class_Terminate() rs.Close Set rs = Nothing End Sub 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, by running 'Err.Raise vbObjectError + OurErrorNumber 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 Public Property Get CompanyName() As Variant CompanyName = rs("CompanyName") End Property Public Property Let CompanyName(ByVal NewValue As Variant) rs("CompanyName") = NewValue End Property Public Property Get ContactName() As Variant ContactName = rs("ContactName") End Property Public Property Let ContactName(ByVal NewValue As Variant) rs("ContactName") = NewValue End Property Public Property Get ContactTitle() As Variant ContactTitle = rs("ContactTitle") End Property Public Property Let ContactTitle(ByVal NewValue As Variant) rs("ContactTitle") = NewValue End Property Public Property Get Address() As Variant Address = rs("Address") End Property Public Property Let Address(ByVal NewValue As Variant) rs("Address") = NewValue End Property Public Property Get City() As Variant City = rs("City") End Property Public Property Let City(ByVal NewValue As Variant) rs("City") = NewValue End Property Public Property Get Region() As Variant Region = rs("Region") End Property Public Property Let Region(ByVal NewValue As Variant) rs("Region") = NewValue End Property Public Property Get PostalCode() As Variant PostalCode = rs("PostalCode") End Property Public Property Let PostalCode(ByVal NewValue As Variant) rs("PostalCode") = NewValue End Property Public Property Get Country() As Variant Country = rs("Country") End Property Public Property Let Country(ByVal NewValue As Variant) rs("Country") = NewValue End Property Public Property Get Phone() As Variant Phone = rs("Phone") End Property Public Property Let Phone(ByVal NewValue As Variant) rs("Phone") = NewValue End Property Public Property Get Fax() As Variant Fax = rs("Fax") End Property Public Property Let Fax(ByVal NewValue As Variant) rs("Fax") = NewValue End Property Public Sub AddNew() rs.AddNew End Sub Public Sub Update() rs.Update End Sub Public Sub CancelUpdate() If rs.EditMode = adEditInProgress Or _rs.EditMode = adEditAdd Then rs.CancelUpdate End If End Sub Public Sub MoveNext() rs.MoveNext End Sub Public Sub MovePrevious() rs.MovePrevious End Sub Public Sub MoveFirst() rs.MoveFirst End Sub Public Sub MoveLast() rs.MoveLast End Sub Public Function FindByCustomerID(CustomerID As String) As Boolean 'Uses the Find method to locate customers 'with a matching CustomerID. 'Returns True value is customer(s) found Dim varBookmark As Variant rs.MoveFirst rs.Find ("CustomerID='" & CustomerID & "'") If rs.EOF = True Then FindByCustomerID = False rs.Bookmark = varBookmark Else FindByCustomerID = True End If End Function Public Property Get EOF() As Boolean 'Example of a read-only property No Property Lets here EOF = rs.EOF End Property Public Property Get BOF() As Boolean 'Another example of a read-only property BOF = rs.BOF End Property Private Sub rs_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, _ ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, _ ByVal pRecordset As ADODB.Recordset) 'Reacts to the recordset MoveComplete 'method - raises event with each move RaiseEvent RecordsetMove End Sub |
需要说明的是:迄今为止,我们仅仅是在一个类中添加代码。当然,也可以选择"Project"->"Add Class"菜单来向工程添加多个类,而且还可利用"collections"使这些类工作在一起。但是在这里,我们仍然想用一个类来处理一个数据表。
将上述类的代码复制并粘贴到自己的类中,下一节将讨论该程序的编译。[@more@]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-925173/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8781179/viewspace-925173/