VERSION 5.00
Begin VB.Form C_search 
   BorderStyle     =   1  'Fixed Single
   ClientHeight    =   2070
   ClientLeft      =   1290
   ClientTop       =   3555
   ClientWidth     =   5715
   ControlBox      =   0   'False
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   2070
   ScaleWidth      =   5715
   Begin VB.CommandButton cmd_Replace 
      Caption         =   "Replace"
      Height          =   372
      Left            =   4380
      TabIndex        =   7
      Top             =   1150
      Visible         =   0   'False
      Width           =   1212
   End
   Begin VB.CommandButton cmd_ReplaceAll 
      Caption         =   "Replace all"
      Height          =   372
      Left            =   4380
      TabIndex        =   6
      Top             =   1600
      Visible         =   0   'False
      Width           =   1212
   End
   Begin VB.CommandButton cmd_next 
      Caption         =   "Find Next"
      Default         =   -1  'True
      Height          =   372
      Left            =   4380
      TabIndex        =   5
      Top             =   120
      Width           =   1212
   End
   Begin VB.CommandButton cmd_cancel 
      Caption         =   "Cancel"
      Height          =   372
      Left            =   4380
      TabIndex        =   4
      Top             =   600
      Width           =   1212
   End
   Begin VB.TextBox txt_Replace 
      Height          =   288
      Left            =   1320
      MaxLength       =   40
      TabIndex        =   2
      Top             =   1200
      Visible         =   0   'False
      Width           =   2772
   End
   Begin VB.TextBox txt_search 
      Height          =   288
      Left            =   1320
      MaxLength       =   40
      TabIndex        =   0
      Top             =   360
      Width           =   2772
   End
   Begin VB.Label lbl_Replace 
      Caption         =   "Replace with :"
      Height          =   255
      Left            =   60
      TabIndex        =   3
      Top             =   1200
      Visible         =   0   'False
      Width           =   1100
   End
   Begin VB.Label lbl 
      Height          =   252
      Left            =   60
      TabIndex        =   1
      Top             =   360
      Width           =   1100
   End
End
Attribute VB_Name = "C_search"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim mi_NbRows As Integer
Dim mi_NbCols As Integer
Dim mi_Ligne As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim success As Integer
Dim toprow_max As Integer
Dim prv_y As Integer
Dim prv_x As Integer
Dim find_ok As Boolean
Dim mb_LastRecord As Boolean
Dim mi_Nbfind As Integer
Dim mi_NbUpd As Integer

Public mo_grid4 As ArmGrid

Private Sub ReadReplace()
Dim ls_Code As String
Dim ls_Modif As String
Dim li_conc As Integer
Dim ls_Lang As String
Dim li_pos As Integer
Dim li_NbChar As Integer
Dim ls_Read As String
Dim ls_Begin As String

On Error GoTo Err_ReadGrid

C_main.Grid.Row = mi_Ligne
Select Case gs_TableName
Case "BI_Marketing"
    C_main.Grid.Col = 0
    ls_Code = C_main.Grid.Text
    C_main.Grid.Col = 1
    li_conc = CInt(C_main.Grid.Text)
    C_main.Grid.Col = 2
    ls_Read = C_main.Grid.Text
Case "Test_Market"
    C_main.Grid.Col = 0
    ls_Code = C_main.Grid.Text
    C_main.Grid.Col = 1
    li_conc = CInt(C_main.Grid.Text)
    C_main.Grid.Col = 4
    ls_Read = C_main.Grid.Text
Case "Value_Type"
    C_main.Grid.Col = 0
    ls_Code = C_main.Grid.Text
    C_main.Grid.Col = 1
    li_conc = CInt(C_main.Grid.Text)
    C_main.Grid.Col = 6
    ls_Read = C_main.Grid.Text
End Select

li_pos = InStr(ls_Read, txt_search)
li_NbChar = Len(txt_search)
If li_pos = 0 Then Exit Sub

mi_Nbfind = mi_Nbfind + 1
If li_pos > 1 Then
    ls_Begin = Mid(ls_Read, 1, li_pos - 1)
Else
    ls_Begin = ""
End If
ls_Modif = ls_Begin & txt_Replace & Mid(ls_Read, li_pos + li_NbChar)
TranslationReplace ls_Code, ls_Modif, li_conc, gut_LangWork.Code
Exit Sub

