Option Explicit

' Module Name: clsUserColl.cls
' This module was created with 'COM-Gen 2000' R1
' ©1999 Q&D Software Development
' http://www.q-d.com
'
' [Developer Notes from 'COM-Gen 2000']
'      This module instantiates the clsCG2KConn object, which utilizes
'      the ADO library.  Don't forget to refer to it.  To do so in VB6,
'      click 'Project->References' from the VB menu, and check the
'      Microsoft ActiveX Data Objects 2.0 Library.
'=========================================================================
' This module was created using a copy of COM-Gen 2000
' licensed to a single user.  Although the code generated by this
' licensed copy of COM-Gen 2000 can be used anywhere,
' the COM-Gen 2000 program itself may only be used on a single
' machine, by a single user, according to the License you purchased.
'=========================================================================
Private mCollection As Collection
Private msRefreshFilter As String
Private msSortOrder As String
Private moConn As clsCG2KConn
Private Sub Class_Initialize()
    Set mCollection = New Collection
    Set moConn = New clsCG2KConn
End Sub
Private Sub Class_Terminate()
    Set mCollection = Nothing
    Set moConn = Nothing
End Sub

'=========================================================================
' Procedure Name: Property Get RefreshFilter ()
' Purpose: Returns the RefreshFilter property of the
'          clsUserColl object.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Get RefreshFilter() As String
On Error GoTo RefreshFilterErr

    RefreshFilter = msRefreshFilter

    Exit Property

RefreshFilterErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.RefreshFilter)", Err.Description

End Property

'=========================================================================
' Procedure Name: Property Let RefreshFilter ()
' Purpose: Sets the RefreshFilter property of the
'          clsUserColl object.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Let RefreshFilter(ByVal vsRefreshFilter As String)
On Error GoTo RefreshFilterErr

    msRefreshFilter = vsRefreshFilter

    Exit Property

RefreshFilterErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.RefreshFilter)", Err.Description

End Property


'=========================================================================
' Procedure Name: Property Get SortOrder ()
' Purpose: Returns the SortOrder property of the
'          clsUserColl object.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Get SortOrder() As String
On Error GoTo SortOrderErr

    SortOrder = msSortOrder

    Exit Property

SortOrderErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.SortOrder)", Err.Description

End Property

'=========================================================================
' Procedure Name: Property Let SortOrder ()
' Purpose: Sets the SortOrder property of the
'          clsUserColl object.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Let SortOrder(ByVal vsSortOrder As String)
On Error GoTo SortOrderErr

    msSortOrder = vsSortOrder

    Exit Property

SortOrderErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.SortOrder)", Err.Description

End Property

'=========================================================================
' Procedure Name: Property Get Count ()
' Purpose: Returns the the number of clsUser objects
'          currently in the clsUserColl collection.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Get Count() As Long
On Error GoTo CountErr

    Count = mCollection.Count

    Exit Property

CountErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.Count)", Err.Description

End Property

'=========================================================================
' Procedure Name: Property Get Item ()
' Purpose: Returns the a clsUser object
'          from the clsUserColl collection
'          based on the index passed.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Get Item(ByVal vlIndex As Long) As clsUser
On Error GoTo ItemErr

    Set Item = mCollection(vlIndex)

    Exit Property

ItemErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.Item)", Err.Description

End Property

'=========================================================================
' Procedure Name: Property Get NewEnum ()
' Purpose: Enables For Each...In functionality for the
'          clsUserColl object.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
'=========================================================================
Public Property Get NewEnum() As IUnknown

    Set NewEnum = mCollection.[_NewEnum]

End Property

