VERSION 5.00
Begin VB.UserControl TSMRptItm 
   ClientHeight    =   10335
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   15480
   LockControls    =   -1  'True
   ScaleHeight     =   10335
   ScaleWidth      =   15480
   Begin Project1.ToolbarControl tlb_main 
      Height          =   690
      Left            =   90
      TabIndex        =   22
      Top             =   30
      Visible         =   0   'False
      Width           =   3285
      _ExtentX        =   5794
      _ExtentY        =   1217
   End
   Begin VB.Frame Fra_Result 
      Caption         =   "Fra_Result"
      Height          =   8145
      Left            =   90
      TabIndex        =   21
      Tag             =   "Fra_Result"
      Top             =   2130
      Visible         =   0   'False
      Width           =   15255
      Begin Project1.ArmGrid grd_reports 
         Height          =   3705
         Left            =   60
         TabIndex        =   8
         Tag             =   "grd_Reports"
         Top             =   4200
         Visible         =   0   'False
         Width           =   15135
         _ExtentX        =   26696
         _ExtentY        =   6535
      End
      Begin Project1.ArmGrid grd_Item 
         Height          =   3705
         Left            =   30
         TabIndex        =   7
         Tag             =   "grd_Items"
         Top             =   240
         Visible         =   0   'False
         Width           =   15135
         _ExtentX        =   26696
         _ExtentY        =   6535
      End
   End
   Begin VB.Frame Fra_Filters 
      Caption         =   "Fra_Filters"
      Height          =   1335
      Left            =   90
      TabIndex        =   10
      Tag             =   "Fra_Filters"
      Top             =   750
      Visible         =   0   'False
      Width           =   15255
      Begin VB.Frame Fra_Type 
         Caption         =   "#Item Type"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   1185
         Left            =   0
         TabIndex        =   14
         Tag             =   "Fra_ItemType"
         Top             =   150
         Width           =   1515
         Begin VB.OptionButton Opt_Type 
            Caption         =   "#All"
            Height          =   285
            Index           =   0
            Left            =   90
            TabIndex        =   9
            Tag             =   "BI_type®©NULL"
            Top             =   270
            Width           =   1335
         End
         Begin VB.OptionButton Opt_Type 
            Caption         =   "#Standard"
            Height          =   345
            Index           =   1
            Left            =   90
            TabIndex        =   0
            Tag             =   "BI_type®©STD"
            Top             =   510
            Value           =   -1  'True
            Width           =   1335
         End
         Begin VB.OptionButton Opt_Type 
            Caption         =   "#Special"
            Height          =   285
            Index           =   2
            Left            =   90
            TabIndex        =   15
            Tag             =   "BI_type®©SPC"
            Top             =   810
            Width           =   1335
         End
      End
      Begin VB.Frame Fra_Validity 
         Caption         =   "#Item Validity"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   1185
         Left            =   1560
         TabIndex        =   11
         Tag             =   "Fra_ItemValidity"
         Top             =   150
         Width           =   1515
         Begin VB.OptionButton Opt_Validity 
            Caption         =   "#All"
            Height          =   255
            Index           =   0
            Left            =   90
            TabIndex        =   13
            Tag             =   "drop_date_ind®©NULL"
            Top             =   300
            Width           =   1335
         End
         Begin VB.OptionButton Opt_Validity 
            Caption         =   "#Dropped"
            Height          =   255
            Index           =   1
            Left            =   90
            TabIndex        =   12
            Tag             =   "drop_date_ind®©Y"
            Top             =   570
            Width           =   1335
         End
         Begin VB.OptionButton Opt_Validity 
            Caption         =   "#Not dropped"
            Height          =   255
            Index           =   2
            Left            =   90
            TabIndex        =   1
            Tag             =   "drop_date_ind®©N"
            Top             =   840
            Value           =   -1  'True
            Width           =   1335
         End
      End
      Begin Project1.ArmCombobox cbo_LogisticCountry 
         Height          =   345
         Left            =   7620
         TabIndex        =   4
         Tag             =   "CT_code_logistic"
         Top             =   390
         Width           =   2475
         _ExtentX        =   4366
         _ExtentY        =   609
      End
      Begin Project1.ArmCombobox cbo_Category 
         Height          =   345
         Left            =   5070
         TabIndex        =   3
         Tag             =   "CG_Code"
         Top             =   390
         Width           =   2475
         _ExtentX        =   4366
         _ExtentY        =   609
      End
      Begin Project1.A_calocx cal_ItemDateCretaed 
         Height          =   375
         Left            =   3150
         TabIndex        =   2
         Tag             =   "date"
         Top             =   390
         Width           =   1815
         _ExtentX        =   3201
         _ExtentY        =   661
      End
      Begin Project1.ArmCombobox cbo_TestCountry 
         Height          =   345
         Left            =   10170
         TabIndex        =   5
         Tag             =   "CT_code_test"
         Top             =   390
         Width           =   2475
         _ExtentX        =   4366
         _ExtentY        =   609
      End
      Begin Project1.ArmCombobox cbo_TestCategory 
         Height          =   345
         Left            =   12720
         TabIndex        =   6
         Tag             =   "cat_code"
         Top             =   390
         Width           =   2475
         _ExtentX        =   4366
         _ExtentY        =   609
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Item Date Created"
         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           =   0
         Left            =   3150
         TabIndex        =   20
         Tag             =   "lbl_ItemDateCreated"
         Top             =   150
         Width           =   1845
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Item Category"
         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            =   5070
         TabIndex        =   19
         Tag             =   "lbl_ItemCategory"
         Top             =   150
         Width           =   2475
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Logistic Country"
         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           =   2
         Left            =   7620
         TabIndex        =   18
         Tag             =   "lbl_LogisticCountry"
         Top             =   150
         Width           =   2475
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Test Country"
         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           =   3
         Left            =   10170
         TabIndex        =   17
         Tag             =   "lbl_TestCountry"
         Top             =   150
         Width           =   2475
      End
      Begin VB.Label lbl_label 
         Caption         =   "#Test Category"
         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           =   4
         Left            =   12720
         TabIndex        =   16
         Tag             =   "lbl_TestCategory"
         Top             =   150
         Width           =   2475
      End
   End