Err_ReadGrid:
StdError
End Sub
Private Sub TranslationReplace(ByVal ls_Code As String, ByVal ls_Desc As String, ByVal li_conc As Integer, ByVal ls_Lang As String)
Dim ls_req As String
Dim ll_Statement As Long
Dim li_Status As Integer
Dim ll_lngrows As Long

On Error GoTo Err_TransUpdate

Select Case gs_TableName
Case "BI_Marketing"
    ls_req = "EXEC BI_Marketing_t_upd '" & ls_Code & "','" & QuoteParam(ls_Desc) & "'," _
    & li_conc & ",'" & ls_Lang & "'"
Case "Test_Market"
    ls_req = "EXEC Test_Market_t_upd " & ls_Code & ",'" & gut_LangWork.Code & "', '" & QuoteParam(ls_Desc) & "'," & li_conc
Case "Value_Type"
    ls_req = "EXEC Value_Type_t_upd " & ls_Code & ",'" & QuoteParam(ls_Desc) & "'," & li_conc & " ,'" & gut_LangWork.Code & "'"
End Select
    
If SQLSubmit(gl_Environment, gl_Database, ll_Statement, ls_req) Then
    li_Status = SQLRowCount(ll_Statement, ll_lngrows)
    li_Status = SQLFreeStatement(ll_Statement, SQL_DROP)
    If ll_lngrows = 0 Then
        SendMessage 2, "Another user change the record", gut_LangLogin.Code
    Else
        C_main.Grid2.Row = mi_Ligne
        C_main.Grid.Row = mi_Ligne
        Select Case gs_TableName
        Case "Test_Market"
            C_main.Grid2.Col = 1
            C_main.Grid2.Text = ls_Desc
            
            C_main.Grid.Col = 4
            C_main.Grid.Text = ls_Desc
            C_main.Grid.Col = 1
            C_main.Grid.Text = li_conc + 1
        Case "Value_Type"
            C_main.Grid2.Col = 2
            C_main.Grid2.Text = ls_Desc
            
            C_main.Grid.Col = 6
            C_main.Grid.Text = ls_Desc
            C_main.Grid.Col = 1
            C_main.Grid.Text = li_conc + 1
        Case Else
            C_main.Grid2.Col = 1
            C_main.Grid2.Text = ls_Desc
            
            C_main.Grid.Col = 2
            C_main.Grid.Text = ls_Desc
            C_main.Grid.Col = 1
            C_main.Grid.Text = li_conc + 1
        End Select
        mi_NbUpd = mi_NbUpd + 1
    End If
Else
    li_Status = SQLFreeStatement(ll_Statement, SQL_DROP)
    SendMessage 3, "Update failed", gut_LangLogin.Code
End If
Exit Sub

Err_TransUpdate:
Select Case Err
Case 30015
    C_main.FillGrid
Case Else
    li_Status = SQLFreeStatement(ll_Statement, SQL_DROP)
    StdError
End Select
End Sub
Private Sub AllowReplace()
If C_main.pic_upd.Visible = KO Then Exit Sub

Height = 2500
lbl_Replace.Visible = OK
txt_Replace.Visible = OK
cmd_Replace.Visible = OK
cmd_ReplaceAll.Visible = OK
End Sub
Private Sub csts_search()

Dim ls_Field As String
Dim ls_Text As String
Dim ls_req As String
Dim ll_Statement As Long
Dim li_Status As Integer

On Error GoTo suite

ls_req = "EXEC Screen_Csts 'search','" & gut_LangLogin.Code & "'"
If SQLSubmit(gl_Environment, gl_Database, ll_Statement, ls_req) Then
    li_Status = SQL_SUCCESS
Else
    li_Status = SQL_ERROR
End If
Do While li_Status = SQL_SUCCESS Or li_Status = SQL_SUCCESS_WITH_INFO
    li_Status = SQLFetch(ll_Statement)
    If li_Status = SQL_SUCCESS Or li_Status = SQL_SUCCESS_WITH_INFO Then
        ls_Field = ODBCData(ll_Statement, 1)
        ls_Text = Left(ODBCData(ll_Statement, 2), 30)
        Select Case ls_Field
        Case "title": Caption = ls_Text
        Case "criteria": lbl.Caption = ls_Text
        Case "Next": cmd_next.Caption = ls_Text
        Case "cancel": cmd_cancel.Caption = ls_Text
        End Select
    End If
Loop
li_Status = SQLFreeStatement(ll_Statement, SQL_DROP)
Exit Sub