'=========================================================================
' Procedure Name: Add()
' Purpose: Inserts a record into the user table and adds the
'          passed in clsUser object to the
'          clsUserColl collection.
' Created 8/26/99 7:37:26 AM by 'COM-Gen 2000'
' Revision History:
'
' [Developer Notes from 'COM-Gen 2000']
'      You indicated that the following fields would be automatically
'      generated by the database:
'
'      user_id
'
'      To call this method from an ASP page:
'
'          Set loUser = Server.CreateObject(".clsUser")
'          Set loUserColl = Server.CreateObject(".clsUserColl")
'
'          loUser.User_type_cd = 
'          loUser.User_login_name = 
'          loUser.User_login_password = 
'          loUser.User_created_by = 
'          loUser.User_created_dt = 
'          loUser.User_updated_by = 
'          loUser.User_updated_dt = 
'
'          If loUserColl.Add(loUser) Then
'              MsgBox "loUser.User_id = " & loUser.User_id
'          End If
'
'      To call this method from VB:
'
'          Dim loUser As New [Your Library Name.]clsUser
'          Dim loUserColl As New [Your Library Name.]clsUserColl
'
'          loUser.User_type_cd = 
'          loUser.User_login_name = 
'          loUser.User_login_password = 
'          loUser.User_created_by = 
'          loUser.User_created_dt = 
'          loUser.User_updated_by = 
'          loUser.User_updated_dt = 
'
'          If loUserColl.Add(loUser) Then
'              MsgBox "loUser.User_id = " & loUser.User_id
'          End If
'
'=========================================================================
Public Function Add(ByRef roUser As clsUser) As Boolean
On Error GoTo AddErr
Dim lsSQL As String
Dim lrsID As New ADODB.Recordset


    lsSQL = "INSERT INTO user ("
    If roUser.User_type_cd <> "" Then
        lsSQL = lsSQL & "user_type_cd, "
    End If
    If roUser.User_login_name <> "" Then
        lsSQL = lsSQL & "user_login_name, "
    End If
    If roUser.User_login_password <> "" Then
        lsSQL = lsSQL & "user_login_password, "
    End If
    If roUser.User_created_by <> "" Then
        lsSQL = lsSQL & "user_created_by, "
    End If
    If roUser.User_created_dt <> "" Then
        lsSQL = lsSQL & "user_created_dt, "
    End If
    If roUser.User_updated_by <> "" Then
        lsSQL = lsSQL & "user_updated_by, "
    End If
    If roUser.User_updated_dt <> "" Then
        lsSQL = lsSQL & "user_updated_dt, "
    End If
    lsSQL = Left(lsSQL, Len(lsSQL) - 2) & ") "
    lsSQL = lsSQL & "VALUES("
    If roUser.User_type_cd <> "" Then
        lsSQL = lsSQL & roUser.User_type_cd & ", "
    End If
    If roUser.User_login_name <> "" Then
        lsSQL = lsSQL & "'" & Replace(roUser.User_login_name, "'", "''") & "', "
    End If
    If roUser.User_login_password <> "" Then
        lsSQL = lsSQL & "'" & Replace(roUser.User_login_password, "'", "''") & "', "
    End If
    If roUser.User_created_by <> "" Then
        lsSQL = lsSQL & "'" & Replace(roUser.User_created_by, "'", "''") & "', "
    End If
    If roUser.User_created_dt <> "" Then
        lsSQL = lsSQL & "#" & roUser.User_created_dt & "#, "
    End If
    If roUser.User_updated_by <> "" Then
        lsSQL = lsSQL & "'" & Replace(roUser.User_updated_by, "'", "''") & "', "
    End If
    If roUser.User_updated_dt <> "" Then
        lsSQL = lsSQL & "#" & roUser.User_updated_dt & "#, "
    End If
    lsSQL = Left(lsSQL, Len(lsSQL) - 2) & ")"

    If moConn.ExecActionSQL(lsSQL) Then
        lsSQL = "SELECT MAX(user_id) FROM user"
        If moConn.ExecSQL(lsSQL, lrsID) > 0 Then
            roUser.User_id = lrsID(0)
        Else
            Exit Function
        End If
        mCollection.Add roUser
        Add = True
    End If

    Exit Function

AddErr:
    RaiseError Err.Number, Err.Source & " (clsUserColl.Add)", Err.Description

End Function