End
Attribute VB_Name = "TSMRptItm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Const SCREEN_NAME           As String = "TSMRptItm"

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


Public Event quit()

#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_Language_Code            As String
Private ml_UCode                    As Long

Private mb_grdreport_loaded         As Boolean

Private ListFieldsMandatory         As Variant


Private Type TRect
    top                             As Long
    left                            As Long
    Width                           As Long
    Height                          As Long
End Type

Private Zone_Client                 As TRect

Const C_ERRORRAISE                  As Long = 5000

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
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 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 Visible(ByVal aVisible As Boolean)
    UserControl.Extender.Visible = aVisible
End Property

Public Property Get Visible() As Boolean
    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
Private Sub HandleToolbar(ByVal atlb_Toolbar As Object, ByVal as_face As String)
On Error GoTo errHandler

    atlb_Toolbar.Visible = False
    atlb_Toolbar.DisplayFace (as_face)
    atlb_Toolbar.Visible = True
    
    Exit Sub
    
errHandler:
    Call ErrorHandler("HandleToolbar")
    
End Sub
Private Function CheckRights() As Boolean
    
On Error GoTo errHandler
    Const C_DB_REQUEST As String = "Exec TSMRptItm_CheckRights '$Login$'"
    
    Dim ls_Request As String
    Dim ll_Curs As Long
    ls_Request = Replace(C_DB_REQUEST, "$Login$", ms_LoginName, , , vbTextCompare)
    ll_Curs = OpenSQLSafe(mo_Db, ls_Request)
    
    If mo_Db.RowCount(ll_Curs) = 0 Then
        Call MsgBox(MsgText(2417, ms_Language_Code, "#You are not allowed to use this module."), vbInformation)
        mo_Db.Close (ll_Curs)
        Exit Function
    End If
    
    mo_Db.Close (ll_Curs)
    CheckRights = True
    Exit Function
    
errHandler:
    mo_Db.Close (ll_Curs)
    Call ErrorHandler("CheckRights")
    
End Function

Public Function Load_A_Com() As Boolean

    Const CL_REQUEST_TB As String = "SELECT Toolbar_Info FROM Toolbars_Users WHERE User_Code=$user_id$ and app_id=1"


