-> DimWithEventsrsAsRecordset
PublicEventRecordsetMove()
PrivateSubClass_Initialize()
Setrs=NewRecordset
rs.ActiveConnection=”Provider=Microsoft.”&_”Jet.OLEDB.4.0;DataSource=C:\ProgramFiles\”&_”MicrosoftVisualStudio\VB98\Nwind.mdb;”&_”PersistSecurityInfo=False”
rs.Open”select*fromcustomers”,,adOpenKeyset,adLockOptimistic
EndSub
PrivateSubClass_Terminate()
rs.Close
Setrs=Nothing
EndSub
PublicPropertyGetCustomerID()AsString
CustomerID=rs(“CustomerID”)
EndProperty
PublicPropertyLetCustomerID(NewValueAsString)
IfthelengthofNewValueisgreaterthanfive
IfLen(NewValue)>5Then
…thenraiseanerrortotheprogram
usingthisclass,byrunning
Err.RaisevbObjectError OurErrorNumber
Err.RaisevbObjectError 1,”CustomerID”,_”CustomerIDcanonlybeuptofive”&_”characterslong!”
Else
…otherwise,changethefieldvalue
rs(“CustomerID”)=NewValue
EndIf
EndProperty
PublicPropertyGetCompanyName()AsVariant
CompanyName=rs(“CompanyName”)
EndProperty
PublicPropertyLetCompanyName(ByValNewValueAsVariant)
rs(“CompanyName”)=NewValue
EndProperty
PublicPropertyGetContactName()AsVariant
ContactName=rs(“ContactName”)
EndProperty
PublicPropertyLetContactName(ByValNewValueAsVariant)
rs(“ContactName”)=NewValue
EndProperty
PublicPropertyGetContactTitle()AsVariant
ContactTitle=rs(“ContactTitle”)
EndProperty
PublicPropertyLetContactTitle(ByValNewValueAsVariant)
rs(“ContactTitle”)=NewValue
EndProperty
PublicPropertyGetAddress()AsVariant
Address=rs(“Address”)
EndProperty
PublicPropertyLetAddress(ByValNewValueAsVariant)
rs(“Address”)=NewValue
EndProperty
PublicPropertyGetCity()AsVariant
City=rs(“City”)
EndProperty
PublicPropertyLetCity(ByValNewValueAsVariant)
rs(“City”)=NewValue
EndProperty
PublicPropertyGetRegion()AsVariant
Region=rs(“Region”)
EndProperty
PublicPropertyLetRegion(ByValNewValueAsVariant)
rs(“Region”)=NewValue
EndProperty
PublicPropertyGetPostalCode()AsVariant
PostalCode=rs(“PostalCode”)
EndProperty
PublicPropertyLetPostalCode(ByValNewValueAsVariant)
rs(“PostalCode”)=NewValue
EndProperty
PublicPropertyGetCountry()AsVariant
Country=rs(“Country”)
EndProperty
PublicPropertyLetCountry(ByValNewValueAsVariant)
rs(“Country”)=NewValue
EndProperty
PublicPropertyGetPhone()AsVariant
Phone=rs(“Phone”)
EndProperty
PublicPropertyLetPhone(ByValNewValueAsVariant)
rs(“Phone”)=NewValue
EndProperty
PublicPropertyGetFax()AsVariant
Fax=rs(“Fax”)
EndProperty
PublicPropertyLetFax(ByValNewValueAsVariant)
rs(“Fax”)=NewValue
EndProperty
PublicSubAddNew()
rs.AddNew
EndSub
PublicSubUpdate()
rs.Update
EndSub
PublicSubCancelUpdate()
Ifrs.EditMode=adEditInProgressOr_rs.EditMode=adEditAddThen
rs.CancelUpdate
EndIf
EndSub
PublicSubMoveNext()
rs.MoveNext
EndSub
PublicSubMovePrevious()
rs.MovePrevious
EndSub
PublicSubMoveFirst()
rs.MoveFirst
EndSub
PublicSubMoveLast()
rs.MoveLast
EndSub
PublicFunctionFindByCustomerID(CustomerIDAsString)AsBoolean
UsestheFindmethodtolocatecustomers
withamatchingCustomerID.
ReturnsTruevalueiscustomer(s)found
DimvarBookmarkAsVariant
rs.MoveFirst
rs.Find(“CustomerID=”&CustomerID&””)
Ifrs.EOF=TrueThen
FindByCustomerID=False
rs.Bookmark=varBookmark
Else
FindByCustomerID=True
EndIf
EndFunction
PublicPropertyGetEOF()AsBoolean
Exampleofaread-onlyproperty
NoPropertyLetshere
EOF=rs.EOF
EndProperty
PublicPropertyGetBOF()AsBoolean
Anotherexampleofaread-onlyproperty
BOF=rs.BOF
EndProperty
PrivateSubrs_MoveComplete(ByValadReasonAsADODB.EventReasonEnum,_
ByValpErrorAsADODB.Error,adStatusAsADODB.EventStatusEnum,_
ByValpRecordsetAsADODB.Recordset)
ReactstotherecordsetMoveComplete
method-raiseseventwitheachmove
RaiseEventRecordsetMove
EndSub->
需要说明的是:迄今为止,我们仅仅是在一个类中添加代码。当然,也可以选择”Project”->”AddClass”菜单来向工程添加多个类,而且还可利用”collections”使这些类工作在一起。但是在这里,我们仍然想用一个类来处理一个数据表。