VERSION 5.00
Begin VB.UserControl SOM_Link 
   ClientHeight    =   10005
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   15090
   LockControls    =   -1  'True
   ScaleHeight     =   10005
   ScaleWidth      =   15090
   Begin VB.Frame fra_invalidate 
      Height          =   2385
      Left            =   1920
      TabIndex        =   37
      Top             =   4040
      Width           =   7425
      Begin VB.CommandButton btn_OK 
         Caption         =   "OK"
         Height          =   615
         Left            =   3000
         Style           =   1  'Graphical
         TabIndex        =   38
         Top             =   1410
         Width           =   1395
      End
      Begin VB.Label lbl_info 
         Alignment       =   2  'Center
         Caption         =   "#The data has been modified and became invalide. This screen will be closed."
         Height          =   825
         Left            =   240
         TabIndex        =   39
         Top             =   450
         Width           =   6975
      End
   End
   Begin VB.TextBox txt_CustID 
      ForeColor       =   &H000000FF&
      Height          =   315
      Left            =   9540
      TabIndex        =   15
      Tag             =   "Cust_ID®©Text"
      Text            =   "Cust_ID®©Text"
      Top             =   780
      Width           =   2205
   End
   Begin VB.TextBox txt_projID 
      ForeColor       =   &H000000FF&
      Height          =   315
      Left            =   7230
      TabIndex        =   14
      Tag             =   "Proj_ID®©Text"
      Text            =   "Proj_ID®©Text"
      Top             =   780
      Width           =   2205
   End
   Begin VB.TextBox txt_key 
      ForeColor       =   &H000000FF&
      Height          =   315
      Left            =   4920
      TabIndex        =   0
      Tag             =   "ProjCustLink_ID®©Text"
      Text            =   "ProjCustLink_ID®©Text"
      Top             =   780
      Width           =   2205
   End
   Begin VB.Frame fra_search 
      Height          =   7755
      Left            =   30
      TabIndex        =   21
      Top             =   2220
      Width           =   15015
      Begin VB.Frame fra_field_proj 
         Height          =   1320
         Left            =   7860
         TabIndex        =   32
         Top             =   180
         Width           =   2670
         Begin VB.OptionButton Opt_field_proj 
            Caption         =   "#Project number"
            Height          =   195
            Index           =   0
            Left            =   480
            TabIndex        =   36
            Tag             =   "Field®©True®©??"
            Top             =   150
            Value           =   -1  'True
            Width           =   2145
         End
         Begin VB.OptionButton Opt_field_proj 
            Caption         =   "#Town"
            Height          =   195
            Index           =   3
            Left            =   480
            TabIndex        =   35
            Tag             =   "Field®©False®©??"
            Top             =   1050
            Width           =   2145
         End
         Begin VB.OptionButton Opt_field_proj 
            Caption         =   "#Address"
            Height          =   195
            Index           =   2
            Left            =   480
            TabIndex        =   34
            Tag             =   "Field®©False®©??"
            Top             =   750
            Width           =   2145
         End
         Begin VB.OptionButton Opt_field_proj 
            Caption         =   "#Name"
            Height          =   195
            Index           =   1
            Left            =   480
            TabIndex        =   33
            Tag             =   "Field®©False®©??"
            Top             =   450
            Width           =   2145
         End
      End
      Begin VB.TextBox txt_SearchStr 
         Height          =   285
         Left            =   210
         MaxLength       =   80
         TabIndex        =   5
         Top             =   690
         Width           =   7515
      End
      Begin Project1.ArmGrid grd_result 
         Height          =   6120
         Left            =   90
         TabIndex        =   13
         Tag             =   "grd_result"
         Top             =   1545
         Width           =   14835
         _ExtentX        =   26167
         _ExtentY        =   10795
      End
      Begin VB.Frame fra_Like 
         Height          =   495
         Left            =   4350
         TabIndex        =   23
         Top             =   180
         Width           =   3375
         Begin VB.OptionButton opt_Like 
            Height          =   255
            Index           =   2
            Left            =   2280
            TabIndex        =   8
            Tag             =   "Like®©False®©??"
            Top             =   180
            Width           =   255
         End
         Begin VB.OptionButton opt_Like 
            Height          =   255
            Index           =   1
            Left            =   1200
            TabIndex        =   7
            Tag             =   "Like®©False®©??"
            Top             =   180
            Width           =   255
         End
         Begin VB.OptionButton opt_Like 
            Height          =   255
            Index           =   0
            Left            =   120
            TabIndex        =   6
            Tag             =   "Like®©True®©??"
            Top             =   180
            Value           =   -1  'True
            Width           =   195
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxxxxx"
            Height          =   195
            Index           =   2
            Left            =   600
            TabIndex        =   25
            Top             =   180
            Width           =   450
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxx"
            ForeColor       =   &H000000FF&
            Height          =   195
            Index           =   3
            Left            =   360
            TabIndex        =   24
            Top             =   180
            Width           =   225
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxx"
            ForeColor       =   &H000000FF&
            Height          =   195
            Index           =   1
            Left            =   1680
            TabIndex        =   28
            Top             =   180
            Width           =   225
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxx      xxx"
            Height          =   195
            Index           =   0
            Left            =   1440
            TabIndex        =   29
            Top             =   180
            Width           =   720
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxxxxx"
            Height          =   195
            Index           =   4
            Left            =   2520
            TabIndex        =   27
            Top             =   180
            Width           =   450
         End
         Begin VB.Label lbl_Like 
            AutoSize        =   -1  'True
            Caption         =   "xxx"
            ForeColor       =   &H000000FF&
            Height          =   195
            Index           =   5
            Left            =   2970
            TabIndex        =   26
            Top             =   180
            Width           =   225
         End
      End
      Begin Project1.ArmCombobox cbo_SearchCountry 
         Height          =   345
         Left            =   10710
         TabIndex        =   12
         Tag             =   "CT_code"
         Top             =   630
         Width           =   4185
         _ExtentX        =   7382
         _ExtentY        =   609
      End
      Begin VB.Frame fra_field_cust 
         Height          =   1125
         Left            =   7860
         TabIndex        =   30
         Top             =   180
         Width           =   2670
         Begin VB.OptionButton Opt_field_cust 
            Caption         =   "#Name"
            Height          =   195
            Index           =   0
            Left            =   480
            TabIndex        =   9
            Tag             =   "Field®©True®©??"
            Top             =   180
            Value           =   -1  'True
            Width           =   2145
         End
         Begin VB.OptionButton Opt_field_cust 
            Caption         =   "#Address"
            Height          =   195
            Index           =   1
            Left            =   480
            TabIndex        =   10
            Tag             =   "Field®©False®©??"
            Top             =   480
            Width           =   2145
         End
         Begin VB.OptionButton Opt_field_cust 
            Caption         =   "#Town"
            Height          =   195
            Index           =   2
            Left            =   480
            TabIndex        =   11
            Tag             =   "Field®©False®©??"
            Top             =   780
            Width           =   2145
         End
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Country"
         Height          =   225
         Index           =   2
         Left            =   10710
         TabIndex        =   31
         Tag             =   "lbl_SearchCountry"
         Top             =   420
         Width           =   4155
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Search string"
         Height          =   225
         Index           =   0
         Left            =   210
         TabIndex        =   22
         Tag             =   "lbl_SearchStr"
         Top             =   450
         Width           =   4095
      End
   End
   Begin VB.TextBox txt_project 
      Height          =   285
      Left            =   1470
      MaxLength       =   80
      TabIndex        =   1
      Tag             =   "Proj_Name®©Text"
      Text            =   "Proj_Name®©Text"
      Top             =   1110
      Width           =   7515
   End
   Begin VB.TextBox txt_projNum 
      Height          =   285
      Left            =   12960
      MaxLength       =   15
      TabIndex        =   2
      Tag             =   "ProjectNumber®©Text"
      Text            =   "ProjectNumber®©Text"
      Top             =   1110
      Width           =   2055
   End
   Begin VB.TextBox txt_CustName 
      Height          =   285
      Left            =   1470
      MaxLength       =   80
      TabIndex        =   3
      Tag             =   "Cust_name®©Text"
      Text            =   "Cust_name®©Text"
      Top             =   1500
      Width           =   7515
   End
   Begin Project1.ToolbarControl tbl_main 
      Height          =   690
      Left            =   0
      TabIndex        =   16
      Top             =   0
      Width           =   6555
      _ExtentX        =   11562
      _ExtentY        =   1217
   End
   Begin Project1.ArmCombobox cbo_RoleInProj 
      Height          =   345
      Left            =   11640
      TabIndex        =   4
      Tag             =   "Cust_Proj_role"
      Top             =   1470
      Width           =   3375
      _ExtentX        =   5953
      _ExtentY        =   609
   End
   Begin VB.Label lbl_label 
      Alignment       =   1  'Right Justify
      Caption         =   "#Role in project"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   225
      Index           =   40
      Left            =   9060
      TabIndex        =   20
      Tag             =   "lbl_RoleInProj"
      Top             =   1560
      Width           =   2535
   End
   Begin VB.Label lbl_label 
      Alignment       =   1  'Right Justify
      Caption         =   "#Project"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   225
      Index           =   36
      Left            =   30
      TabIndex        =   19
      Tag             =   "lbl_ProjectName"
      Top             =   1170
      Width           =   1395
   End
   Begin VB.Label lbl_label 
      Alignment       =   1  'Right Justify
      Caption         =   "#Project Number"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   225
      Index           =   1
      Left            =   9060
      TabIndex        =   18
      Tag             =   "lbl_ProjectNum"
      Top             =   1170
      Width           =   3855
   End
   Begin VB.Label lbl_label 
      Alignment       =   1  'Right Justify
      Caption         =   "#Customer"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   225
      Index           =   57
      Left            =   30
      TabIndex        =   17
      Tag             =   "lbl_CustomerName"
      Top             =   1560
      Width           =   1395
   End
End
Attribute VB_Name = "SOM_Link"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Public Event OnExit()
Public Event OnItemAdd(ByVal ae_CptType As eComponent, ByVal al_Key As Long, ByVal as_SrzFields As String)
Public Event OnItemUpdate(ByVal ae_CptType As eComponent, ByVal al_Key As Long, ByVal as_SrzFields As String)
Public Event OnItemDelete(ByVal ae_CptType As eComponent, ByVal al_Key As Long)
Public Event OnItemNext()
Public Event OnItemPrevious()


Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetLocaleInfo Lib "Kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Declare Function GetSystemDefaultLCID Lib "Kernel32" () As Long

Private Const SCREEN_NAME As String = "SOM_LINK"

Private Const SEP As String = "®©"
Private Const SEP1 As String = "®"
Private Const SEP2 As String = "©"


Private Const LOCALE_SDECIMAL = &HE ' Decimal separator
Private Const LOCALE_STHOUSAND = &HF ' Thousand separator
Private Const LOCALE_USER_DEFAULT = &H400

Private ms_DecimalSeparator  As String
Private ms_ThousandSeparator As String

Const C_ERRORRAISE As Long = 3000

Private Enum ArmErr
    DBCnxFailed = C_ERRORRAISE + 1             ' Unable to connect to the database
    CPTAlreadyInitialized = C_ERRORRAISE + 2   ' We try to initialize a component that is already initialized
    CPTNotInitialized = C_ERRORRAISE + 3       ' We try to use or free that is not initialized yet
    InvalidArgument = C_ERRORRAISE + 4
    PropertyNotSet = C_ERRORRAISE + 5
    SQLFailure = C_ERRORRAISE + 6               ' A SQL runtime error has occured : syntax wrong....
    SQLBadRowAffectedCount = C_ERRORRAISE + 7   ' A SQL request has not affected the expected rowcount (ex: one Update do nothing)
    SQLBadRowExpectedCount = C_ERRORRAISE + 8   ' A SQL request does not return the expected rowcount : select an item return nothing...
    DrivingError = C_ERRORRAISE + 9
    CompFncFailed = C_ERRORRAISE + 10           ' when component function fail
    GridLoadFailed = C_ERRORRAISE + 11          ' load function failed ... bad sql
End Enum

Private Enum ArmCusErr
    DuplicityDetected = C_ERRORRAISE + 2301                ' detected row with same unique id
End Enum


#If LIVE Then
    Private mo_Db                   As Object
#Else
    Private mo_Db                   As ArmDb
#End If

Private mb_Initialized              As Boolean
Private ms_LoginName                As String
Private ms_FullUserName             As String
Private ms_Language_Code            As String
Private ml_UCode                    As Long

Private mb_IsVisible As Boolean

Private mb_IsFullAccess As Boolean

Private me_Mode As eMode
Private me_OldMode As eMode
Private me_ProjStatus As eProjStatus