On Error GoTo errHandler

    If mb_Initialized Then Exit Function
    
    
    ms_Language_Code = ms_Language_Code
    
    
    Dim ls_Toolbar_Info As String
    Dim lCursTB 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 tlb_main.Load_A_Com
    tlb_main.Language = "E"
    Call tlb_main.SetToolbarInfoStringParameters(ls_Toolbar_Info, "039")
    Call HandleToolbar(tlb_main, "0")
    
    If CheckRights Then
        'Initialize Components
        Call InitComponents
    Else
        cal_ItemDateCretaed.Language = ms_Language_Code
        cal_ItemDateCretaed.ZOrder
        cal_ItemDateCretaed.Limited_as_embedded = 1
    End If

    mb_grdreport_loaded = False
    
    ReDim ListFieldsMandatory(4, 1)
    Set ListFieldsMandatory(0, 0) = cal_ItemDateCretaed
    ListFieldsMandatory(0, 1) = 0
    Set ListFieldsMandatory(1, 0) = cbo_Category
    ListFieldsMandatory(1, 1) = 1
    Set ListFieldsMandatory(2, 0) = cbo_LogisticCountry
    ListFieldsMandatory(2, 1) = 2
    Set ListFieldsMandatory(3, 0) = cbo_TestCountry
    ListFieldsMandatory(3, 1) = 3
    Set ListFieldsMandatory(4, 0) = cbo_TestCategory
    ListFieldsMandatory(4, 1) = 4
    
    Call LoadAllLabels

    grd_Item.Visible = False
    grd_reports.Visible = False
    
    Fra_Filters.Caption = ""
    Fra_Result.Caption = ""
    
    Fra_Filters.BorderStyle = 0
    Fra_Result.BorderStyle = 0
    
    Fra_Filters.Visible = True
    Fra_Filters.ZOrder
    Fra_Result.Visible = True
    Fra_Result.ZOrder
    
    mb_Initialized = True
    Load_A_Com = True
    
    Exit Function

errHandler:
    Call ErrorHandler("Load_A_Com")

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

Private Sub InitComponents()

On Error GoTo errHandler

'Option buttons
    Opt_Type.Item(1).value = True
    Opt_Validity.Item(2).value = True
    
'Calendar
    cal_ItemDateCretaed.Language = ms_Language_Code
    cal_ItemDateCretaed.ZOrder
    cal_ItemDateCretaed.Limited_as_embedded = 1

'Grids
    Set grd_Item.ArmDb = mo_Db
    grd_Item.Load_A_Com
    grd_Item.AllowExcelExport = True
    grd_Item.ExportTitles = True
    grd_Item.MultiSelect = True
    grd_Item.Title = "Items"
    
    ReDim lColumns(12)
    lColumns(0) = Join(Array("BI_Sap_code", 2000, 1, "BI_Sap_code", "#BI SAP Code"), SEP)
    lColumns(1) = Join(Array("bi_desc", 3000, 0, "bi_desc", "#Item Description"), SEP)
    lColumns(2) = Join(Array("MFGP_code", 0, 0, "MFGP_code", "#Plant/Suppliers code"), SEP)
    lColumns(3) = Join(Array("MFGP_Desc", 1800, 0, "MFGP_Desc", "#Plant/Suppliers"), SEP)
    lColumns(4) = Join(Array("BML_Desc", 1500, 0, "BML_Desc", "#Base Material"), SEP)
    lColumns(5) = Join(Array("SF_Desc", 1500, 0, "SF_Desc", "#Surface"), SEP)
    lColumns(6) = Join(Array("PM_Desc", 1500, 0, "PM_Desc", "#Module"), SEP)
    lColumns(7) = Join(Array("BI_Thickness", 900, 0, "BI_Thickness", "#Thickness"), SEP)
    lColumns(8) = Join(Array("PS_Desc", 1500, 0, "PS_Desc", "#Segmentation"), SEP)
    lColumns(9) = Join(Array("PE_Desc", 1500, 0, "PE_Desc", "#Edge Detail"), SEP)
    lColumns(10) = Join(Array("PF_Desc", 1500, 0, "PF_Desc", "#Family"), SEP)
    lColumns(11) = Join(Array("SH_Desc", 1500, 0, "SH_Desc", "#Shape"), SEP)
    lColumns(12) = Join(Array("PP_Desc", 1500, 0, "PP_Desc", "#Pattern"), SEP)
    
    If Not grd_Item.SetColumns(lColumns) Then
        Debug.Print "grd_Item.SetColumns error"
        Call Unload_A_Com
        End
    End If
    
    Set grd_reports.ArmDb = mo_Db
    grd_reports.Load_A_Com
    grd_reports.AllowExcelExport = True
    grd_reports.ExportTitles = True
    grd_reports.MultiSelect = False
    grd_reports.Title = "Reports"
    
    
    ReDim lColumns(5)
    lColumns(0) = Join(Array("TST_code", 0, 0, "TST_code", "Test Code"), SEP)
    lColumns(1) = Join(Array("full_test_number", 2000, 1, "full_test_number", "Reference Number"), SEP)
    lColumns(2) = Join(Array("THO_Desc", 4000, 0, "THO_Desc", "Test House"), SEP)
    lColumns(3) = Join(Array("STD_desc", 2000, 0, "STD_desc", "Test Standard"), SEP)
    lColumns(4) = Join(Array("TT_Desc", 2000, 0, "TT_Desc", "Test Type"), SEP)
    lColumns(5) = Join(Array("Start_Date", 2000, 0, "Start_Date", "Start Date"), SEP)
    
    If Not grd_reports.SetColumns(lColumns) Then
        Debug.Print "grd_reports.SetColumns error"
        Call Unload_A_Com
        End
    End If
    