'=========================================================================
' Procedure Name: Refresh()
' Purpose: Refreshes the clsUserColl based upon the
'         RefreshFilter property setting.
' Created 8/26/99 7:37:27 AM by 'COM-Gen 2000'
' Revision History:
'
' [Developer Notes from 'COM-Gen 2000']
'      If no Refresh Filter is set, calling this method will cause
'      the clsUserColl collection object to contain one
'      clsUser object for each row in the 'user'
'      table.  You may filter your results by setting the RefreshFilter
'      property prior to calling this method.
'
'      To call this method from an ASP page:
'
'          Set loUser = Server.CreateObject(".clsUser")
'          Set loUserColl = Server.CreateObject(".clsUserColl")
'
'
'          loUserColl.RefreshFilter = "user_id =  AND user_type_cd =  AND user_login_name = '' AND user_login_password = '' "
'          loUserColl.SortOrder = "user_id, user_type_cd, user_login_name, user_login_password"
'          loUserColl.Refresh
'
'          For Each loUser In loUserColl
'             MsgBox loUser.
'          Next
'
'      To call this method from VB:
'
'          Dim loUser As New [Your Library Name.]clsUser
'          Dim loUserColl As New [Your Library Name.]clsUserColl
'
'
'          loUserColl.RefreshFilter = "user_id =  AND user_type_cd =  AND user_login_name = '' AND user_login_password = '' "
'          loUserColl.SortOrder = "user_id, user_type_cd, user_login_name, user_login_password"
'          loUserColl.Refresh
'
'          For Each loUser In loUserColl
'             MsgBox loUser.
'          Next
'
'=========================================================================
Public Sub Refresh()
On Error GoTo RefreshErr
Dim lsSQL As String
Dim lrsUsers As New ADODB.Recordset
Dim loUser As clsUser
Dim lcollUsers As New Collection


    lsSQL = "SELECT " _
             & "user_id, user_type_cd, user_login_name, user_login_password, user_created_by, user_created_dt, user_updated_by, user_updated_dt " _
             & "FROM user "
    If RefreshFilter <> "" Then
        lsSQL = lsSQL & "WHERE " & RefreshFilter
    End If

    If SortOrder <> "" Then
        lsSQL = lsSQL & " ORDER BY " & SortOrder
    End If

    Set mCollection = Nothing
    Set mCollection = New Collection

    If moConn.ExecSQL(lsSQL, lrsUsers) > 0 Then
        Do While Not lrsUsers.EOF
            Set loUser = New clsUser
            loUser.mbLoading = True
            'Assign object properties for each column
            loUser.User_id = lrsUsers("user_id")
            If Not IsNull(lrsUsers("user_type_cd")) Then
                loUser.User_type_cd = lrsUsers("user_type_cd")
            End If
            loUser.User_login_name = "" & lrsUsers("user_login_name")
            loUser.User_login_password = "" & lrsUsers("user_login_password")
            loUser.User_created_by = "" & lrsUsers("user_created_by")
            If Not IsNull(lrsUsers("user_created_dt")) Then
                loUser.User_created_dt = lrsUsers("user_created_dt")
            End If
            loUser.User_updated_by = "" & lrsUsers("user_updated_by")
            If Not IsNull(lrsUsers("user_updated_dt")) Then
                loUser.User_updated_dt = lrsUsers("user_updated_dt")
            End If
            loUser.mbLoading = False
            mCollection.Add loUser
            Set loUser = Nothing
            lrsUsers.MoveNext
        Loop
    End If
    lrsUsers.Close

    Exit Sub

RefreshErr:
    loUser.mbLoading = False
    RaiseError Err.Number, Err.Source & " (clsUserColl.Refresh)", Err.Description

End Sub

'=========================================================================
' Procedure Name: Remove()
' Purpose: Deletes records from the user table and refreshes
'          the clsUserColl collection based on the
'          filter passed into the method.  A blank filter will be rejected
'          by this method for safety reasons.
' Created 8/26/99 7:37:27 AM by 'COM-Gen 2000'
' Revision History:
'
' [Developer Notes from 'COM-Gen 2000']
'      This method uses a passed in filter to determine
'      which records to delete from the user table.  It then
'      causes the clsUserColl collection object to be
'      refreshed.
'
'      To call this method from an ASP page:
'
'          Set loUserColl = Server.CreateObject(".clsUserColl")
'
'          lsFilter = "user_id =  "
'          If Not loUserColl.Remove lsFilter Then
'              Response.Write "Error!"
'          End If
'
'      To call this method from VB:
'
'          Dim loUserColl As New [Your Library Name.]clsUserColl
'          Dim lsFilter As String
'
'          lsFilter = "user_id =  "
'          If Not loUserColl.Remove lsFilter Then
'              MsgBox "Error!"
'          End If
'
'=========================================================================
Public Function Remove(ByVal vsFilter As String) As Boolean
On Error GoTo RemoveErr
Dim lsSQL As String

    If Trim(vsFilter) = "" Or IsNull(vsFilter) Then
        'If no filter is passed, COM-Gen 2000
        'can not create a valid WHERE clause to filter deletions.
        'Execution will then be halted to avoid accidental deletion.
        Exit Function
    End If


    lsSQL = "DELETE FROM user " _
         & "WHERE " & vsFilter


    If moConn.ExecActionSQL(lsSQL) Then
        Refresh
        Remove = True
    End If

    Exit Function

RemoveErr:
    RaiseError Err.Number, Err.Source & " (clsUser.Delete)", Err.Description

End Function