Private mb_FirstAddInit As Boolean

Private ml_Driver As eComponent
Private ml_LocalID As Long

Private ml_Cursor As Long

Private mb_Internal As Boolean

Private ms_LabelGridProj As String

Private ms_LabelGridCust As String


Private Type TConfigRecord
    RequestView As String
    RequestAdd As String
    RequestUpd As String
    RequestDel As String
    ListFieldsMandatory             As Variant
End Type

Private mt_ClassInfoLst() As TConfigRecord


#If LIVE Then
Public Property Set Db(ByRef aDb As Object)
#Else
Public Property Set Db(ByRef aDb As ArmDb)
#End If
    Set mo_Db = aDb
End Property


Public Property Get Initialized() As Boolean
    Initialized = mb_Initialized
End Property

Public Property Let LoginName(ByVal aLoginName As String)
    ms_LoginName = aLoginName
End Property

Public Property Let FullUserName(ByVal aFullUserName As String)
    ms_FullUserName = aFullUserName
End Property

Public Property Let Ucode(ByVal aUcode As Long)
    ml_UCode = aUcode
End Property


Public Property Let Language_Code(ByVal aLanguage_Code As String)
    ms_Language_Code = aLanguage_Code
End Property

Public Property Let A_Visible(ByVal aVisible As Boolean)
    UserControl.Extender.Visible = aVisible
    mb_IsVisible = aVisible
    If mb_IsVisible Then
        cbo_RoleInProj.SetFocus
    End If
End Property

Public Property Get IsVisible() As Boolean
    IsVisible = mb_IsVisible
End Property

Public Property Get A_Visible() As Boolean
    A_Visible = UserControl.Extender.Visible
End Property

Public Sub Move(ByVal aLeft As Single, ByVal aTop As Single, ByVal aWidth As Single, ByVal aHeight As Single)
    Call UserControl.Extender.Move(aLeft, aTop, aWidth, aHeight)
End Sub

Public Property Let DrivenBy(ByVal al_CptType As Long)
    ml_Driver = al_CptType
End Property

Public Sub Run(ByVal ae_ScrMode As eMode, ByVal al_Key As Long, ByVal as_SrzFields As String)
On Error GoTo ErrHandler
    
    Debug.Assert (mb_Initialized = True)
    
    Call LockScreen(True)
    
    fra_invalidate.Visible = False
    
    Select Case ae_ScrMode
        Case eMode.emView
            Call Item_ViewInit(al_Key)
        Case eMode.emAdd
            Call Item_AddInit(as_SrzFields)
        Case eMode.emUpdate
            Call Item_UpdateInit(al_Key)
        Case eMode.emDelete
            Call Item_DeleteInit(al_Key)
            
    End Select
    
    Call LockScreen(False)
    
    Exit Sub
ErrHandler:
    Call LockScreen(False)
    Call ErrorHandler("Run")
End Sub

Public Sub Notify(ByVal ae_CptType As eComponent, ByVal ae_Mode As eMode, ByVal al_Key As Long, ByVal as_SrzFields As String)
 On Error GoTo ErrHandler
       
    Dim ls_Desc As String
    Dim ll_ID As Long

    If Not mb_Initialized Then Exit Sub
    
    Debug.Assert (mb_Initialized = True)
        
    Select Case ae_CptType
    Case eCustomer
        Select Case ae_Mode
            
            Case eMode.emDelete
                
                If grd_result.Rows > 0 Then
                    Call DeleteLineToGrid(grd_result, "Cust_ID", "Cust_ID", al_Key)
                End If
            
            
                If Val(txt_CustID.Text) <> al_Key Then Exit Sub
                
                Call Invalidate_Process(MsgText(3056, ms_Language_Code, "#You have deleted the customer from an other place. This action will be terminated."))
            
            Case eMode.emUpdate
                
                If grd_result.Rows > 0 Then
                    If Not CheckIsInFilter(as_SrzFields) Then
                        Call DeleteLineToGrid(grd_result, "Cust_ID", "Cust_ID", al_Key)
                    Else
                        If Not UpdateLineToGrid(grd_result, "Cust_ID", al_Key, as_SrzFields, True) Then
                            Call AddLineToGrid(grd_result, "Cust_ID", as_SrzFields)
                        End If
                    End If
                End If
                
                If txt_CustID.Text = "" Then Exit Sub
                
                If Val(txt_CustID.Text) <> al_Key Then Exit Sub

                txt_CustName.Text = GetFieldValueFromSrz(as_SrzFields, "CUST_NAME")
                
            Case eMode.emAdd
                If grd_result.Rows = 0 Then Exit Sub
                
                If Not CheckIsInFilter(as_SrzFields) Then Exit Sub
                
                Call AddLineToGrid(grd_result, "Cust_ID", as_SrzFields)
                                
            
            Case eMode.emView
                If Val(txt_CustID.Text) <> al_Key Then Exit Sub
                
                txt_CustName.Text = GetFieldValueFromSrz(as_SrzFields, "CUST_NAME")
        
        End Select
        
    Case eProject
        Select Case ae_Mode
            Case eMode.emDelete
            
                If grd_result.Rows > 0 Then
                    Call DeleteLineToGrid(grd_result, "Proj_ID", "Proj_ID", al_Key)
                End If
            
                If Val(txt_projID.Text) <> al_Key Then Exit Sub
                                                    
                Call Invalidate_Process(MsgText(3057, ms_Language_Code, "#You have deleted the project from an other place. This action will be terminated."))
                
            Case eMode.emUpdate
            
                If grd_result.Rows > 0 Then
                    If Not CheckIsInFilter(as_SrzFields) Then
                        Call DeleteLineToGrid(grd_result, "Proj_ID", "Proj_ID", al_Key)
                    Else
                        If Not UpdateLineToGrid(grd_result, "Proj_ID", al_Key, as_SrzFields, True) Then
                            Call AddLineToGrid(grd_result, "Proj_ID", as_SrzFields)
                        End If
                    End If
                End If
                
                If txt_projID.Text = "" Then Exit Sub
                
                If Val(txt_projID.Text) <> al_Key Then Exit Sub
                
                txt_project.Text = GetFieldValueFromSrz(as_SrzFields, "PROJ_NAME")
                txt_projNum = GetFieldValueFromSrz(as_SrzFields, "PROJECTNUMBER")
                
                
            Case eMode.emAdd
                If grd_result.Rows = 0 Then Exit Sub
                
                If Not CheckIsInFilter(as_SrzFields) Then Exit Sub
                
                Call AddLineToGrid(grd_result, "Proj_ID", as_SrzFields)
                
            Case eMode.emView
                If Val(txt_projID.Text) <> al_Key Then Exit Sub
                
                txt_project.Text = GetFieldValueFromSrz(as_SrzFields, "PROJ_NAME")
                txt_projNum = GetFieldValueFromSrz(as_SrzFields, "PROJECTNUMBER")
        
        End Select
        
    Case eLink
       ' If txt_key.Text <> "" Then Exit Sub
        
        Select Case ae_Mode
            Case eMode.emDelete
                If Val(txt_key.Text) <> al_Key Then Exit Sub
                
                Call Invalidate_Process(MsgText(3058, ms_Language_Code, "#You have deleted the link between the project and the customer from an other place. This action will be terminated."))
                    
            Case eMode.emUpdate
                If Val(txt_key.Text) <> al_Key Then Exit Sub
                
                Select Case me_Mode
                    Case eMode.emView, eMode.emDelete
                        ll_ID = CLng(GetFieldValueFromSrz(as_SrzFields, "Cust_Proj_role"))
                        ls_Desc = GetFieldValueFromSrz(as_SrzFields, "Cust_Proj_role_desc")
                        
                        If Not cbo_RoleInProj.SearchItem(ll_ID, 0, , True) Then
                            Call cbo_RoleInProj.AddItem(Array(ll_ID, ls_Desc), True)
                        End If
                    Case Else
                        Call Invalidate_Process(MsgText(3062, ms_Language_Code, "#You have updated this link from an other place. This action will be terminated."))
                
                End Select
        End Select
    End Select
        
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Notify")
End Sub
Private Function CheckIsInFilter(ByVal as_SrzFields As String) As Boolean
On Error GoTo ErrHandler
    Dim ls_Param As String
    Dim ls_param1 As String
    
            
            If cbo_IsFilled(cbo_SearchCountry) Then
                If UCase(GetFieldValueFromSrz(as_SrzFields, "CT_code")) = UCase(cbo_SearchCountry.SelectedItem.Key) Then Exit Function
            End If
            
            If txt_SearchStr.Text <> "" Then
            
            Select Case ml_Driver
                Case eCustomer
                    If Opt_field_proj(0).value = True Then ls_Param = "ProjectNumber"
                    
                    If Opt_field_proj(1).value = True Then ls_Param = "Proj_name"
                    
                    If Opt_field_proj(2).value = True Then
                        ls_Param = "Proj_addr1"
                    End If
                                    
                    If Opt_field_proj(3).value = True Then ls_Param = "Proj_Town"
                
                Case eProject
                    If Opt_field_cust(0).value = True Then ls_Param = "Cust_name"
                    
                    If Opt_field_cust(1).value = True Then
                        ls_Param = "Cust_addr1"
                        ls_param1 = "Cust_addr2"
                    End If
                                    
                    If Opt_field_cust(2).value = True Then ls_Param = "Cust_Town"
                End Select
                
            
                If opt_Like(0).value = True Then
                    If UCase(GetFieldValueFromSrz(as_SrzFields, ls_Param)) Like UCase(txt_SearchStr.Text) & "*" Or _
                    (ls_param1 <> "" And UCase(GetFieldValueFromSrz(as_SrzFields, ls_param1)) Like UCase(txt_SearchStr.Text) & "*") Then
                        CheckIsInFilter = True
                        Exit Function
                    End If
                End If
                
                If opt_Like(1).value = True Then
                    If UCase(GetFieldValueFromSrz(as_SrzFields, ls_Param)) Like "*" & UCase(txt_SearchStr.Text) & "*" Or _
                    (ls_param1 <> "" And UCase(GetFieldValueFromSrz(as_SrzFields, ls_param1)) Like "*" & UCase(txt_SearchStr.Text) & "*") Then
                        CheckIsInFilter = True
                        Exit Function
                    End If
                End If
                
                If opt_Like(2).value = True Then
                    If UCase(GetFieldValueFromSrz(as_SrzFields, ls_Param)) Like "*" & UCase(txt_SearchStr.Text) Or _
                    (ls_param1 <> "" And UCase(GetFieldValueFromSrz(as_SrzFields, ls_param1)) Like "*" & UCase(txt_SearchStr.Text)) Then
                        CheckIsInFilter = True
                        Exit Function
                    End If
                End If
        
                
            End If

    

    Exit Function


ErrHandler:

    Call ErrorHandler("CheckIsInFilter")
End Function



Private Sub DeleteLineToGrid(ByVal ao_grid As ArmGrid, ByVal as_keyField As String, ByVal as_KeyFieldToSearch As String, ByVal al_Key As Long)
On Error GoTo ErrHandler
    
    Dim ll_Selectedkey As Long
    
    'To keep the initial seleted line in memory
    If ao_grid.SelectedCount > 0 Then ll_Selectedkey = ao_grid.SelectedLine(0, as_keyField)
    
    ao_grid.Columns(as_KeyFieldToSearch).Key = True
    
    While ao_grid.SearchKey(True, Array(al_Key))
        ao_grid.DeleteLine
    Wend
    
    ao_grid.Columns(as_KeyFieldToSearch).Key = False
    
    'Select the initial selected line
    If ll_Selectedkey <> 0 Then
        ao_grid.Columns(as_keyField).Key = True
        ao_grid.SearchKey True, Array(ll_Selectedkey)
        ao_grid.Columns(as_keyField).Key = False
    Else
        ao_grid.DeselectRow
    End If
    
    If ao_grid.Rows = 0 Then Call OnFilterChange
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("DeleteLineToGrid")
End Sub

