;** AC_RTRIM *******************************************************************
;*  Rotina p/ remover espa‡amentos vazios em registros de campos das tabelas   *
;*******************************************************************************
;Autor           : Vise Informatica (VISERP)
;Desenvolvido    : 20/11/2015 por Bruno Carvalho
;Ultima Alteracao:   /  /     por 
;Motivo Alteracao:   

;*******************************************************************************
;* Rotina                                                                      *
;*******************************************************************************
Proc SD_Dialog(Disparo,Campo,Evento,Elemento)
Temp=True
MSG_CAMPO=""      
TECLA=""
If Disparo="OPEN"  Then AoCriarShowDialog() EndIf
If Disparo="EVENT" And Evento["Type"]="KEY" Then
   Switch
     Case Evento["KeyCode"] = 9   : Temp=False
     Case Evento["KeyCode"] = -59 : Dialog_F1()
     Case Evento["KeyCode"] =  27 :
          Temp=False
          Switch
            Case Campo="TG_DIR"            : Campo="NO"
            Case Campo="TG_TAB"            : Campo="TG_DIR"  
            Case Campo="YES" Or Campo="NO" : Campo="TG_DIR"
          EndSwitch
     Case (Evento["KeyCode"] <= -104 And Evento["KeyCode"] >= -113) Or 
           Evento["KeyCode"]  = -139 Or  Evento["KeyCode"]  = -140:
           SYS_TECLA_ACIONADA=Evento["KeyCode"]
           Seta_Tecla_Acionada()
           CancelDialog
     Case Evento["KeyCode"] = 13:
          Temp=False
          If Checa_TAG()
             Then Switch
                    Case Campo="TG_DIR"    : Campo="TG_TAB"
                          VSDGDIR = ControlValue("TG_DIR")
                    Case Campo="TG_TAB" : Campo="YES"
                         VSDGTABELA = ControlValue("TG_TAB")
                  EndSwitch
          EndIf
          RefreshDialog
   EndSwitch
EndIf
Switch
  Case Campo="TG_DIR":
     MSG_CAMPO="Informe o diret¢rio"
  Case Campo="TG_TAB":
     MSG_CAMPO="Informe a tabela p/ a remo‡Æo dos espa‡os em branco nos campos"
Endswitch
Monta_Prompt(MSG_CAMPO,TECLA)
Return Temp
EndProc

;*******************************************************************************
;* ROTINA PRINCIPAL                                                            *
;*******************************************************************************
AutoLib="\\Sistemas\\Cadastro\\Bib_Show,\\Sistemas\\Cadastro\\Bibliot"
LimpAguarde()

ConfGeral("Rotina para remover espa‡amentos vazios em registros de tabelas!//Deseja continuar com este procedimento?")
If Ok<>"S"
   Then Return
EndIf

VSDGTABELA = ""
VSDGDIR    = ""

While True
  ShowDialog "Remove Espa‡amentos" Proc "SD_Dialog"
  Trigger "OPEN","ARRIVE"
  Key "F1","ENTER","ESC","TAB",
      -104, -105, -106, -107, -108, -109, -110, -111, -112, -113, -139, -140  
   @ 05,12   Height 07 Width 56

    Label  @ 00,00 "Diretorio"  For "TG_DIR"
    Accept @ 01,01 Width 11 "A8" Picture "!*!" Tag "TG_DIR"  To VSDGDIR

    Label  @ 00,13 "Tabela"  For "TG_TAB"
    Accept @ 01,14 Width 11 "A8" Picture "!*!" Tag "TG_TAB"  To VSDGTABELA

    Pushbutton @ 03,14 width 11 "~O~k"      OK     Default Value "Ok" Tag "YES" To BottonValue
    Pushbutton @ 03,28 width 11 "~C~ancela" CANCEL                    Tag "NO"  To BottonValue

  EndDialog
  If Not RetVal Then QuitLoop EndIf
  
  If IsBlank(VSDGTABELA) Or IsBlank(VSDGDIR)
     Then Loop
  EndIf
   
  VSDGDIR    = Rtrim(VSDGDIR)
  VSDGTABELA = Rtrim(VSDGTABELA)
   
  DIR = SubStr(Directory(),1,3)
  If DirExists(DIR+"SISTEMAS\\"+Rtrim(VSDGDIR)+"\\")=0
     Then Mensagem("Diret¢rio ou Tabela n„o localizados!")
          Loop
  EndIf   
       
  AUX_DIR = Directory()                           
  SetDir DIR+"SISTEMAS\\"+Rtrim(VSDGDIR)+"\\"                            
  
  If Not IsTable(VSDGTABELA)
     Then SetDir AUX_DIR
          Loop
  EndIf
                             
  {Ask} TypeIn VSDGTABELA Enter Check
  Do_it!
  ClearAll
  
  If Not IsEmpty("Answer")
     Then BACKUP = "BK_"+SubStr(VSDGTABELA,1,3)         
          Copy VSDGTABELA BACKUP
          
          {Tools} {Info} {Structure} {Answer}
          ClearAll
          
          STRUCT = NRecords("Struct")
          NR = NRecords("Answer")
          
          Coedit "Answer"
          Scan 
            Barra(NR, RecNo())
            Aguarde("Aguarde, removendo espa‡os em branco dos registros do(a) "+VSDGTABELA+" "+BARRA_STRING)
            CtrlHome
            For i From 1 To STRUCT+1
              If i = 1 
                 Then Right
                      Loop
              EndIf 
              AUX = Rtrim([])
              If Len(AUX)=0
                 Then AUX = ""
              EndIf   
              CtrlBackSpace
              If Not IsBlank(AUX) 
                 Then Switch
                        Case Type([])="D"             : AUX = DateVal(AUX) 
                        Case SubStr(Type([]),1,1)="A" :
                        OtherWise                     : AUX = NumVal(AUX)
                     EndSwitch   
                     []=AUX
              EndIf   
              If i <> STRUCT+1
                 Then Right
              EndIf   
            EndFor
          EndScan
          Do_It!
          CleaRAll
          
          ;{Tools} {Copy} {JustFamily} TypeIn DIR+"BASE\\SISTEMAS\\"+Rtrim(VSDGDIR)+"\\"+VSDGTABELA Enter {Answer} {Replace}
          ;Copy "Answer" VSDGTABELA
          
          Empty VSDGTABELA
          Add "Answer" VSDGTABELA
  EndIf

  SetDir AUX_DIR
  Mensagem("Processo Finalizado!//Backup Gerado: "+BACKUP)
EndWhile
ClearAll
Release Vars All
Release Procs All