'combos
    Set cbo_Category.ArmDb = mo_Db
    Call cbo_Category.Load_A_Com
    Call cbo_Category.Clear
    cbo_Category.FirstBlankItem = False
    cbo_Category.Request = "exec categories_cbo '" & ms_Language_Code & "'"
    cbo_Category.Load
    
    
    Set cbo_LogisticCountry.ArmDb = mo_Db
    Call cbo_LogisticCountry.Load_A_Com
    Call cbo_LogisticCountry.Clear
    cbo_LogisticCountry.FirstBlankItem = False
    cbo_LogisticCountry.Request = "exec TSMRptItm_LogisticCountry_cbo '" & ms_Language_Code & "'"
    cbo_LogisticCountry.Load
    
    Set cbo_TestCountry.ArmDb = mo_Db
    Call cbo_TestCountry.Load_A_Com
    Call cbo_TestCountry.Clear
    cbo_TestCountry.FirstBlankItem = False
    cbo_TestCountry.Request = "exec TSMRptItm_TestCountry_cbo '" & ms_Language_Code & "','" & ms_LoginName & "'"
    cbo_TestCountry.Load
    
    Set cbo_TestCategory.ArmDb = mo_Db
    Call cbo_TestCategory.Load_A_Com
    Call cbo_TestCategory.Clear
    cbo_TestCategory.FirstBlankItem = False
    cbo_TestCategory.Request = "exec Test_Category_cbo '" & ms_Language_Code & "'"
    cbo_TestCategory.Load
    
    Exit Sub

errHandler:
    Call ErrorHandler("InitComponents")
    
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 Sub ExitScreen()
    RaiseEvent quit
End Sub

Private Function CheckMandatory() As Boolean

On Error GoTo errHandler
    
    Dim lIdx As Long, lCount As Long
    Dim lv_MsgReplaceInfo(0, 1) As String
    
    lCount = UBound(ListFieldsMandatory, 1)
    
    For lIdx = 0 To lCount
            Select Case UCase(TypeName(ListFieldsMandatory(lIdx, 0)))
                Case "FRAME", "LABEL", "MSFLEXGRID", "TOOLBARCONTROL"
                    ' Do nothing !
                
                Case "TEXTBOX"
                    ' Do nothing !
                    
                Case "ARMGRID", "ARMCHECKVIEW", "COMMANDBUTTON", "ARMTREEVIEW", "LISTBOX", "PICTUREBOX", "TABSTRIP", "CHECKBOX"
                    ' Do nothing !
                
                Case "A_CALOCX"
                    If ListFieldsMandatory(lIdx, 0).date_courte = "" Then
                        lv_MsgReplaceInfo(0, 0) = "$LabelCaption$"
                        lv_MsgReplaceInfo(0, 1) = lbl_label(ListFieldsMandatory(lIdx, 1)).Caption
                        Call MsgBox(MsgText(2120, ms_Language_Code, "The field " & lv_MsgReplaceInfo(0, 1) & " is mandatory.", lv_MsgReplaceInfo), vbInformation)
                        ListFieldsMandatory(lIdx, 0).SetFocus
                        Exit Function
                    End If
                                    
                Case "OPTIONBUTTON"
                    ' Do nothing !
                    
                Case "ARMCOMBOBOX"
                    If ListFieldsMandatory(lIdx, 0).SelectedItem Is Nothing Then
                        lv_MsgReplaceInfo(0, 0) = "$LabelCaption$"
                        lv_MsgReplaceInfo(0, 1) = lbl_label(ListFieldsMandatory(lIdx, 1)).Caption
                        Call MsgBox(MsgText(2120, ms_Language_Code, "The field " & lv_MsgReplaceInfo(0, 1) & " is mandatory.", lv_MsgReplaceInfo), vbInformation)
                        ListFieldsMandatory(lIdx, 0).SetFocus
                        Exit Function
                    End If
                                    
                Case Else
                    Debug.Print "Item_CheckMandatory " & UCase(TypeName(ListFieldsMandatory(lIdx, 0)))
            End Select
    Next

    CheckMandatory = True

    Exit Function