Private Function UpdateLineToGrid(ByVal ao_grid As ArmGrid, as_keyField As String, ByVal al_Key As Long, ByVal as_SrzFields As String, ByVal ab_OneRowToUpdate As Boolean) As Boolean
On Error GoTo ErrHandler
    
    Dim lv_SrzFields As Variant
    Dim lv_Values
    Dim ll_count As Long
    Dim ll_Nb As Long, ll_Nb2 As Long, ll_Idx As Long
    
    Dim ls_ColNames As String
    
    ll_Nb = ao_grid.Rows - 1
    For ll_count = 0 To ll_Nb
        
        'si cust_id = alkey mettre à jour la ligne de la grille
        If ao_grid.Data(ll_count, as_keyField) = al_Key Then
            lv_SrzFields = Split(as_SrzFields, SEP)
            ll_Nb2 = UBound(lv_SrzFields) - 1
            
            'build the list of columns of the grid
            ls_ColNames = SEP2
            Dim i As Long
            For i = 0 To ao_grid.Cols - 1
                ls_ColNames = ls_ColNames & UCase(ao_grid.Columns(i).FieldName) & SEP2
            Next i
            
            For ll_Idx = 0 To ll_Nb2
                lv_Values = Split(lv_SrzFields(ll_Idx), SEP1)
                 If InStr(1, UCase(ls_ColNames), SEP2 & UCase(lv_Values(0)) & SEP2) Then
                    ao_grid.Data(ll_count, lv_Values(0)) = lv_Values(1)
                    UpdateLineToGrid = True
                End If
            Next

            'there is only on link per proj/cust
            If ab_OneRowToUpdate Then Exit For
        
        End If
    Next
    
    
    
    Exit Function
ErrHandler:
    Call ErrorHandler("UpdateLineToGrid")
End Function

Public Function Load_A_COM() As Boolean

On Error GoTo ErrHandler

    If mb_Initialized Then Exit Function
    
    Call LockScreen(True)
    
'to handle Decimal with the local settings
    Dim sBuffer As String
    Dim nBufferLen As Long
    Dim LCID As Long

    LCID = GetSystemDefaultLCID

    ms_DecimalSeparator = Format(0, ".")

    nBufferLen = 255
    sBuffer = String$(nBufferLen, vbNullChar)
    nBufferLen = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sBuffer, nBufferLen)
    If nBufferLen > 0 Then
        ms_ThousandSeparator = Left$(sBuffer, nBufferLen - 1)
    End If

    mb_IsFullAccess = SOM_lib.IsFullAccess
    ml_LocalID = GetLCIDFromCodePage(Get_Code_Page)
    
    Call Components_Settings
    
    Call Load_ClassInfo
    
    Call InitComponents
    
    Call LoadAllLabels
    
    LockScreen (False)
    
    mb_Initialized = True
    
    Load_A_COM = True
    
    Exit Function

ErrHandler:
    LockScreen (False)
    Call ErrorHandler("Load_A_Com")
End Function

Private Sub Components_Settings()
On Error GoTo ErrHandler
    
    Call Component_SetUp(tbl_main, "", 20)
    
    Call Component_SetUp(txt_CustID, "Cust_ID" & SEP & "Text", 19)
    Call Component_SetUp(txt_projID, "Proj_ID" & SEP & "Text", 18)
    
    Call Component_SetUp(grd_result, "grd_result", 17)
    
    
    Call Component_SetUp(lbl_label(2), "lbl_SearchCountry")
    Call Component_SetUp(cbo_SearchCountry, "CT_code" & SEP & "CT_desc", 16)
    
    Call Component_SetUp(fra_field_proj, "")
    Call Component_SetUp(Opt_field_proj(3), "FieldP3" & SEP & "False" & SEP & "#Town", 15)
    Call Component_SetUp(Opt_field_proj(2), "FieldP2" & SEP & "False" & SEP & "#Address", 14)
    Call Component_SetUp(Opt_field_proj(1), "FieldP1" & SEP & "False" & SEP & "#Name", 13)
    Call Component_SetUp(Opt_field_proj(0), "FieldP0" & SEP & "True" & SEP & "#Project number", 12)
    
    Call Component_SetUp(fra_field_cust, "")
    Call Component_SetUp(Opt_field_cust(2), "FieldC2" & SEP & "False" & SEP & "#Town", 11)
    Call Component_SetUp(Opt_field_cust(1), "FieldC1" & SEP & "False" & SEP & "#Address", 10)
    Call Component_SetUp(Opt_field_cust(0), "FieldC0" & SEP & "True" & SEP & "#Name", 9)
    
    Call Component_SetUp(fra_Like, "")
    Call Component_SetUp(lbl_Like(4), "")
    Call Component_SetUp(lbl_Like(5), "")
    Call Component_SetUp(opt_Like(2), "LikeContain" & SEP & "False" & SEP & "#Contain", 8)
    Call Component_SetUp(lbl_Like(1), "")
    Call Component_SetUp(lbl_Like(0), "")
    Call Component_SetUp(opt_Like(1), "LikeEnd" & SEP & "False" & SEP & "#End", 7)
    Call Component_SetUp(lbl_Like(2), "")
    Call Component_SetUp(lbl_Like(3), "")
    Call Component_SetUp(opt_Like(0), "LikeBegin" & SEP & "True" & SEP & "#Begin", 6)
    
    Call Component_SetUp(fra_search, "")
    Call Component_SetUp(lbl_label(0), "lbl_SearchStr")
    Call Component_SetUp(txt_SearchStr, "", 5)
    
    Call Component_SetUp(lbl_label(40), "lbl_RoleInProj")
    Call Component_SetUp(cbo_RoleInProj, "Cust_Proj_Role" & SEP & "Cust_Proj_Role_desc", 4)
    
    Call Component_SetUp(lbl_label(1), "lbl_ProjectNum")
    Call Component_SetUp(txt_projNum, "ProjectNumber" & SEP & "Text", 3)
    
    Call Component_SetUp(lbl_label(57), "lbl_CustomerName")
    Call Component_SetUp(txt_CustName, "Cust_name" & SEP & "Text", 2)
    
    Call Component_SetUp(lbl_label(36), "lbl_ProjectName")
    Call Component_SetUp(txt_project, "Proj_Name" & SEP & "Text", 1)
    
    Call Component_SetUp(txt_key, "ProjCustLink_ID" & SEP & "Text", 0)

    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Components_Settings")
End Sub


Private Sub Component_SetUp(ByVal ao_cpt As Object, ByVal as_Tag As String, Optional ai_TabIndex As Integer)

On Error GoTo ErrHandler
    
    ao_cpt.Tag = as_Tag
    ao_cpt.TabIndex = ai_TabIndex
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Component_SetUp")
End Sub

Private Sub HandleToolbar(ByVal atlb_Toolbar As Object, ByVal as_face As String, ByVal ab_ButtonsVisible As Boolean)
On Error GoTo ErrHandler

    atlb_Toolbar.Visible = False
    atlb_Toolbar.DisplayFace (as_face)
   
    If Not ab_ButtonsVisible Then
        atlb_Toolbar.Redraw = False
        atlb_Toolbar.ButtonVisible("C") = False
        atlb_Toolbar.Redraw = True
    End If
    
    atlb_Toolbar.Visible = True
    
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("HandleToolbar")
    
End Sub

Private Sub InitComponents()

On Error GoTo ErrHandler
    
    Const CL_REQUEST_TB As String = "SELECT Toolbar_Info FROM Toolbars_Users WHERE User_Code=$user_id$"

    Dim ls_Toolbar_Info As String
    Dim lCursTB As Long
    
    Dim ll_Charset As Long
    
    lCursTB = OpenSQLSafe(mo_Db, Replace(CL_REQUEST_TB, "$user_id$", 0))
    
    ls_Toolbar_Info = mo_Db.GetFields(lCursTB, "toolbar_info")
    Call mo_Db.Close(lCursTB)
    
    Call tbl_main.Load_A_COM
    tbl_main.Language = "E" 'ms_Language_Code
    
    Call tbl_main.SetToolbarInfoStringParameters(ls_Toolbar_Info, "053")
    Call HandleToolbar(tbl_main, "0", mb_IsFullAccess)
    
    ll_Charset = GetCharSetFromCodePage(Get_Code_Page)
    
    Dim lo_Control As Control

    For Each lo_Control In UserControl.Controls
        Select Case UCase(TypeName(lo_Control))
            Case "ARMGRID"
                Set lo_Control.ArmDb = mo_Db
                lo_Control.Font.Name = "Arial"
                lo_Control.Font.Charset = ll_Charset
                lo_Control.LocalID = ml_LocalID
                lo_Control.codepage = Get_Code_Page
                lo_Control.Load_A_COM
                
            Case "ARMCOMBOBOX"
                Set lo_Control.ArmDb = mo_Db
                lo_Control.Font.Name = "Arial"
                lo_Control.Font.Charset = ll_Charset
                lo_Control.Load_A_COM
                
            Case "CHECKBOX", "LABEL", "COMMANDBUTTON", "FRAME", "TEXTBOX"
                lo_Control.Font.Name = "Arial"
                lo_Control.Font.Charset = ll_Charset
                
                Select Case UCase(lo_Control.Name)
                    Case "TXT_KEY"
                        lo_Control.Visible = False
                End Select
            
            Case "OPTIONBUTTON"
                lo_Control.Font.Name = "Arial"
                lo_Control.Font.Charset = ll_Charset
                lo_Control.value = False
        
        End Select
    Next
    
    
    cbo_RoleInProj.Request = "exec SOM_CustomerType_cbo '" & ms_Language_Code & "'"
    cbo_SearchCountry.Request = "exec SOM_Countries_cbo '" & ms_Language_Code & "'"
    cbo_SearchCountry.FirstBlankItem = True
    
    txt_key.Visible = False
    txt_CustID.Visible = False
    txt_projID.Visible = False
    
    btn_OK.Caption = ""
    btn_OK.Picture = LoadResPicture(129, vbResIcon)
    
    
    Call fra_invalidate.Move(3833, 3870, 7425, 2265)
    fra_invalidate.Visible = False
    
    Exit Sub
    
ErrHandler:
    mo_Db.Close (lCursTB)
    Call ErrorHandler("InitComponents")
End Sub
Private Function Get_Code_Page() As Long
On Error GoTo ErrHandler

    Const Request As String = "SELECT Code_Page FROM Language WHERE Language_code = '$Language_code$'"
    
    Static lb_HasRun As Boolean
    Static ls_code_page As Long

    
    
    Dim ls_req As String
    Dim ll_curs As Long
    If Not lb_HasRun Then
        ls_req = Replace(Request, "$Language_code$", ms_Language_Code, , , vbTextCompare)
        
        ll_curs = OpenSQLSafe(mo_Db, ls_req)
        
        If mo_Db.RowCount(ll_curs) = 0 Then
             Err.Raise C_ERRORRAISE + 503, , "Enable to get the code page for the language " & ms_Language_Code & " - " & Join(mo_Db.SQLErrorMessages, vbCrLf)
        End If
        
        ls_code_page = mo_Db.GetFields(ll_curs, "Code_Page")
        
        mo_Db.Close (ll_curs)
    End If
    
    Get_Code_Page = ls_code_page
    
    
    Exit Function

ErrHandler:
    mo_Db.Close (ll_curs)
    Call ErrorHandler("Get_Code_Page")
End Function


Private Sub LockScreen(ByVal ab_lock As Boolean)

    Dim ll_errNumber As Long, ls_ErrSrc As String, ls_ErrDesc As String
    ll_errNumber = Err.Number
    ls_ErrSrc = Err.Source
    ls_ErrDesc = Err.Description

On Error GoTo ErrHandler
    Static ll_count As Long
    Static ll_Mousepointer As Long
    Static lb_Locked As Boolean
      
      
    ll_count = ll_count + IIf(ab_lock, 1, -1)
    Debug.Assert (ll_count >= 0)
    
    ' First lock
    If Not lb_Locked And ab_lock Then
        ll_Mousepointer = Screen.MousePointer
        Screen.MousePointer = vbHourglass
        LockWindowUpdate UserControl.hwnd
        lb_Locked = True
    End If
    
    ' Unlock
    If ll_count = 0 Then
        DoEvents ' Flush events
        LockWindowUpdate 0
        UserControl.Refresh ' Repaint immediately
        Screen.MousePointer = ll_Mousepointer
        lb_Locked = False
    End If
    
    Err.Number = ll_errNumber
    Err.Source = ls_ErrSrc
    Err.Description = ls_ErrDesc
    
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("LockScreen")
End Sub

Public Function Unload_A_COM() As Boolean

On Error GoTo ErrHandler

    If Not mb_Initialized Then Exit Function
    
    Dim lo_Control As Control
    
    For Each lo_Control In UserControl.Controls
        Select Case UCase(TypeName(lo_Control))
            Case "TOOLBARCONTROL", "ARMCOMBOBOX", "ARMGRID"
                lo_Control.Unload_A_COM
        End Select
    Next
    
           
    Set mo_Db = Nothing
    
    mb_Initialized = False
    
    Unload_A_COM = True
    
    Exit Function
    
ErrHandler:
    Call ErrorHandler("Unload_A_Com")
End Function

Private Function BeginTran(as_Tran As String) As Boolean