suite:
li_Status = SQLFreeStatement(ll_Statement, SQL_DROP)
StdError
End Sub
Private Sub cmd_Cancel_Click()

success = 1
Unload Me

End Sub
Private Sub cmd_next_Click()
Dim i As Integer

On Error GoTo suite

mi_Ligne = 0
Select Case gi_SearchGrid
Case 1
    If txt_search = "" Then
        Exit Sub
    Else
        For y = prv_y To (mi_NbRows - 1)
            C_main.Grid2.Row = y
            For x = prv_x To (mi_NbCols - 1)
                C_main.Grid2.Col = x
                z = InStr(1, C_main.Grid2.Text, txt_search, 1)
                If z <> 0 Then
                    find_ok = -1
                    C_main.Grid2.HighLight = flexHighlightAlways
'                    C_main.Grid2.Row = Y
'                    C_main.Grid2.Col = X
                    GoTo fin1
                End If
            Next
            prv_x = 0
        Next
        mb_LastRecord = OK
        If find_ok = 0 Then
            SendMessage 10, "Cannot find matching data", gut_LangLogin.Code
            MouseOn
        End If
        Unload Me
        Exit Sub
    End If
fin1:
    mi_Ligne = y
    prv_x = x + 1
    If prv_x > (mi_NbCols - 1) Then
        prv_x = 0
        prv_y = y + 1
    Else
        prv_y = y
    End If
    'Last record detected
    If prv_x = (mi_NbCols) And prv_y = (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If prv_y > (mi_NbRows) Or y > (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If y < toprow_max Then
        C_main.Grid2.TopRow = y
    Else
        C_main.Grid2.TopRow = toprow_max
    End If
    Exit Sub
Case 2
    If txt_search = "" Then
        Exit Sub
    Else
        For y = prv_y To (mi_NbRows - 1)
            frm_main_mc.Grid.Row = y
            For x = prv_x To (mi_NbCols - 1)
                frm_main_mc.Grid.Col = x
                z = InStr(1, frm_main_mc.Grid.Text, txt_search, 1)
                If z <> 0 Then
                    find_ok = -1
                    frm_main_mc.Grid.HighLight = flexHighlightAlways
                    frm_main_mc.Grid.Row = y
                    frm_main_mc.Grid.Col = x
                    success = 1
                    GoTo fin2
                End If
            Next
            prv_x = 0
        Next
        If find_ok = 0 Then
            SendMessage 10, "Cannot find matching data", gut_LangLogin.Code
            MouseOn
        End If
        Unload Me
        Exit Sub
    End If
fin2:
    prv_x = x + 1
    If prv_x > (mi_NbCols - 1) Then
        prv_x = 0
        prv_y = y + 1
    Else
        prv_y = y
    End If
    'Last record detected
    If prv_x = (mi_NbCols) And prv_y = (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If prv_y > (mi_NbRows) Or y > (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If y < toprow_max Then
        frm_main_mc.Grid.TopRow = y
    Else
        frm_main_mc.Grid.TopRow = toprow_max
    End If
    Exit Sub
Case 3
    If txt_search = "" Then
        Exit Sub
    Else
        For y = prv_y To (mi_NbRows - 1)
            frm_main_cwpr.Grid.Row = y
            For x = prv_x To (mi_NbCols - 1)
                frm_main_cwpr.Grid.Col = x
                z = InStr(1, frm_main_cwpr.Grid.Text, txt_search, 1)
                If z <> 0 Then
                    find_ok = -1
                    frm_main_cwpr.Grid.Row = y
                    frm_main_cwpr.Grid.Col = x
                    frm_main_cwpr.Grid.HighLight = flexHighlightAlways        '-1
                    success = 1
                    GoTo fin3
                End If
            Next
            prv_x = 0
        Next
        If find_ok = 0 Then
            SendMessage 10, "Cannot find matching data", gut_LangLogin.Code
            MouseOn
        End If
        Unload Me
        Exit Sub
    End If
fin3:
    prv_x = x + 1
    If prv_x > (mi_NbCols - 1) Then
        prv_x = 0
        prv_y = y + 1
    Else
        prv_y = y
    End If
    'Last record detected
    If prv_x = (mi_NbCols) And prv_y = (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If prv_y > (mi_NbRows) Or y > (mi_NbRows) Then
        Unload Me
        Exit Sub
    End If
    If y < toprow_max Then
        frm_main_cwpr.Grid.TopRow = y
    Else
        frm_main_cwpr.Grid.TopRow = toprow_max
    End If
    Exit Sub
Case 4
    If txt_search = "" Then Exit Sub
    
    Debug.Assert (Not mo_grid4 Is Nothing)
    
    If mo_grid4.SearchText(False, txt_search.Text, True) = -1 Then
        SendMessage 10, "Cannot find matching data", gut_LangLogin.Code
        MouseOn
        Unload Me
        Exit Sub
    End If
    
    Exit Sub
End Select

suite:
StdError
End Sub

Private Sub cmd_Replace_Click()

On Error GoTo Err_Replace

MouseOff
If mb_LastRecord = OK Then Exit Sub
If mi_Ligne = 0 Then cmd_next_Click
If find_ok = 0 Then Exit Sub

ReadReplace

cmd_next_Click
MouseOn
Exit Sub

Err_Replace:
MouseOn
StdError
End Sub

Private Sub cmd_ReplaceAll_Click()
Dim i As Integer
Dim li_pos As Integer
Dim li_NbChar As Integer
Dim ls_Begin As String
Dim ls_Read As String

On Error GoTo Err_ReplaceAll

MouseOff
mi_Ligne = 0
For i = 1 To C_main.Grid.Rows - 1
    mi_Ligne = i
    ReadReplace
Next i
MouseOn

If mi_Nbfind > 0 Then MsgBox "Record(s) updated : " & mi_NbUpd & "/" & mi_Nbfind
Unload Me
Exit Sub

Err_ReplaceAll:
MouseOn
StdError
End Sub

Private Sub Form_Load()

MouseOff

If gl_CodePage <> 1252 And gl_CodePage <> 0 Then ChangeCharset Me

Height = 1150
find_ok = 0
success = 0
csts_search
mi_Ligne = 0
mb_LastRecord = KO
mi_Nbfind = 0
mi_NbUpd = 0

Select Case gi_SearchGrid
Case 1
    C_main.Grid2.HighLight = flexHighlightNever
    C_main.Grid2.Row = 0
    C_main.Grid2.Col = 0
    
    mi_NbRows = C_main.Grid2.Rows
    mi_NbCols = C_main.Grid2.Cols
Case 2
    frm_main_mc.Grid.HighLight = flexHighlightNever
    mi_NbRows = frm_main_mc.Grid.Rows
    mi_NbCols = frm_main_mc.Grid.Cols
Case 3
    frm_main_cwpr.Grid.HighLight = flexHighlightNever
    mi_NbRows = frm_main_cwpr.Grid.Rows
    mi_NbCols = frm_main_cwpr.Grid.Cols
End Select
prv_y = 1
toprow_max = mi_NbRows - 17
If toprow_max < 1 Then
    toprow_max = 1
End If

Select Case gs_TableName
Case "BI_Marketing": AllowReplace
Case "Test_Market":
    If gut_LangWork.Code <> "E" Then AllowReplace
Case "Value_Type":
    If gut_LangWork.Code <> "E" Then AllowReplace
End Select

MouseOn

End Sub
Private Sub Form_Unload(Cancel As Integer)

Select Case gi_SearchGrid
Case 1
    If success = 1 And find_ok = -1 Then
        C_main.Grid2.HighLight = flexHighlightAlways
        C_main.Grid.Row = C_main.Grid2.Row
        C_main.Grid_Click
    Else
        C_main.Grid2.HighLight = flexHighlightNever
        C_main.Grid2.Row = 0
        C_main.Grid2.Col = 0
        gs_TableCode = ""
    End If
Case 2
    If success = 1 And find_ok = -1 Then
        frm_main_mc.Grid.HighLight = flexHighlightAlways
        gs_TableCode = frm_main_mc.cbo_MediaCode.List(frm_main_mc.Grid.Row - 1)
        frm_main_mc.Grid_Click
    Else
        frm_main_mc.Grid.HighLight = flexHighlightNever
        gs_TableCode = ""
    End If
Case 3
    If success = 1 And find_ok = -1 Then
        frm_main_cwpr.Grid.HighLight = flexHighlightAlways
        
        gs_TableCode = frm_main_cwpr.cbo_CodeProd.List(frm_main_cwpr.Grid.Row - 1)
        frm_main_cwpr.Grid_Click
    Else
        frm_main_cwpr.Grid.HighLight = flexHighlightNever
        gs_TableCode = ""
    End If
End Select

End Sub

