Wednesday, 7 March 2012

Creating Classes for User Objects

The below program uses a class module to create a user object for customer databases. Customer information is stored in a user object that adds, update or deletes customer information from a database.
The customer_retrieve method retrieves or gets customer information using a ADO connection and the Customer_update method updates data in the tabels. The let and get methods updates the properties of the customer object.
Option Explicit
Private MCustomerID As String
Private MCustomerName As String
Private MPhoneNumberR As Long
Private MPhoneNumberM As Long
Private MCustomerAddress As String
Private MCustomer_ID_Date As Date
Private MCompanyName As String
Private MRegistrationNumber As Long
Private MRegistrationDate As Date
Private MCompanyAddress As String
Public Property Let CustomerID(ByVal cID As String)
MCustomerID = cID
End Property
Public Property Get CustomerID() As String
CustomerID = MCustomerID
End Property
Public Property Let CustomerName(ByVal CName As String)
MCustomerName = CName
End Property
Public Property Get CustomerName() As String
CustomerName = MCustomerName
End Property
Public Property Let PhoneNumberR(ByVal PR As Long)
MPhoneNumberR = PR
End Property
Public Property Get PhoneNumberR() As Long
'Dim PhoneNumberR As Long
PhoneNumberR = MPhoneNumberR
End Property
Public Property Let PhoneNumberM(ByVal PM As Long)
MPhoneNumberM = PM
End Property
Public Property Get PhoneNumberM() As Long
'Dim PhoneNumberM As Long
PhoneNumberM = MPhoneNumberM
End Property
Public Property Let CustomerAddress(ByVal CA As String)
MCustomerAddress = CA
End Property
Public Property Get CustomerAddress() As String
CustomerAddress = MCustomerAddress
End Property
Public Property Let Customer_ID_Date(ByVal CIDDate As Date)
MCustomer_ID_Date = CIDDate
End Property
Public Property Get Customer_ID_Date() As Date
Customer_ID_Date = MCustomer_ID_Date
End Property
Public Property Let CompanyName(ByVal CN As String)
MCompanyName = CN
End Property
Public Property Get CompanyName() As String
CompanyName = MCompanyName
End Property
Public Property Let RegistrationNumber(ByVal RN As Long)
MRegistrationNumber = RN
End Property
Public Property Get RegistrationNumber() As Long
'Dim RegistrationNumber As Long
RegistrationNumber = MRegistrationNumber
End Property
Public Property Let RegistrationDate(ByVal RD As Date)
MRegistrationDate = RD
End Property
Public Property Get RegistrationDate() As Date
RegistrationDate = MRegistrationDate
End Property
Public Property Let CompanyAddress(ByVal CAdd As String)
MCompanyAddress = CAdd
End Property
Public Property Get CompanyAddress() As String
CompanyAddress = MCompanyAddress
End Property
'Public Property Set Customer_Object(objOwner As clsCustomer)
'Set objCust = objOwner
'End Property
Public Sub customer_update()
Dim ado As ADODB.Connection
Dim rs As ADODB.Recordset
Set ado = New ADODB.Connection
ado.Open "DSN=cis_dsn"
Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
rs.Source = "customer"
rs.ActiveConnection = ado
rs.Open
rs.AddNew
rs.Fields("Customer ID") = MCustomerID
rs.Fields("Customer Name") = MCustomerName
rs.Fields("Customer Address") = MCustomerAddress
rs.Fields("Phone Number (R)") = MPhoneNumberR
rs.Fields("Phone Number (M)") = MPhoneNumberM
rs.Fields("Customer ID Date") = MCustomer_ID_Date
rs.Fields("Company Name") = MCompanyName
rs.Fields("Registration Number") = MRegistrationNumber
rs.Fields("Registration Date") = MRegistrationDate
rs.Fields("Company Address") = MCompanyAddress
rs.Update
rs.Close
ado.Close
Set rs = Nothing
End Sub
Public Function customer_retrieve(MCustID As String)
Dim myConnection As ADODB.Connection
Dim myRecordSet As ADODB.Recordset
Set myConnection = New ADODB.Connection
myConnection.Open "DSN=cis_dsn"
Set myRecordSet = New ADODB.Recordset
myRecordSet.CursorType = adOpenKeyset
myRecordSet.LockType = adLockOptimistic
myRecordSet.Source = "customer"
myRecordSet.ActiveConnection = myConnection
myRecordSet.Open ("Select * From Customer")
'Set myRecordSet = myConnection.Execute()
Do While Not myRecordSet.EOF
If myRecordSet.Fields(0) = MCustID Then
'MsgBox "" & myRecordSet.Fields(0)
CustomerID = myRecordSet.Fields("Customer ID")
CustomerName = myRecordSet.Fields("Customer Name")
CustomerAddress = myRecordSet.Fields("Customer Address")
PhoneNumberR = myRecordSet.Fields("Phone Number (R)")
PhoneNumberM = myRecordSet.Fields("Phone Number (M)")
Customer_ID_Date = myRecordSet.Fields("Customer ID Date")
CompanyName = myRecordSet.Fields("Company Name")
RegistrationNumber = myRecordSet.Fields("Registration Number")
RegistrationDate = myRecordSet.Fields("Registration Date")
CompanyAddress = myRecordSet.Fields("Company Address")
End If
myRecordSet.MoveNext
Loop
'myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=D:\CIS\CIS.mdb"
' Create a Recordset by executing a SQL statement
' Show the first title in the recordset.
'MsgBox myRecordSet("Title")
' Close the recordset and connection.
myRecordSet.Close
myConnection.Close
Set myRecordSet = Nothing
End Function

No comments:

Post a Comment