errHandler:
    Call ErrorHandler("Item_CheckMandatory")
    
End Function


Private Sub LinkReportToItem()
 
 On Error GoTo errHandler
 
    Const C_DB_REQUEST As String = "Exec Test_Link_Products_ins1 $test_code$, '$bi_sap_code$', '$mfgp_code$', '$linktype$'"


    If grd_Item.SelectedCount = 0 Or grd_reports.SelectedCount = 0 Then Exit Sub
    
    If MsgBox(MsgText(2418, ms_Language_Code, "#Are you sure you want to associate these items with this test reports?"), vbYesNo + vbDefaultButton2 + vbQuestion, "Validate") = vbNo Then Exit Sub
    
    Dim lIdx As Long, lCount As Long
    lCount = grd_Item.SelectedCount - 1
    If lCount < 0 Then Exit Sub
    
    Dim lKeys As Variant
    Dim ls_Request As String
    lKeys = grd_Item.MultiSelectedKey
    For lIdx = 0 To lCount
        ls_Request = Replace(C_DB_REQUEST, "$test_code$", grd_reports.SelectedLine(0, "TST_code"), , , vbTextCompare)
        ls_Request = Replace(ls_Request, "$bi_sap_code$", grd_Item.SelectedLine(0, "BI_SAP_Code"), , , vbTextCompare)
        ls_Request = Replace(ls_Request, "$mfgp_code$", "" & grd_Item.SelectedLine(0, "MFGP_code"), , , vbTextCompare)
        ls_Request = Replace(ls_Request, "$linktype$", "A", , , vbTextCompare)
        
        ExecuteSQLSafe mo_Db, ls_Request
        
        grd_Item.DeleteLine (lKeys(lIdx))
    Next
    grd_Item.DeselectRow
    
    Exit Sub
    
errHandler:
    Call ErrorHandler("LinkReportToItem")
    
End Sub
' 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



#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


Private Sub ResetScreen()

On Error GoTo errHandler

    Call HideClear
    
    Opt_Type.Item(1).value = True
    Opt_Validity.Item(2).value = True
    
    
    cal_ItemDateCretaed.reinit_cal
    cbo_Category.Clear
    cbo_LogisticCountry.Clear
    cbo_TestCountry.Clear
    cbo_TestCategory.Clear
    
    Exit Sub

errHandler:
    Call ErrorHandler("ResetScreen")

End Sub


Private Sub HideClear()
    
On Error GoTo errHandler
    
    Call HandleToolbar(tlb_main, "0")
    
    mb_grdreport_loaded = False

    Fra_Result.Visible = False
    
    grd_reports.Visible = False
    grd_Item.Requests = ""
    grd_reports.Requests = ""
    grd_Item.ClearGrid
    grd_reports.ClearGrid
    
    Exit Sub
    