On Error GoTo ErrHandler
    BeginTran = False
    ExecuteSQLSafe mo_Db, "BEGIN TRANSACTION " & as_Tran

    BeginTran = True
    Exit Function
    
ErrHandler:
    mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".BeginTran, your application will be close. Please contact your IT support"
    End
End Function
Private Function CommitTran(as_Tran As String) As Boolean

On Error GoTo ErrHandler
    CommitTran = False
    ExecuteSQLSafe mo_Db, "COMMIT TRANSACTION " & as_Tran

    CommitTran = True
    Exit Function
    
ErrHandler:
    mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".CommitTran, your application will be close. Please contact your IT support"
    End

End Function

Private Function RollbackTran(as_Tran As String) As Boolean
    
    Dim ll_errNumber As Long, ls_ErrSrc As String, ls_ErrDesc As String
    ll_errNumber = Err.Number
    ls_ErrSrc = Err.Source
    ls_ErrDesc = Err.Description

On Error GoTo ErrHandler
    RollbackTran = False
    
    ExecuteSQLSafe mo_Db, "ROLLBACK TRANSACTION " & as_Tran


    Err.Number = ll_errNumber
    Err.Source = ls_ErrSrc
    Err.Description = ls_ErrDesc

    RollbackTran = True
    Exit Function
    
ErrHandler:
    mo_Db.Disconnect
    Set mo_Db = Nothing
    MsgBox "A Fatal error occured in " & SCREEN_NAME & ".RollbackTran, your application will be close. Please contact your IT support"
    End
End Function
Private Sub ErrorHandler(ByVal as_Fct As String)
    Err.Raise Err.Number, UserControl.Name & "." & UserControl.Ambient.DisplayName & "::" & as_Fct & SEP1 & Err.Source, Err.Description
End Sub

Function MsgText(ByVal aID As Long, ByVal aLang As String, ByVal aDefault As String, Optional ByVal aInfo As Variant) As String

On Error GoTo ErrHandler

Const DB_REQ As String = "SELECT message_text FROM error_message WHERE msgid = $id$ AND Language_code = '$lang$'"

    MsgText = ""
    
    Dim lRequest As String
    lRequest = Replace(DB_REQ, "$id$", aID)
    lRequest = Replace(lRequest, "$lang$", aLang)
    Dim lData As Long
    
    lData = OpenSQLSafe(mo_Db, lRequest)
    
    Dim lBuffer As String
    lBuffer = mo_Db.GetFields(lData, "message_text")
    mo_Db.Close (lData)
    If lBuffer = "" Then lBuffer = aDefault
    
    Dim li_Idx As Integer
    If Not IsMissing(aInfo) Then
        For li_Idx = 0 To UBound(aInfo)
            lBuffer = Replace(lBuffer, aInfo(li_Idx, 0), aInfo(li_Idx, 1), , , vbTextCompare)
        Next li_Idx
    End If
    
    
    MsgText = lBuffer
    Exit Function
ErrHandler:
    mo_Db.Close (lData)
    Call ErrorHandler("MsgText")
   
End Function
' Return the result of a SQL request
' Convert SQL runtime errors and process errors to VB Error
#If LIVE = 1 Then
Private Function OpenSQLSafe(ByVal ao_Db As Object, ByVal as_Request As String, Optional ByVal al_RowExpectedCount = -1) As Long
#Else
Private Function OpenSQLSafe(ByVal ao_Db As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowExpectedCount = -1) As Long
#End If

On Error GoTo ErrHandler

    Dim lc_Data As Long
    lc_Data = ao_Db.OpenSQL(as_Request)
    
    If lc_Data = 0 Then
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_Db.SQLErrorCodes, SEP2) & SEP1 & Join(ao_Db.SQLErrorMessages, SEP2)
    End If
'    Debug.Print 1 / 0
    If al_RowExpectedCount <> -1 Then
        ' Then check the rowcount
        If ao_Db.RowCount(lc_Data) <> al_RowExpectedCount Then
            Err.Raise ArmErr.SQLBadRowExpectedCount, "SQL : " & as_Request, al_RowExpectedCount & "<>" & ao_Db.RowCount(lc_Data)
        End If
    End If

    OpenSQLSafe = lc_Data

    Exit Function

ErrHandler:

    Call ErrorHandler("OpenSQLSafe")

End Function
' Execute a SQL request returning no data
' Convert SQL runtime errors and process errors to VB Error
' Params:
' ao_Db (Object)
' as_Request (String)
' al_RowAffectedCount (String)
#If LIVE = 1 Then
Private Sub ExecuteSQLSafe(ByVal ao_Db As Object, ByVal as_Request As String, Optional ByVal al_RowAffectedCount = -1, Optional ab_DuplicityCheck As Boolean = False)
#Else
Private Sub ExecuteSQLSafe(ByVal ao_Db As ARMSYSCOMLib.ArmDb, ByVal as_Request As String, Optional ByVal al_RowAffectedCount = -1, Optional ab_DuplicityCheck As Boolean = False)
#End If
On Error GoTo ErrHandler

    ' First execute the request
    If Not ao_Db.ExecuteSQL(as_Request) Then
        Err.Raise ArmErr.SQLFailure, "SQL : " & as_Request, Join(ao_Db.SQLErrorCodes, SEP2) & SEP1 & Join(ao_Db.SQLErrorMessages, SEP2)
    End If

    If al_RowAffectedCount <> -1 Then
        ' Then check the rowcount
        If ao_Db.SQLRowsAffected <> al_RowAffectedCount Then
            
            If ab_DuplicityCheck Then
                Err.Raise ArmCusErr.DuplicityDetected, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_Db.SQLRowsAffected
            Else
                Err.Raise ArmErr.SQLBadRowAffectedCount, "SQL : " & as_Request, al_RowAffectedCount & "<>" & ao_Db.SQLRowsAffected
            End If
        End If
    End If

    Exit Sub

ErrHandler:
    Call ErrorHandler("ExecuteSQLSafe")
End Sub
' logs message to database
Private Sub LogMessage(ByVal as_logMsg As String, Optional ByVal as_logType As String = "I", Optional ab_throwException As Boolean = True)
On Error GoTo ErrHandler
Const InsertReq As String = "EXEC A_log_ins $UCODE$, '$LOGTYPE$', '$MSG$', '$APP$'"
    Dim ls_req As String
    Dim ll_cursor As Long
    
    ls_req = Replace(InsertReq, "$UCODE$", CStr(ml_UCode))
    ls_req = Replace(ls_req, "$APP$", Left(Trim(SQLStr(SCREEN_NAME & " " & App.Title & " " & App.Major & "." & App.Minor & "." & App.Revision)), 50))
    ls_req = Replace(ls_req, "$MSG$", right(Trim(SQLStr(as_logMsg)), 4000))
    ls_req = Replace(ls_req, "$LOGTYPE$", SQLStr(as_logType), 1)
    
    Call ExecuteSQLSafe(mo_Db, ls_req)
    
    Exit Sub
ErrHandler:
    If ab_throwException Then Call ErrorHandler(Extender.Name & ".LogMessage - " & Err.Number & ": " & Err.Description)
End Sub

Private Function SQLStr(ByVal as_str As String) As String
    SQLStr = Replace(as_str, "'", "''")
End Function

Public Function SqlDate(ByVal as_Date As String) As String
  SqlDate = "'" & Format$(as_Date, "YYYY-MM-DD") & "'"
End Function

Public Function SqlTime(ByVal ad_Seconds As Double) As String
Dim ll_Hours As Long
Dim ll_Minutes As Long
Dim ll_Seconds As Long
  
  ll_Seconds = ad_Seconds
  ll_Hours = ll_Seconds \ 3600
  ll_Minutes = (ll_Seconds - (ll_Hours * 3600)) \ 60
  ll_Seconds = ll_Seconds Mod 60
  SqlTime = "'" & Format(ll_Hours, "00") & ":" & Format(ll_Minutes, "00") & ":" & Format(ll_Seconds, "00") & "'"
End Function




Private Sub LoadAllLabels()
    
On Error GoTo ErrHandler
    
    Call LoadLabels(UserControl.Controls, Me, SCREEN_NAME, ms_Language_Code)
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("LoadAllLabels")
 
End Sub
Private Function HasContainer(ByRef aControl As Control, ByRef aContainer As Object) As Boolean
    
    HasContainer = False
    Dim lControl As Control
    
    Set lControl = aControl
    While Not (lControl Is Nothing)
        On Error GoTo NotFound
        If lControl.Container Is aContainer Then
            Set lControl = Nothing
            HasContainer = True
            Exit Function
        End If
        Set lControl = lControl.Container
    Wend

NotFound:
    Set lControl = Nothing
    HasContainer = False
End Function

Public Sub LoadLabels(ByRef aControls As Variant, ByRef av_Container As Object, ByVal as_ScreenName As String, ByVal as_Language As String)

On Error GoTo ErrHandler
    Dim lIdx As Long, lCount As Long, lLabels As Long, lb_Apply As Boolean, lv_Iter As Object, ls_Buffer As String
    Dim lControl As Control, ll_Idx2 As Long, ll_Count2 As Long, ll_IdxGrd As Long
    
    lLabels = OpenSQLSafe(mo_Db, "exec Screen_Csts '" & as_ScreenName & "','" & as_Language & "'")
    lCount = aControls.Count - 1
    
    For lIdx = 0 To lCount
        Set lControl = aControls.Item(lIdx)
        
        If Not av_Container Is Nothing Then
            lb_Apply = HasContainer(lControl, av_Container)
        Else
            lb_Apply = True
        End If
        If lb_Apply Then
            Select Case UCase(TypeName(lControl))
                Case "LABEL", "FRAME", "COMMANDBUTTON", "CHECKBOX"
                    If lControl.Tag <> "" Then
                        If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                            lControl.Caption = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                    End If
                Case "TEXTBOX"
                    If lControl.Tag <> "" Then
                        If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                            lControl.Text = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                    End If
                Case "TABSTRIP"
                    
                    If lControl.Tag <> "" Then
                        For Each lv_Iter In lControl.Tabs
                            If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag & "_" & lv_Iter.Tag, , 1) >= 0 Then
                                lv_Iter.Caption = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                            End If
                        Next
                    End If
                
                Case "OPTIONBUTTON"
                    If lControl.Tag <> "" Then
                        ls_Buffer = Split(lControl.Tag, SEP, , vbTextCompare)(0)
                        On Error Resume Next
                        ls_Buffer = ls_Buffer
                        On Error GoTo 0
                        If mo_Db.Find(lLabels, "FIELD_NAME", ls_Buffer, , 1) >= 0 Then
                            lControl.Caption = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                    End If
                Case "ARMGRID"
                    If lControl.Tag <> "" Then
                        Dim ls_Tag As String
                        
                        ls_Tag = lControl.Tag & "_proj"
                        
                        If mo_Db.Find(lLabels, "FIELD_NAME", ls_Tag, , 1) >= 0 Then
                            ms_LabelGridProj = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                        
                        ls_Tag = lControl.Tag & "_cust"
                        
                        If mo_Db.Find(lLabels, "FIELD_NAME", ls_Tag, , 1) >= 0 Then
                            ms_LabelGridCust = mo_Db.GetFields(lLabels, "LOCAL_TEXT")
                        End If
                        
                            'Call lControl.LoadConstants(ptStatic, mo_Db.GetFields(lLabels, "LOCAL_TEXT"), ctColumns)
                        'End If
                    End If
                    
                Case "FRAME", "MSFLEXGRID", "TOOLBARCONTROL", "COMMANDBUTTON", "ARMCHECKVIEW", "ARMCOMBOBOX", "A_CALOCX", "ARMTREEVIEW", "LISTBOX", "PICTUREBOX"
                   ' NOTHING
                
            End Select
        End If
        Set lControl = Nothing
    Next
    
CleanUp:
    mo_Db.Close (lLabels)
    Exit Sub
            
ErrHandler:
    mo_Db.Close (lLabels)
    If Not lControl Is Nothing Then Set lControl = Nothing
    Call ErrorHandler("LoadLabels")
            
End Sub

Private Sub btn_OK_Click()
On Error GoTo ErrHandler

    Call Item_Exit

    Exit Sub
    
ErrHandler:
    
    Select Case Err.Number
    Case Else
        Call LogMessage("btn_OK_Click: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E")
        Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
        End
    End Select

End Sub

Private Sub cbo_RoleInProj_ComboItemSelected()
On Error GoTo ErrHandler

    cbo_RoleInProj.AutoDroppedWidth
    
    Exit Sub
    
ErrHandler:
    Select Case Err.Number
    Case Else
        Call LogMessage("cbo_RoleInProj_ComboItemSelected: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E")
        Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
        End
    End Select
