Quantcast
Viewing all articles
Browse latest Browse all 5

Visual basic 6 Classes

Creating Custom Classes in VB6

  • Requirements: Microsoft ActiveX Data Objects 2.0 Library (msado20.tlb)
  • Level: Advance
  • Language: Microsoft Visual Basic 6.0
  • Knowledge Background: ADO, Classes, VB6

ADO Class in VB6

Visual Basic 6.0 provides support for defining classes through the class module. The definition of a class is stored in a special file type, the .cls file. One class is defined in each class module. Some of the definition of the class is embedded in the .cls file and only editable through the project system. The .cls extension is the class module, wherein you place your class codes, a Class in vb6 is also a template, creating an instance of this class using the new keyword. In this article I am going to show you how to create a class in vb6.

Add Class on your project

If you haven’t done so you can check the figure below on how to add class Module on your project, Right click your project name, select Add on Sub menu and then select the Class Module, rename it to “clsAdo.cls” and paste all the content of the code sample below into that newly created class module.

Image may be NSFW.
Clik here to view.
Adding Class Module in VB6 Project

Sample #1: clsAdo.cls

'*********************************************************************************
'*************ClsAdo created by Nolan Sunico *************************************
'*************This is a Customed class ADO ***************************************
'*************that you can use to update/insert/delete data **********************
'*************Created January 19, 2009 2:00 PM ***********************************
'*********************************************************************************

Option Explicit
Dim clsCon As New ADODB.Connection
Dim clsRS As New ADODB.Recordset
Dim Connectfail As Boolean
Dim m_ConnectionString As String
Dim m_RecordSource As String
Dim m_TotalRecords As Integer

Public Function CheckConnection() As Boolean
On Error GoTo CheckErr

CheckConnection = True
CheckExit:
   Exit Function
CheckErr:
   CheckConnection = False
End Function

Public Property Get ConnectionString() As String
   ConnectionString = m_ConnectionString
End Property

Public Property Let ConnectionString(ByVal vNewValue As String)
   m_ConnectionString = vNewValue
   clsCon.ConnectionString = m_ConnectionString
End Property

Public Property Get RecordSource() As String
   RecordSource = m_RecordSource
End Property

Public Property Let RecordSource(ByVal vNewValue As String)
   m_RecordSource = vNewValue
End Property

Public Sub Refresh()
On Error GoTo OpenErr

clsCon.Open
clsCon.Execute RecordSource, m_TotalRecords
With clsRS
   .CursorLocation = adUseClient
   .CursorType = adOpenKeyset
   .LockType = adLockOptimistic
   .Properties("IRowsetIdentity") = True 'remove comment to display data
   .Open RecordSource, clsCon, adOpenDynamic, adLockOptimistic
End With

OpenExit:
   Exit Sub
OpenErr:
   MsgBox "Error: During Openning Connection, " & Err.Description, vbCritical
   Resume OpenExit
End Sub

Public Property Get Recordset() As ADODB.Recordset
   Set Recordset = clsRS
End Property

Public Property Let Recordset(ByVal vNewValue As ADODB.Recordset)
   Set clsRS = vNewValue
End Property

Public Sub Dispose()
   If clsCon.State = adStateOpen Then
      clsCon.Close
   End If
   Set clsRS = Nothing
   Set clsCon = Nothing
End Sub

Public Property Get TotalRecords() As Integer
   TotalRecords = m_TotalRecords
End Property

Public Sub Execute(SQLCommand As String, Optional ByRef RecordsAffected As Variant = Empty, Optional ByRef myRecordSet As Variant = Empty)
   On Error GoTo ExeErr

   If clsCon.State = adStateClosed Then
      clsCon.Open
   End If
   If RecordsAffected = Empty And myRecordSet = Empty And RecordsAffected <> 0 Then
      clsCon.Execute SQLCommand
   ElseIf RecordsAffected = Empty And myRecordSet <> Empty Then
      Set myRecordSet = clsCon.Execute(SQLCommand)
   ElseIf RecordsAffected <> Empty And myRecordSet = Empty Then
      clsCon.Execute SQLCommand, RecordsAffected
   ElseIf RecordsAffected <> Empty And myRecordSet <> Empty Then
      Set myRecordSet = clsCon.Execute(SQLCommand, RecordsAffected)
   ElseIf RecordsAffected = Empty And myRecordSet = Empty Then
      clsCon.Execute SQLCommand, RecordsAffected
   End If

ExeExit:
   Exit Sub
ExeErr:
   MsgBox "Error: " &amp;amp; Err.Description, vbInformation
   Resume ExeExit
End Sub

The Above Sample Create a Class name clsAdo, with four(4) Properties and four(4) subroutines.

Properties:

  1. ConnectionString = referenced a connectionstring to connect to database.
  2. RecordSource= The Source or Query to execute against the database.
  3. Recordset= The recordset return by the query.
  4. TotalRecords= The Total record returned by the query.

Subroutines:

  1. CheckConnection =Checks if the connection is valid or not.
  2. Dispose= Close Connection and free all references.
  3. Execute= Execute or runs a command.
  4. Refresh=Open a connection to database and refreshes it recordset.

In your main module to create an instance of this class, sample is given as shown:

   dim cAdo as new clsAdo()
   cAdo.ConnectionString="DSN=test"
   cAdo.RecordSource="Select * from tblpersons"
   cAdo.Refresh
   'To Extract recordset;
   dim rs as new adodb.recordset
   set rs=cAdo.Recordset
   'To Check the record count
   dim i as integer
   i=cAdo.TotalRecords

For more Sample Code, please visit www.sourcehints.com from time to time, for any new updates

Keywords: , , , , , , , , , ,

Other reading this article are also reading these:


Viewing all articles
Browse latest Browse all 5

Trending Articles