errHandler:
    Call ErrorHandler("HideClear")
    
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

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
                
                Case "ARMCOMBOBOX"
                    'TODO : If not mandatory
                    'If cbo_Production_Unit_of_Measure.SelectedItem Is Nothing Then Stop
                    If Not lControl.SelectedItem Is Nothing Then
                        as_Request = Replace(as_Request, "$" & lControl.Tag & "$", IIf(lControl.SelectedItem.Key = "", "NULL", lControl.SelectedItem.Key), , , vbTextCompare)
                    Else
                        as_Request = Replace(as_Request, "$" & lControl.Tag & "$", "Null", , , vbTextCompare)
                    End If
                    
                Case "A_CALOCX"
                    If lControl.date_us <> "" Then
                        as_Request = Replace(as_Request, "$" & lControl.Tag & "$", lControl.date_us, , , vbTextCompare)
                    End If
               
                    
                Case "OPTIONBUTTON" ', "ARMTREEVIEW", "LISTBOX", "PICTUREBOX" '"A_CALOCX",
                    If lControl.value = True Then
                        lValues = Split(lControl.Tag, SEP)
                        as_Request = Replace(as_Request, "$" & lValues(0) & "$", lValues(1), , , 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, "'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 RunNow()

On Error GoTo errHandler

    Dim ls_Request_Item As String
    Dim ls_Request_Rep As String
    

    Const C_DB_REQUEST As String = "exec TSMRptItm_grid_Item '$date$','$drop_date_ind$','$CG_Code$','$CT_code_logistic$','$CT_code_test$',$cat_code$,'$BI_type$','$Language_Code$'"
    Const C_DB_REQUEST_Rep As String = "exec TSMRptItm_grid_reports '$CT_code_test$',$cat_code$,'$Language_Code$'"
    

    
    Screen.MousePointer = vbHourglass
    
    tlb_main.Enabled = False
    
    If Not CheckMandatory Then GoTo EndSub
    
    Fra_Filters.Enabled = False
    
    ls_Request_Item = ReplaceHolders(UserControl.Controls, Fra_Filters, C_DB_REQUEST)
    
    grd_Item.Requests = ls_Request_Item

    If grd_Item.Execute = False Then
        Debug.Print "grd_Item pb"
    End If

    
    grd_reports.Requests = ""
    grd_reports.ClearGrid
    
    ls_Request_Rep = ReplaceHolders(UserControl.Controls, Fra_Filters, C_DB_REQUEST_Rep)
    Call grd_reports.Load(ls_Request_Rep, False, , , True)
    
    mb_grdreport_loaded = False


    grd_Item.Visible = True
    grd_reports.Visible = True
    Fra_Result.Visible = True
    
    
EndSub:
    tlb_main.Enabled = True
    Fra_Filters.Enabled = True
    
    Screen.MousePointer = vbDefault
    
    Exit Sub

errHandler:
    Call ErrorHandler("RunNow")
    
End Sub

Private Sub cal_ItemDateCretaed_datechangee()
    If Fra_Result.Visible Then
        Call HideClear
    End If
End Sub

Private Sub cal_ItemDateCretaed_inoutcontainer(in_out As Byte)
    If in_out Then
        Set cal_ItemDateCretaed.Container = Me
        cal_ItemDateCretaed.top = Fra_Filters.top + cal_ItemDateCretaed.top
        cal_ItemDateCretaed.left = Fra_Filters.left + cal_ItemDateCretaed.left
    Else
        Set cal_ItemDateCretaed.Container = Fra_Filters
        cal_ItemDateCretaed.top = cal_ItemDateCretaed.top - Fra_Filters.top
        cal_ItemDateCretaed.left = cal_ItemDateCretaed.left - Fra_Filters.left
    End If
End Sub


Private Sub cbo_category_ComboItemSelected()
    If Fra_Result.Visible Then
        Call HideClear
    End If
End Sub

Private Sub cbo_LogisticCountry_ComboItemSelected()
    If Fra_Result.Visible Then
        Call HideClear
    End If
End Sub

Private Sub cbo_TestCategory_ComboItemSelected()
    If Fra_Result.Visible Then
        Call HideClear
    End If

End Sub

Private Sub cbo_TestCountry_ComboItemSelected()
    If Fra_Result.Visible Then
        Call HideClear
    End If

End Sub


Private Sub grd_reports_RowLoaded(ByVal al_Row As Long)
    If Not mb_grdreport_loaded Then
        Call HandleToolbar(tlb_main, "1")
        mb_grdreport_loaded = True
    End If
End Sub

Private Sub Opt_Type_Click(Index As Integer)
    If Fra_Result.Visible Then
        Call HideClear
    End If
End Sub

Private Sub Opt_Validity_Click(Index As Integer)
    If Fra_Result.Visible Then
        Call HideClear
    End If
End Sub

Private Sub tlb_main_action(ByVal as_Role As String, as_Language As String)
    Select Case as_Role
        Case "H" 'execute
            Call RunNow
        Case "I" 'Reset
            Call ResetScreen
        Case "A" 'Link reports to Items
            Call LinkReportToItem
        Case "T"
            Call ExitScreen
    End Select
End Sub
Private Sub LoadAllLabels()
    
On Error GoTo errHandler
    

    Call LoadLabels(UserControl.Controls, Fra_Filters, SCREEN_NAME & "_" & Fra_Filters.Tag, ms_Language_Code)
    Call LoadLabels(UserControl.Controls, Fra_Result, SCREEN_NAME & "_" & Fra_Result.Tag, ms_Language_Code)
    
    Exit Sub
    
errHandler:
    Call ErrorHandler("LoadAllLabels")
 
End Sub
' Load the labels of a containers
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 & lControl.Index
                        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
                        If mo_Db.Find(lLabels, "FIELD_NAME", lControl.Tag, , 1) >= 0 Then
                            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

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