End Sub

Private Sub cbo_SearchCountry_ComboDropDown()
On Error GoTo ErrHandler

    cbo_SearchCountry.AutoDroppedWidth
    
    Exit Sub
    
ErrHandler:
    
    Select Case Err.Number
    Case Else
        Call LogMessage("cbo_SearchCountry_ComboDropDown: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E")
        Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
        End
    End Select

End Sub

Private Sub cbo_SearchCountry_ComboItemSelected()
On Error GoTo ErrHandler
    
    If mb_Internal Then Exit Sub
    
    Call LockScreen(True)
    
    Call OnFilterChange
    
    Call LockScreen(False)
    Exit Sub
    
ErrHandler:

    Call LockScreen(False)
    
    Select Case Err.Number
    Case Else
        Call LogMessage("cbo_projStatus_ComboItemSelected: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
        Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
        End
    End Select
    
End Sub

Private Sub grd_result_DblClick()
On Error GoTo ErrHandler

    Select Case ml_Driver
    Case eProject
        txt_CustName.Text = grd_result.SelectedLine(0, "Cust_name")
        txt_CustID.Text = grd_result.SelectedLine(0, "Cust_ID")
        
    Case eCustomer
        txt_project.Text = grd_result.SelectedLine(0, "Proj_name")
        txt_projID.Text = grd_result.SelectedLine(0, "Proj_ID")
    End Select
    
    Exit Sub
ErrHandler:

    Select Case Err.Number
        Case Else
            Call LogMessage("grd_result_DblClick: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
    End Select
End Sub

Private Sub Opt_field_cust_Click(Index As Integer)
On Error GoTo ErrHandler
    
    Call LockScreen(True)
    
    Call OnFilterChange
    
    Call LockScreen(False)
    
    Exit Sub
    
ErrHandler:
    Call LockScreen(False)
    
    Select Case Err.Number
        Case Else
            Call LogMessage("Opt_field_cust_Click: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
    End Select
End Sub

Private Sub Opt_field_proj_Click(Index As Integer)
On Error GoTo ErrHandler

    Call LockScreen(True)
    
    Call OnFilterChange
    
    Call LockScreen(False)
    
    Exit Sub
    
ErrHandler:
    Call LockScreen(False)
    
    Select Case Err.Number
        Case Else
            Call LogMessage("Opt_field_proj_Click: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
    End Select
End Sub

Private Sub opt_Like_Click(Index As Integer)
On Error GoTo ErrHandler

    Call LockScreen(True)
    
    Call OnFilterChange
    
    Call LockScreen(False)
    
    Exit Sub
ErrHandler:

    Call LockScreen(False)
    
    Select Case Err.Number
        Case Else
            Call LogMessage("opt_Like_Click: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
    End Select
End Sub

Private Sub tbl_main_action(ByVal as_Role As String, as_Language As String)
On Error GoTo ErrHandler
    Static DoCheck As Boolean
    
    If DoCheck = True Then Exit Sub
    DoCheck = True
    
    Dim ll_Key As Long
    ll_Key = CLng(txt_key.Text)
    
    Call LockScreen(True)
    tbl_main.Enabled = False

    Select Case as_Role
    
        Case "H" ' validate delete
            Call Item_Add
            
        Case "F" 'Refresh Add
            Call Item_AddReinit
            
        Case "C" 'delete button
            Call Item_DeleteInit(CLng(txt_key.Text))

        Case "D" ' validate delete
            Call Item_Delete
    
        Case "I" 'Refresh update
            Call Item_UpdateReinit(ml_Cursor)
        
        Case "B" 'goto Update screen
            Call Item_UpdateInit(ll_Key)
            
        Case "U" 'Validate update
            Call Item_Update
                        
        Case "J" 'Goto previous item
            RaiseEvent OnItemPrevious
        
        Case "K" 'Goto next item
            RaiseEvent OnItemNext
            
        Case "T"
            Call Item_Exit
            
    End Select
    
    tbl_main.Enabled = True
    Call LockScreen(False)
    
    DoCheck = False
    
    Exit Sub
ErrHandler:

    DoCheck = False
    
    tbl_main.Enabled = True
    Call LockScreen(False)
    
    Select Case Err.Number
    
        Case 3007
            MsgBox MsgText(3054, ms_Language_Code, "#This data has been updated by another user. Please reload the data and try again."), vbInformation
        
        Case 3008
            MsgBox MsgText(2138, ms_Language_Code, "#The record you try to open has been deleted by an other user. Please refresh the grid."), vbInformation
            Call Item_Exit
            
        Case Else
            Call LogMessage("tbl_main_action: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
        End Select
End Sub


Private Sub Load_ClassInfo()
On Error GoTo ErrHandler

    ReDim mt_ClassInfoLst(0)
    
        With mt_ClassInfoLst(0)
            .RequestView = "exec SOM_ProjCustLink_sel $ProjCustLink_ID$ , '$Language_code$'"

            .RequestAdd = "exec SOM_ProjCustLink_ins $ProjCustLink_ID$, $Proj_ID$, $Cust_ID$,'E', $Cust_Proj_role$, $Z_Creator$" & SEP & _
            "exec SOM_Project_LastUpd_Upd $Proj_ID$,$Z_last_upd_user$" & SEP & _
            "exec SOM_Customer_LastUpd_Upd $Cust_ID$,$Z_last_upd_user$"

            .RequestUpd = "exec SOM_ProjCustLink_upd $ProjCustLink_ID$, $Cust_Proj_Role$, $Z_last_upd_user$, $iConcurrency$" & SEP & _
            "exec SOM_Project_LastUpd_Upd $Proj_ID$,$Z_last_upd_user$" & SEP & _
            "exec SOM_Customer_LastUpd_Upd $Cust_ID$,$Z_last_upd_user$"

            .RequestDel = "exec SOM_ProjCustLink_Del3 $ProjCustLink_ID$, $Z_last_upd_user$, $iConcurrency$" & SEP & _
                                "exec SOM_Quote_Del3 $ProjCustLink_ID$, $Z_last_upd_user$" & SEP & _
            "exec SOM_Project_LastUpd_Upd $Proj_ID$,$Z_last_upd_user$" & SEP & _
            "exec SOM_Customer_LastUpd_Upd $Cust_ID$,$Z_last_upd_user$"


           
            ReDim .ListFieldsMandatory(2, 1)
            Set .ListFieldsMandatory(0, 0) = txt_project
             .ListFieldsMandatory(0, 1) = 36
            Set .ListFieldsMandatory(1, 0) = txt_CustName
             .ListFieldsMandatory(1, 1) = 57
            Set .ListFieldsMandatory(2, 0) = cbo_RoleInProj
             .ListFieldsMandatory(2, 1) = 40

        End With

    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Load_ClassInfo")
    
End Sub

Private Sub Item_ViewInit(ByVal al_Key As Long)
On Error GoTo ErrHandler

    Call SetMode(eMode.emView)
    
    'visuel
    Call ClearForm(UserControl.Controls, Me)
    Call EnableFrame(UserControl.Controls, Me, False)
    Call HandleToolbar(tbl_main, 0, mb_IsFullAccess)
    
    txt_key.Text = al_Key
    
    Call OpenCursor(mt_ClassInfoLst(0).RequestView)
    
    Call LoadDataToForm(ml_Cursor, UserControl.Controls, Me)
    
    'keep project status in memory and hide icons
    me_ProjStatus = mo_Db.GetFields(ml_Cursor, "PS_code")
    Hide_Icons
    
    fra_search.Visible = False
    
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Item_ViewInit")
End Sub

Private Sub LoadDataToForm(ByVal ac_Cursor As Long, ByRef aControls As Variant, ByRef aContainer As Object)
On Error GoTo ErrHandler
   
    Dim lIdx As Long, lCount As Long
    Dim lControl As Control
    Dim lValues As Variant
    Dim ls_TempTag As String
    
        lCount = aControls.Count - 1
    
        For lIdx = 0 To lCount
            Set lControl = aControls.Item(lIdx)
            If HasContainer(lControl, aContainer) Then
                Select Case UCase(TypeName(lControl))
                    Case "TEXTBOX"
                            ls_TempTag = lControl.Tag & SEP
                            lValues = Split(ls_TempTag, SEP)
                            If mo_Db.GetFieldIndex(ac_Cursor, lValues(0)) >= 0 Then
                                Select Case lValues(1)
                                    Case "Text"
                                        lControl.Text = mo_Db.GetFields(ac_Cursor, lValues(0))
                                    Case "Num"
                                        If (me_Mode = eMode.emView Or me_Mode = eMode.emDelete) Then
                                            lControl.Text = FormatNumber(mo_Db.GetFields(ac_Cursor, lValues(0)), lValues(2))
                                        Else
                                            lControl.Text = Replace(mo_Db.GetFields(ac_Cursor, lValues(0)), ms_DecimalSeparator, ".", , , vbTextCompare)
                                        End If
                                End Select
                            End If
                    
                    Case "ARMCOMBOBOX"
                        mb_Internal = True
                        ls_TempTag = lControl.Tag & SEP
                        lValues = Split(ls_TempTag, SEP)
                        If mo_Db.GetFieldIndex(ac_Cursor, lValues(0)) >= 0 Then
                            If mo_Db.GetFields(ac_Cursor, lValues(0)) = 0 Or mo_Db.GetFields(ac_Cursor, lValues(0)) = "" Then
                                Set lControl.SelectedItem = Nothing
                            Else
                                If lControl.SearchItem(mo_Db.GetFields(ac_Cursor, lValues(0)), 0, 0, True) = False Then
                                    If lControl.AddItem(Array(mo_Db.GetFields(ac_Cursor, lValues(0)), mo_Db.GetFields(ac_Cursor, lValues(1))), True) Is Nothing Then
                                        Err.Raise 2222, "", ""
                                    End If
                                End If
                            End If
                        End If
                        mb_Internal = False
                        
                    Case "OPTIONBUTTON"
                        lValues = Split(lControl.Tag, SEP)
                        If mo_Db.GetFieldIndex(ac_Cursor, lValues(0)) >= 0 Then
                            If UCase(lValues(2)) Like UCase(mo_Db.GetFields(ac_Cursor, lValues(0))) Then
                                lControl.value = True
                            End If
                        End If
                        
                    Case "CHECKBOX"
                        If mo_Db.GetFieldIndex(ac_Cursor, lControl.Tag) >= 0 Then
                            If UCase(mo_Db.GetFields(ac_Cursor, lControl.Tag)) Like "X" Then
                                lControl.value = vbChecked
                            Else
                                lControl.value = vbUnchecked
                            End If
                        End If
                        
                    Case "A_CALOCX"
                        lControl.date_courte = mo_Db.GetFields(ac_Cursor, lControl.Tag)
                        
                    Case "LABEL", "FRAME", "DIRLISTBOX", "FILELISTBOX", "DRIVELISTBOX", "TOOLBARCONTROL"
                        'Do Nothing
                    
                    Case "ARMGRID"
                    
                    Case Else
                        Debug.Print "LoadDataToForm  -> " & UCase(TypeName(lControl))
                End Select
            End If
            Set lControl = Nothing
        Next

    Exit Sub

ErrHandler:
    If Not lControl Is Nothing Then Set lControl = Nothing
    Call ErrorHandler("LoadDataToForm")

End Sub

Private Sub OpenCursor(ByVal as_Request As String)
On Error GoTo ErrHandler
    Dim ls_SelectRequest As String
        
        
    ls_SelectRequest = Replace(as_Request, "$Language_code$", ms_Language_Code, , , vbTextCompare)
    ls_SelectRequest = Replace(ls_SelectRequest, "$ProjCustLink_ID$", CLng(txt_key.Text), , , vbTextCompare)
    
    If ml_Cursor > 0 Then
        mo_Db.Close (ml_Cursor)
        ml_Cursor = 0
    End If
    
    ml_Cursor = OpenSQLSafe(mo_Db, ls_SelectRequest, 1)
    
    Exit Sub

ErrHandler:
    mo_Db.Close (ml_Cursor)
    Call ErrorHandler("OpenCursor")

End Sub

Private Sub SetMode(ByVal aMode As eMode)
    me_OldMode = me_Mode
    me_Mode = aMode
End Sub

Private Sub ClearForm(ByRef aControls As Variant, ByRef aContainer As Object)
On Error GoTo ErrHandler
    

    Dim lIdx As Long, lCount As Long, lControl As Object
    lCount = aControls.Count - 1
    For lIdx = 0 To lCount
        Set lControl = aControls.Item(lIdx)
        
        If HasContainer(lControl, aContainer) Then
            Select Case UCase(TypeName(lControl))
                Case "TEXTBOX"
                    lControl.Text = ""
                
                Case "ARMCOMBOBOX"
                    'Set lControl.SelectedItem = Nothing
                    lControl.Clear
                    DoEvents
                    
                Case "A_CALOCX"
                    lControl.reinit_cal
                
                Case "CHECKBOX"
                    lControl.value = vbUnchecked
                
                Case "ARMCHECKVIEW"
                    'lControl.Reset
                    
                Case "FRAME", "LABEL", "TOOLBARCONTROL", "PICTUREBOX", "COMMANDBUTTON"
                
                Case "ARMGRID"
                    lControl.ClearGrid
                    lControl.Requests = ""
                
                Case "LISTBOX"
                    lControl.ListIndex = -1
                
                Case "OPTIONBUTTON"
                    Dim lValues As Variant
                    lValues = Split(lControl.Tag, SEP)
                    lControl.value = lValues(1)
                
                Case "TABSTRIP", "DRIVELISTBOX", "DIRLISTBOX", "FILELISTBOX"
                
            End Select
        End If
        
        Set lControl = Nothing
    Next

    Exit Sub
ErrHandler:
    Call ErrorHandler("ClearForm")
End Sub

Private Sub EnableFrame(ByRef aControls As Variant, ByRef aContainer As Object, ByVal aEnabled As Boolean)
On Error GoTo ErrHandler
    Dim lIdx As Long, lCount As Long
    Dim lControl As Control
    
    lCount = aControls.Count - 1
    
    For lIdx = 0 To lCount
        Set lControl = aControls.Item(lIdx)
        If HasContainer(lControl, aContainer) Then
            Select Case UCase(TypeName(lControl))
                Case "TOOLBARCONTROL"
                    lControl.Visible = aEnabled
                
                Case "FRAME", "LABEL", "MSFLEXGRID"
                    ' Do nothing !
                
                Case "TEXTBOX"
                        lControl.Locked = Not aEnabled
                        lControl.BackColor = IIf(aEnabled, CL_COLOR_ENABLED, CL_COLOR_DISABLED)
                
                Case "ARMGRID", "ARMCHECKVIEW"
                    
                Case "DIRLISTBOX", "DRIVELISTBOX", "FILELISTBOX"
                    lControl.Enabled = aEnabled
                    lControl.BackColor = IIf(aEnabled, CL_COLOR_ENABLED, CL_COLOR_DISABLED)

                Case "ARMCOMBOBOX", "A_CALOCX", "OPTIONBUTTON", "ARMTREEVIEW", "LISTBOX", "PICTUREBOX", "TABSTRIP", "CHECKBOX"
                    lControl.Enabled = aEnabled
                
                Case "COMMANDBUTTON"
                
                
                Case "SOM_LINK", "SOM_QUOTE"
                
                Case Else
                    lControl.Enabled = aEnabled
                    Debug.Print "EnableFrame " & UCase(TypeName(lControl))
            End Select
        End If
        Set lControl = Nothing
    Next
    Exit Sub
ErrHandler:
    If Not lControl Is Nothing Then Set lControl = Nothing
    Call ErrorHandler("EnableFrame")

End Sub

Private Sub Item_UpdateInit(ByVal al_Key As Long)
On Error GoTo ErrHandler

    Dim ls_req As String

    Call SetMode(eMode.emUpdate)

    'visuel
    Call ClearForm(UserControl.Controls, Me)
    Call EnableFrame(UserControl.Controls, Me, True)
    
    fra_search.Visible = False
    Call DisableFields
    
    Call HandleToolbar(tbl_main, 2, mb_IsFullAccess)
    
    txt_key.Text = al_Key
    
    Call OpenCursor(mt_ClassInfoLst(0).RequestView)
    
    Call LoadDataToForm(ml_Cursor, UserControl.Controls, Me)
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_UpdateInit")
End Sub

Private Function Item_CheckMandatory() As Boolean

On Error GoTo ErrHandler
    Dim lIdx As Long, lCount As Long
    Dim lValues As Variant
    Dim ls_TempTag As String
    Dim ls_Str As String
    Dim lControl As Control
    
    Dim lv_MsgReplaceInfo(0, 1) As String
    
    'lecture du tableau des valeurs par defaut en prmeier car + petit
    lCount = UBound(mt_ClassInfoLst(0).ListFieldsMandatory)
    For lIdx = 0 To lCount
         
        Set lControl = mt_ClassInfoLst(0).ListFieldsMandatory(lIdx, 0)
            
        Select Case UCase(TypeName(lControl))
            Case "TEXTBOX"
                If lControl.Text = "" Then
                    lv_MsgReplaceInfo(0, 0) = "$LabelCaption$"
                    lv_MsgReplaceInfo(0, 1) = lbl_label(mt_ClassInfoLst(0).ListFieldsMandatory(lIdx, 1)).Caption
                    Call MsgBox(MsgText(2120, ms_Language_Code, "The field " & lv_MsgReplaceInfo(0, 1) & " is mandatory.", lv_MsgReplaceInfo), vbInformation)
                    lControl.SetFocus
                    
                    Exit Function
                End If
                
            Case "ARMCOMBOBOX"
                If lControl.SelectedItem Is Nothing Then
                    lv_MsgReplaceInfo(0, 0) = "$LabelCaption$"
                    lv_MsgReplaceInfo(0, 1) = lbl_label(mt_ClassInfoLst(0).ListFieldsMandatory(lIdx, 1)).Caption
                    Call MsgBox(MsgText(2120, ms_Language_Code, "The field " & lv_MsgReplaceInfo(0, 1) & " is mandatory.", lv_MsgReplaceInfo), vbInformation)
                    lControl.SetFocus
                    
                    Exit Function
                End If
                
            Case "A_CALOCX"
                If lControl.date_courte = "" Then
                    lv_MsgReplaceInfo(0, 0) = "$LabelCaption$"
                    lv_MsgReplaceInfo(0, 1) = lbl_label(mt_ClassInfoLst(0).ListFieldsMandatory(lIdx, 1)).Caption
                    Call MsgBox(MsgText(2120, ms_Language_Code, "The field " & lv_MsgReplaceInfo(0, 1) & " is mandatory.", lv_MsgReplaceInfo), vbInformation)
                    lControl.SetFocus
                    
                    Exit Function
               End If
                
            Case "OPTIONBUTTON", "CHECKBOX", "ARMGRID", "LABEL", "FRAME", "DIRLISTBOX", "FILELISTBOX", "DRIVELISTBOX", "TOOLBARCONTROL"
                'Nothing to do
            Case Else
                Debug.Print "Item_CheckMandatory " & UCase(TypeName(mt_ClassInfoLst(0).ListFieldsMandatory(lIdx, 0)))
        End Select
        Set lControl = Nothing
    
    Next
    
    
    Item_CheckMandatory = True
    Exit Function
    
ErrHandler:
    Call ErrorHandler("Item_CheckMandatory")
    
End Function

Private Sub Item_Update()
On Error GoTo ErrHandler
    
    If Not Item_CheckMandatory() Then Exit Sub
    
    If Not Item_UpdateDB() Then Exit Sub

    RaiseEvent OnItemUpdate(eLink, CLng(txt_key.Text), Build_SrzString(UserControl.Controls, Me))

    Call Item_Exit
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_Update")
   
End Sub

Private Function Item_UpdateDB() As Boolean
On Error GoTo ErrHandler
    Dim lv_Request As Variant
    Dim lb_TranOpened As Boolean
    Dim ls_req As String

    

'open the transaction
    If Not BeginTran("UpdLinkDB") Then
        Err.Raise C_ERRORRAISE + 3, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to Start the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    lb_TranOpened = True
    
    ls_req = mt_ClassInfoLst(0).RequestUpd
    
'replace the holders
    ls_req = ReplaceHolders(UserControl.Controls, Me, ls_req)
    
    lv_Request = Split(ls_req, SEP)
    lv_Request(0) = Replace(lv_Request(0), "$iConcurrency$", mo_Db.GetFields(ml_Cursor, "iConcurrency"), , , vbTextCompare)
    
    ExecuteSQLSafe mo_Db, lv_Request(0), 1
    
    ExecuteSQLSafe mo_Db, lv_Request(1)
    
    ExecuteSQLSafe mo_Db, lv_Request(2)


'close the transaction
    If Not CommitTran("UpdLinkDB") Then
        Err.Raise C_ERRORRAISE + 4, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to commit the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    lb_TranOpened = False
    
    Item_UpdateDB = True

    Exit Function
    
ErrHandler:
    Item_UpdateDB = False
    If lb_TranOpened Then
         Call RollbackTran("UpdLinkDB")
    End If
    
    Call ErrorHandler("Item_UpdateDB")

End Function
Private Function Build_SrzStringFromOneLineInGrid(ByVal ao_grid As ArmGrid, ByVal as_KeyFieldToSearch As String, ByVal al_Key As Long) As String
On Error GoTo ErrHandler
    
    Dim ls_SrzString As String
    
    ao_grid.Columns(as_KeyFieldToSearch).Key = True
    ao_grid.SearchKey True, Array(al_Key)
    ao_grid.Columns(as_KeyFieldToSearch).Key = False
    
    Dim i As Long
    For i = 0 To ao_grid.Cols - 1
        ls_SrzString = ls_SrzString & ao_grid.Columns(i).Name & SEP1 & ao_grid.SelectedLine(0, i) & SEP
    Next i
    
    Build_SrzStringFromOneLineInGrid = ls_SrzString
    
    Exit Function
    
ErrHandler:
    Call ErrorHandler("Build_SrzStringFromOneLineInGrid")
End Function


Private Function Build_SrzString(ByRef aControls As Variant, ByRef aContainer As Object) As String
On Error GoTo ErrHandler
    Dim ls_SrzString As String
    Dim lo_Control As CheckBox
    Dim lIdx As Long, lCount As Long
    
    Dim lValues As Variant
    Dim ls_TempTag As String
    Dim ls_Str As String
    Dim lControl As Control
   
    
        lCount = aControls.Count - 1
        ls_SrzString = ""
    
        For lIdx = 0 To lCount
            Set lControl = aControls.Item(lIdx)
            If HasContainer(lControl, aContainer) Then
                
                ls_TempTag = lControl.Tag & SEP
                lValues = Split(ls_TempTag, SEP)
                
                Select Case UCase(TypeName(lControl))
                    Case "TEXTBOX"
                            Select Case lValues(1)
                                Case "Text"
                                    ls_SrzString = ls_SrzString & lValues(0) & SEP1 & lControl.Text & SEP
                                Case "Num"
                                    ls_Str = Replace(lControl.Text, ms_ThousandSeparator, "")
                                    ls_Str = Replace(ls_Str, ms_DecimalSeparator, ".")
                                    ls_SrzString = ls_SrzString & lValues(0) & SEP1 & ls_Str & SEP
                           End Select
                    
                    Case "ARMCOMBOBOX"
                        If cbo_IsFilled(lControl) Then
                            ls_SrzString = ls_SrzString & lValues(0) & SEP1 & lControl.SelectedItem.Key & SEP
                            ls_SrzString = ls_SrzString & lValues(1) & SEP1 & lControl.SelectedItem.GetData(1) & SEP
                        End If
                    Case "OPTIONBUTTON"
                        
                    Case "CHECKBOX"
                        
                    Case "A_CALOCX"
                        ls_SrzString = ls_SrzString & lValues(0) & SEP1 & lControl.date_courte & SEP
                        
                        
                    Case "LABEL", "FRAME", "DIRLISTBOX", "FILELISTBOX", "DRIVELISTBOX", "TOOLBARCONTROL"
                        'Do Nothing
                    
                    Case "ARMGRID"
                    
                    Case Else
                        Debug.Print "Build_SrzString  -> " & UCase(TypeName(lControl))
                End Select
            End If
            Set lControl = Nothing
        Next

    ls_SrzString = Trim(ls_SrzString)
    Build_SrzString = ls_SrzString
    
    Exit Function
    
ErrHandler:
    Call ErrorHandler("Build_SrzString")
End Function

Private Function ReplaceHolders(ByRef aControls As Variant, ByRef aContainer As Object, ByVal as_Request As String) As String
    
On Error GoTo ErrHandler

    Dim lIdx As Long, lCount As Long
    Dim lControl As Control
    Dim lv_number As Variant
    Dim lValues As Variant
    
    
    lCount = aControls.Count - 1
    
    For lIdx = 0 To lCount
        Set lControl = aControls.Item(lIdx)
        If HasContainer(lControl, aContainer) Then
            Select Case UCase(TypeName(lControl))
                Case "TEXTBOX"
                    'handle numeric
                    lValues = Split(lControl.Tag, SEP)
                    If UBound(lValues) > 0 Then
                        Select Case lValues(1)
                            Case "Text"
                                as_Request = Replace(as_Request, "$" & lValues(0) & "$", Replace(Trim(lControl.Text), "'", "''"), , , vbTextCompare)
                            Case "Num"
                                'lv_number = Replace(Trim(IIf(lControl.Text = "", 0, lControl.Text)), ms_ThousandSeparator, "", , , vbTextCompare)
                                lv_number = Replace(Trim(IIf(lControl.Text = "", 0, lControl.Text)), ms_ThousandSeparator, "", , , vbTextCompare)
                                lv_number = Replace(lv_number, ms_DecimalSeparator, ".", , , vbTextCompare)
                                'lv_number = Replace(Trim(IIf(lControl.Text = "", 0, lControl.Text)), ms_DecimalSeparator, ".", , , vbTextCompare)
                                as_Request = Replace(as_Request, "$" & lValues(0) & "$", lv_number, , , vbTextCompare)
                                
                        End Select
                    End If
                Case "ARMCOMBOBOX"
                    lValues = Split(lControl.Tag, SEP)
                    If Not lControl.SelectedItem Is Nothing Then
                        as_Request = Replace(as_Request, "$" & lValues(0) & "$", IIf(lControl.SelectedItem.Key = "", "NULL", lControl.SelectedItem.Key), , , vbTextCompare)
                    Else
                        as_Request = Replace(as_Request, "$" & lValues(0) & "$", "NULL", , , vbTextCompare)
                    End If
                    
                Case "OPTIONBUTTON"
                'To be done
                'only for add
'                    If lControl.Value = True Then
'                        lValues = Split(lControl.Tag, SEP)
'                        as_Request = Replace(as_Request, "$" & lValues(0) & "$", lValues(2), , , vbTextCompare)
'                    End If
                
            End Select
        End If
        Set lControl = Nothing
    Next
    
    as_Request = Replace(as_Request, "$Language_code$", ms_Language_Code, , , vbTextCompare)
    as_Request = Replace(as_Request, "$Z_Creator$", ml_UCode, , , vbTextCompare)
    as_Request = Replace(as_Request, "$Z_last_upd_user$", ml_UCode, , , vbTextCompare)
    as_Request = Replace(as_Request, "'Null'", "Null", , , vbTextCompare)

    ReplaceHolders = as_Request
    Exit Function
    
ErrHandler:
    If Not lControl Is Nothing Then Set lControl = Nothing
    Call ErrorHandler("ReplaceHolders")
    
End Function

Private Sub DisableFields()
On Error GoTo ErrHandler

    txt_CustName.Locked = True
    txt_CustName.BackColor = CL_COLOR_DISABLED
    txt_project.Locked = True
    txt_project.BackColor = CL_COLOR_DISABLED
    txt_projNum.Locked = True
    txt_projNum.BackColor = CL_COLOR_DISABLED
    
    If ml_Driver = eCustomer Then
        lbl_label(1).Visible = False
        txt_projNum.Visible = False
    End If
    
    If me_Mode = eMode.emAdd Then
        Select Case ml_Driver
        Case eProject
            fra_field_cust.Visible = True
            fra_field_proj.Visible = False
        
        Case eCustomer
            fra_field_cust.Visible = False
            fra_field_proj.Visible = True
            
        Case Else
            Debug.Print "DisableFields: other component !!!"
        End Select
    End If
    
   Exit Sub
ErrHandler:
    Call ErrorHandler("DisableFields")
End Sub

Private Sub Item_Exit()
On Error GoTo ErrHandler
    
    If ml_Cursor > 0 Then
        mo_Db.Close (ml_Cursor)
        ml_Cursor = 0
    End If
    
    RaiseEvent OnExit
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_Exit")
End Sub

Private Sub Item_UpdateReinit(ByVal al_Cursor As Long)
On Error GoTo ErrHandler

    Dim ls_req As String
    
    Call ClearForm(UserControl.Controls, Me)
    
    Call LoadDataToForm(al_Cursor, UserControl.Controls, Me)
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_UpdateReinit")
End Sub

Private Sub Item_DeleteInit(ByVal al_Key As Long)
On Error GoTo ErrHandler

    Call SetMode(eMode.emDelete)
    
    'visuel
    Call ClearForm(UserControl.Controls, Me)
    Call EnableFrame(UserControl.Controls, Me, False)
    Call EnableFrame(UserControl.Controls, fra_search, False)
    Call HandleToolbar(tbl_main, 3, mb_IsFullAccess)
    
    txt_key.Text = al_Key
    
    'Remplissage
    Call OpenCursor(mt_ClassInfoLst(0).RequestView)
    
    Call LoadDataToForm(ml_Cursor, UserControl.Controls, Me)
    
    fra_search.Visible = False
    
    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Item_DeleteInit")
End Sub

Private Sub Item_Delete()
On Error GoTo ErrHandler

    If MsgBox(MsgText(C_ERRORRAISE + 52, ms_Language_Code, "#Do you really want to delete the selected item and all the data related to this item?"), vbExclamation + vbDefaultButton2 + vbYesNo) <> vbYes Then Exit Sub

    If Not Item_DeleteDB() Then Exit Sub
        
    RaiseEvent OnItemDelete(eLink, CLng(txt_key.Text))
    
    Call Item_Exit
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_Delete")
End Sub

Private Function Item_DeleteDB() As Boolean
On Error GoTo ErrHandler

    Dim lv_DEL_Request As Variant
    Dim lb_TranOpened As Boolean
    

'open the transaction
    If Not BeginTran("DelLinkDB") Then
        Err.Raise C_ERRORRAISE + 3, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to Start the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    lb_TranOpened = True
    
    lv_DEL_Request = Split(ReplaceHolders(UserControl.Controls, Me, Replace(mt_ClassInfoLst(0).RequestDel, "$iConcurrency$", mo_Db.GetFields(ml_Cursor, "iConcurrency"))), SEP)
    lv_DEL_Request(0) = Replace(lv_DEL_Request(0), "$iConcurrency$", mo_Db.GetFields(ml_Cursor, "iConcurrency"), , , vbTextCompare)
    
    ExecuteSQLSafe mo_Db, lv_DEL_Request(0), 1
    
    ExecuteSQLSafe mo_Db, lv_DEL_Request(1)
    ExecuteSQLSafe mo_Db, lv_DEL_Request(2)
    ExecuteSQLSafe mo_Db, lv_DEL_Request(3)
    
'close the transaction
    If Not CommitTran("DelLinkDB") Then
        Err.Raise C_ERRORRAISE + 4, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to commit the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    lb_TranOpened = False
    
    Item_DeleteDB = True

    Exit Function
    
ErrHandler:
    Item_DeleteDB = False
    If lb_TranOpened Then
         Call RollbackTran("DelLinkDB")
    End If
    
    Call ErrorHandler("Item_DeleteDB")
End Function

Private Sub Item_AddInit(ByVal as_SrzFields As String)
On Error GoTo ErrHandler
    
    Call SetMode(eMode.emAdd)
    
    'Moved there because in initcomponent the property DrivenBy is not relevant ( apllyed later)
    'ToDO: lables
    ' May be only one at the first init
    If mb_FirstAddInit = False Then
        grd_result.Title = "#Result"
        grd_result.AllowPrint = False
        grd_result.MultiSelect = False
    
        Select Case ml_Driver
        Case eProject
            ReDim lColumns(6)
            lColumns(0) = Join(Array("Cust_ID", 0, 0, "Cust_ID", "#Cust_ID"), SEP)
            lColumns(1) = Join(Array("Cust_name", 2000, 0, "Cust_name", "#Name"), SEP)
            lColumns(2) = Join(Array("Cust_addr1", 2000, 0, "Cust_addr1", "#Address1"), SEP)
            lColumns(3) = Join(Array("Cust_addr2", 2000, 0, "Cust_addr2", "#Address2"), SEP)
            lColumns(4) = Join(Array("Cust_town", 2000, 0, "Cust_town", "#Town"), SEP)
            lColumns(5) = Join(Array("Cust_tel", 2000, 0, "Cust_tel", "#Tel No"), SEP)
            lColumns(6) = Join(Array("CT_desc", 2000, 0, "CT_desc", "#Country"), SEP)
    
        Case eCustomer
            ReDim lColumns(11)
            lColumns(0) = Join(Array("Proj_ID", 0, 0, "Proj_ID", "#Proj_ID"), SEP)
            lColumns(1) = Join(Array("ProjectNumber", 1000, 0, "ProjectNumber", "#Project no."), SEP)
            lColumns(2) = Join(Array("Proj_Name", 2000, 0, "Proj_Name", "#Name"), SEP)
            lColumns(3) = Join(Array("Proj_Town", 2000, 0, "Proj_Town", "#Town"), SEP)
            lColumns(4) = Join(Array("CT_desc", 2000, 0, "CT_desc", "#Country"), SEP)
            lColumns(5) = Join(Array("MKS_desc", 2000, 0, "MKS_desc", "#Market segment"), SEP)
            lColumns(6) = Join(Array("Proj_Size", 1500, 0, "Proj_Size", "#Volume"), SEP)
            lColumns(7) = Join(Array("UM_code", 1000, 0, "UM_code", "#In"), SEP)
            lColumns(8) = Join(Array("Proj_Exporderdate", 1500, 0, "Proj_Exporderdate", "#Exp order date"), SEP)
            lColumns(9) = Join(Array("Proj_Followupdate", 1500, 0, "Proj_Followupdate", "#Follow up date"), SEP)
            lColumns(10) = Join(Array("SSR_Desc", 2000, 0, "SSR_Desc", "#sales person"), SEP)
            lColumns(11) = Join(Array("PS_desc", 1500, 0, "PS_desc", "#Project status"), SEP)
        End Select
        
        If Not grd_result.SetColumns(lColumns) Then
            Debug.Print "grd_result.SetColumns error"
            Call Unload_A_COM
            End
        End If
        
        Dim ls_Text As String
        Dim ls_Title As String
        
        Select Case ml_Driver
            Case eProject
                ls_Text = ms_LabelGridCust
                
                If ls_Text <> "" And InStr(1, ls_Text, SEP) <> 0 Then
                    ls_Title = Mid(ls_Text, 1, InStr(1, ls_Text, SEP) - 1)
                            
                    grd_result.Title = ls_Title
                                
                    ls_Text = Mid(ls_Text, InStr(1, ls_Text, SEP) + 2)
                    Call grd_result.LoadConstants(ptStatic, ls_Text, ctColumns)
                End If
                
            Case eCustomer
                ls_Text = ms_LabelGridProj
                If ls_Text <> "" And InStr(1, ls_Text, SEP) <> 0 Then
                    ls_Title = Mid(ls_Text, 1, InStr(1, ls_Text, SEP) - 1)
                                
                    grd_result.Title = ls_Title
                                
                    ls_Text = Mid(ls_Text, InStr(1, ls_Text, SEP) + 2)
                
                    Call grd_result.LoadConstants(ptStatic, ls_Text, ctColumns)
                End If
                
        End Select
        
        mb_FirstAddInit = True
    End If

    'visuel
    Call ClearForm(UserControl.Controls, Me)
    Call EnableFrame(UserControl.Controls, Me, True)
    
    Call DisableFields
    Call HandleToolbar(tbl_main, 1, mb_IsFullAccess)
    
    Call EnableFrame(UserControl.Controls, fra_search, True)
    fra_search.Visible = True
    
    txt_key.Text = 0
    
    'Get data to display from data area
    Select Case ml_Driver
        Case eProject
            txt_projID = GetFieldValueFromSrz(as_SrzFields, "Proj_ID")
            txt_project = GetFieldValueFromSrz(as_SrzFields, "Proj_Name")
            txt_projNum = GetFieldValueFromSrz(as_SrzFields, "ProjectNumber")
            
        Case eCustomer
            txt_CustID = GetFieldValueFromSrz(as_SrzFields, "Cust_ID")
            txt_CustName = GetFieldValueFromSrz(as_SrzFields, "Cust_Name")
    
    End Select
    
    Call OnFilterChange
    
   Exit Sub
    
ErrHandler:
    Call ErrorHandler("Item_AddInit")
End Sub

Private Function GetFieldValueFromSrz(ByVal as_SrzFields As String, ByVal as_Param As String) As String
On Error GoTo ErrHandler
    
    Dim lv_SrzFields As Variant
    Dim lv_Values
    Dim ll_count As Long
    Dim ll_Nb As Long
       
    If right(as_SrzFields, 2) <> SEP Then as_SrzFields = as_SrzFields & SEP
       
    lv_SrzFields = Split(as_SrzFields, SEP)
    ll_Nb = UBound(lv_SrzFields) - 1

    For ll_count = 0 To ll_Nb
        lv_Values = Split(lv_SrzFields(ll_count), SEP1)
         If UCase(as_Param) = UCase(lv_Values(0)) Then
           GetFieldValueFromSrz = lv_Values(1)
           Exit For
         End If
    Next ll_count
    
    Exit Function
ErrHandler:
    Call ErrorHandler("GetFieldValueFromSrz")
End Function

Private Sub OnFilterChange()
On Error GoTo ErrHandler

    Const REQ_CUST As String = "EXEC SOM_Customer_Lst '$Proj_ID$','$0$', '$1$', '$2$', '$Country$', '$Language_code$'"
    Const REQ_PROJ As String = "EXEC SOM_Project_Lst2 '$Cust_ID$','$0$', '$1$', '$2$', '$3$', '$CT_code$', '$Language_code$'"
    
    Dim ls_req As String
    Dim ls_Like As String
    Dim ll_Idx As Long, ll_count As Long
    
    ' Build string like
    ls_Like = "null"
    If Len(txt_SearchStr.Text) > 0 Then
        If opt_Like(0).value = True Then ls_Like = txt_SearchStr.Text & "%"
        If opt_Like(1).value = True Then ls_Like = "%" & txt_SearchStr.Text & "%"
        If opt_Like(2).value = True Then ls_Like = "%" & txt_SearchStr.Text
    End If
    
    
    'Who is the driver
    Select Case ml_Driver
    Case eProject
        ls_req = REQ_CUST
    Case eCustomer
        ls_req = REQ_PROJ
    End Select
    
    'replace the holders
    ls_req = ReplaceHolders(UserControl.Controls, Me, ls_req)
    
    'Put like string at the right place
        Select Case ml_Driver
        
        Case eProject
            ll_count = Opt_field_cust.UBound
            
        Case eCustomer
            ll_count = Opt_field_proj.UBound
        End Select
        
    For ll_Idx = 0 To ll_count
        Select Case ml_Driver
        
        Case eProject
            If Opt_field_cust(ll_Idx).value = True Then
                ls_req = Replace(ls_req, "$" & ll_Idx & "$", ls_Like)
            Else
                ls_req = Replace(ls_req, "$" & ll_Idx & "$", "null")
            End If
            
        Case eCustomer
            If Opt_field_proj(ll_Idx).value = True Then
                ls_req = Replace(ls_req, "$" & ll_Idx & "$", ls_Like)
            Else
                ls_req = Replace(ls_req, "$" & ll_Idx & "$", "null")
            End If
        End Select
    Next
    
    'Is combo filled
    If cbo_IsFilled(cbo_SearchCountry) Then
        ls_req = Replace(ls_req, "$Country$", cbo_SearchCountry.SelectedItem.GetData(0))
    Else
        ls_req = Replace(ls_req, "$Country$", "null")
    End If
    
    'Do not allow a search without any paramater
    'If Not cbo_IsFilled(cbo_SearchCountry) And txt_SearchStr.Text = "" Then Exit Sub
    
    ls_req = Replace(ls_req, "'null'", "null")
    
    Call grd_result.ClearGrid
    grd_result.Requests = ""
    
    Call grd_result.Load(ls_req, False, , , True)

    Exit Sub
ErrHandler:

    Call ErrorHandler("OnFilterChange")
    
End Sub

Private Sub lbl_Like_Click(Index As Integer)
On Error GoTo ErrHandler
    
    Select Case Index
        Case 2, 3
            opt_Like(0).value = True
        Case 0, 1
            opt_Like(1).value = True
        Case 4, 5
            opt_Like(2).value = True
    End Select
    
    Exit Sub

ErrHandler:
    Select Case Err.Number
    Case Else
        Call LogMessage("lbl_Like_Click: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E")
        Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
        End
    End Select
    
End Sub


Private Function cbo_IsFilled(ByVal ao_Combo As ArmCombobox) As Boolean
On Error GoTo ErrHandler

    If ao_Combo.SelectedItem Is Nothing Then Exit Function
    
    If ao_Combo.SelectedItem.Key = "" Then Exit Function
    
    cbo_IsFilled = True
    Exit Function
ErrHandler:
    Call ErrorHandler("cbo_IsFilled")

End Function

Private Sub txt_SearchStr_Change()
On Error GoTo ErrHandler

    'clear grid if it has already row(s)
    grd_result.ClearGrid
    
    OnFilterChange
    
    Exit Sub
ErrHandler:

    Select Case Err.Number
        Case Else
            Call LogMessage("txt_SearchStr_Change: " & Err.Number & ": " & Err.Source & ": " & Err.Description, "E", False)
            Call MsgBox("Error during the process, Contact immediatly your IT support.", vbCritical)
            End
    End Select
End Sub

Private Sub Item_AddReinit()
On Error GoTo ErrHandler
    
    Select Case ml_Driver
    Case eProject
        txt_CustID.Text = ""
        txt_CustName.Text = ""
            
    Case eCustomer
        txt_projID.Text = ""
        txt_project.Text = ""
    End Select
    
    Exit Sub
ErrHandler:
    Call ErrorHandler("Item_AddReinit")
End Sub

Private Function Item_Add() As Boolean
On Error GoTo ErrHandler
    
    Dim ls_SrzString As String
    
    'check the data validity -> Item_Check
    If Not Item_CheckMandatory() Then Exit Function
    
    'check if link already exists
    If Not Item_Check() Then Exit Function
    
    'update data on the db ->
    If Not Item_AddDB Then Exit Function  'GoTo BackToGrid
        
    Select Case ml_Driver
        Case eProject
            ls_SrzString = Build_SrzStringFromOneLineInGrid(grd_result, "Cust_ID", CLng(txt_CustID.Text))
        Case eCustomer
            ls_SrzString = Build_SrzStringFromOneLineInGrid(grd_result, "Proj_ID", CLng(txt_projID.Text))
    End Select
    
    If right(ls_SrzString, 2) <> SEP Then ls_SrzString = ls_SrzString & SEP
    
    ls_SrzString = ls_SrzString & Build_SrzString(UserControl.Controls, Me)
    
    RaiseEvent OnItemAdd(eLink, CLng(txt_key.Text), ls_SrzString)
    
    Call Item_Exit
    
    Item_Add = True
    
    Exit Function
ErrHandler:
    Call ErrorHandler("Item_Add")

End Function

Private Function Item_AddDB() As Boolean
On Error GoTo ErrHandler
    
    Dim lv_ADD_Request As Variant
    Dim ls_ADD_Request As String
    Dim lb_TranOpened As Boolean
    
'get the request from the ClassInfo
    ls_ADD_Request = mt_ClassInfoLst(0).RequestAdd
    
    Dim ll_NextID As Long
    Dim ll_NextCountryID As Long
    
    ll_NextID = mo_Db.SQLNextID("SOM_ProjCustLink")
    
    txt_key.Text = ll_NextID
    
'replace the holders
    ls_ADD_Request = ReplaceHolders(UserControl.Controls, Me, ls_ADD_Request)
    
    lv_ADD_Request = Split(ls_ADD_Request, SEP)
    
    If Not BeginTran("AddLinkDB") Then
        Err.Raise C_ERRORRAISE + 3, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to Start the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    
    lb_TranOpened = True
    
    
    ExecuteSQLSafe mo_Db, lv_ADD_Request(0), 1
    
    ExecuteSQLSafe mo_Db, lv_ADD_Request(1)
    
    ExecuteSQLSafe mo_Db, lv_ADD_Request(2)
    
'close the transaction
    If Not CommitTran("AddLinkDB") Then
        Err.Raise C_ERRORRAISE + 4, SCREEN_NAME, "Transaction Failure." & vbCrLf & "Enable to commit the transaction." & vbCrLf & Join(mo_Db.SQLErrorMessages, vbCrLf)
    End If
    lb_TranOpened = False
    
    Item_AddDB = True
    
    Exit Function
    
ErrHandler:
    Item_AddDB = False
    If lb_TranOpened Then
         Call RollbackTran("AddLinkDB")
    End If
    
    Call ErrorHandler("Item_AddDB")

End Function

Private Sub Invalidate_Process(ByVal as_caption As String)
On Error GoTo ErrHandler
    
'    Select Case ml_Driver
'    Case eProject
'        lbl_info.Caption = MsgText(3056, ms_Language_Code, "#You have deleted the customer from an other place. This action will be terminated.")
'    Case eCustomer
'        lbl_info.Caption = MsgText(3057, ms_Language_Code, "#You have deleted the project from an other place. This action will be terminated.")
'    Case Else
'        lbl_info.Caption = MsgText(3058, ms_Language_Code, "#You have deleted the link between the project and the customer from an other place. This action will be terminated.")
'    End Select
    
    lbl_info.Caption = as_caption
    
    Call EnableFrame(UserControl.Controls, Me, False)
    Call EnableFrame(UserControl.Controls, fra_invalidate, True)
    
    fra_invalidate.ZOrder
    fra_invalidate.Visible = True

    Exit Sub
ErrHandler:
    Call ErrorHandler("Invalidate_Process")
End Sub

Private Sub AddLineToGrid(ByVal ao_grid As ArmGrid, ByVal as_key As String, ByVal as_SrzFields As String)
On Error GoTo ErrHandler
    
    Dim lv_SrzFields As Variant
    Dim lv_Values
    Dim ll_count As Long
    Dim ll_Nb As Long
    
    Dim ls_ColNames As String
    
    Dim ls_req As String
    Dim ll_curs As Long
    
    If ao_grid.Requests(0) = "" Then Exit Sub

    ao_grid.AddLine
    
    ls_ColNames = SEP2
    Dim i As Long
    For i = 0 To ao_grid.Cols - 1
        ls_ColNames = ls_ColNames & ao_grid.Columns(i).FieldName & SEP2
    Next i
    
    For i = 0 To ao_grid.Cols - 1
        If GetFieldValueFromSrz(as_SrzFields, ao_grid.Columns(i).FieldName) = "" Then
            ls_req = ao_grid.Requests(0) & "," & GetFieldValueFromSrz(as_SrzFields, as_key)
            as_SrzFields = ""
            ll_curs = OpenSQLSafe(mo_Db, ls_req, 1)
           
            For ll_count = 0 To mo_Db.FieldCount(ll_curs) - 1
                as_SrzFields = as_SrzFields & mo_Db.GetFieldName(ll_curs, ll_count) & SEP1 & mo_Db.GetFields(ll_curs, ll_count) & SEP
            Next ll_count
            mo_Db.Close (ll_curs)

            Exit For
        End If
    Next i
    
    
    lv_SrzFields = Split(as_SrzFields, SEP)
    ll_Nb = UBound(lv_SrzFields) - 1

    For ll_count = 0 To ll_Nb
        lv_Values = Split(lv_SrzFields(ll_count), SEP1)
         If InStr(1, UCase(ls_ColNames), SEP2 & UCase(lv_Values(0)) & SEP2) Then
            ao_grid.SelectedLine(0, lv_Values(0)) = lv_Values(1)
         End If
    Next ll_count
    
    Exit Sub
ErrHandler:
    mo_Db.Close (ll_curs)
    Call ErrorHandler("AddLineToGrid")
End Sub

Private Function Item_Check() As Boolean
On Error GoTo ErrHandler

    Const C_CHECK As String = "EXEC SOM_ProjCustLink_chk $Proj_ID$, $Cust_ID$"
    Dim ls_req As String
    Dim ll_curs As Long
    
    ls_req = C_CHECK

    ls_req = Replace(ls_req, "$Proj_ID$", CLng(txt_projID.Text))
    ls_req = Replace(ls_req, "$Cust_ID$", CLng(txt_CustID))
    
    ' Check unicity
    If Not mo_Db.ExecuteSQL(ls_req) Or mo_Db.SQLRowsAffected <> 0 Then
        MsgBox MsgText(3061, ms_Language_Code, "This link between this project & this customer already exists. Change one and retry."), vbInformation
        Exit Function
    End If

    Item_Check = True
    Exit Function
    
ErrHandler:
    Call ErrorHandler("Item_Check")

End Function

Private Sub Hide_Icons()
On Error GoTo ErrHandler

    'retrieve project status
    Select Case me_ProjStatus
    Case eProjSpecification, eProjQuoteTender
        tbl_main.ButtonEnabled("B") = True
        tbl_main.ButtonEnabled("C") = True
    
    Case Else
        'disabled icon
        tbl_main.ButtonEnabled("B") = False
        tbl_main.ButtonEnabled("C") = False
    End Select

    Exit Sub
    
ErrHandler:
    Call ErrorHandler("Hide_Icons")
End Sub

