REVELAR

Fórum Como eu poderia parametrizar consultas com CDS ? #325320

13/07/2006

0

Olá amigos, quero fazer uma função para validação de cadastros em todo o sistema, onde passo o índice da função, o componente que tem o conteúdo e o label para apresentar o resultado da validação.

Algumas partes consigo fazer, só que a classe TClientDataSet não tem o CommandText, FieldByName e ParamByName.

A função ficaria assim:

function ValidaCadastro(nCadastro: integer; VarCodigo: Currency; NomeEdit: TCurrencyEdit; NomeLabel: TLabel): boolean;
var
  cdsTemp: TClientDataSet;
  vNT,vCC,vCR: string;
begin
   Result:=False;
   if (TeclaPressionada(VK_RETURN) or TeclaPressionada(VK_DOWN)) then
      begin
         try
            if VarCodigo = 0 then
               case nCadastro of
                  1:    VarCodigo := ChamaListaCod(6,´codmar´);
                  2:    VarCodigo := ChamaListaCod(4,´codgru´);
                  3:    VarCodigo := ChamaListaCod(3,´codvol´);
                  4:    VarCodigo := ChamaListaCod(5,´codsgru´);
               end;
            if VarCodigo <> 0 then
               begin
                  case nCadastro of
                     1:    begin vNT := ´marcas´   ; vCC := ´codmar´    ; vCR := ´nommar´    ; end;
                     2:    begin vNT := ´grupos´   ; vCC := ´codgru´    ; vCR := ´nomgru´    ; end;
                     3:    begin vNT := ´volumes´  ; vCC := ´volumes´   ; vCR := ´nomvol´    ; end;
                     4:    begin vNT := ´sgrupos´  ; vCC := ´sgrupos´   ; vCR := ´nomsgru´   ; end;
                  end;
                  cdsTemp := TClientDataSet( DM.FindComponent(´cds_´+vNT ));
                  cdstemp.Close ;
                  cdsTemp := TClientDataSet( DM.FindComponent(´sql_´+vNT ));
                  case nCadastro of
                     1:    cdsTemp.CommandText := ´select codmar,nommar   from Marcas  where CodEmp=:CodEmp AND codmar=:codmar´ ;
                     2:    cdsTemp.CommandText := ´select codgru,nomgru   from Grupos  where CodEmp=:CodEmp AND codgru=:codgru´ ;
                     3:    cdsTemp.CommandText := ´select codvol,nomvol   from Volumes where CodEmp=:CodEmp AND codvol=:codvol´ ;
                     4:    cdsTemp.CommandText := ´select codsgru,nomsgru from sGrupos where CodEmp=:CodEmp AND codsgru=:codsgru´ ;
                  end;
                  cdsTemp := TClientDataSet( DM.FindComponent(´sql_´+vNT ));
                  cdstemp.Params.ParamByName(´CodEmp´).AsInteger  := fMenu.CodEmpSelec ;
                  cdstemp.Params.ParamByName(vCC).AsCurrency := VarCodigo;
                  cdsTemp := TClientDataSet( DM.FindComponent(´cds_´+vNT ));
                  cdstemp.open ;
                  if (not cdstemp.eof) then
                     Result:=True;
               end;
         finally
            NomeEdit.Value    := cdstemp.FieldByName(vCC).asCurrency;
            NomeLabel.Caption := cdstemp.FieldByName(vCR).asstring ;
         end;
         if not Result then
            NomeEdit.SetFocus ;
      end;
end;


Aguardo ajuda

Mario


Aldus

Aldus

Responder

Posts

14/07/2006

Aldus

Alguma idéia?


Responder

Gostei + 0

14/07/2006

Aldus

Resolvido pessoal! Inclui no Uses ´SqlExpr´, e a função ficou assim:

function ValidaCadastro(nCadastro: integer; VarCodigo: Currency; NomeEdit: TCurrencyEdit; NomeLabel: TLabel; MostraRet: boolean = True): boolean;
var
  cdsTemp: TClientDataSet;
  sqlTemp: TSQLDataSet;
  vNT,vCC,vCR: string;
begin
   Result:=False;
   if (TeclaPressionada(VK_RETURN) or TeclaPressionada(VK_DOWN)) then
      begin
         try
            if VarCodigo = 0 then
               case nCadastro of
                  1:    VarCodigo := ChamaListaCod(6,´codmar´);
                  2:    VarCodigo := ChamaListaCod(4,´codgru´);
                  3:    VarCodigo := ChamaListaCod(3,´codvol´);
                  4:    VarCodigo := ChamaListaCod(5,´codsgru´);
               end;
            if VarCodigo <> 0 then
               begin
                  case nCadastro of
                     1:    begin vNT := ´marcas´   ; vCC := ´codmar´    ; vCR := ´nommar´    ; end;
                     2:    begin vNT := ´grupos´   ; vCC := ´codgru´    ; vCR := ´nomgru´    ; end;
                     3:    begin vNT := ´volumes´  ; vCC := ´volumes´   ; vCR := ´nomvol´    ; end;
                     4:    begin vNT := ´sgrupos´  ; vCC := ´sgrupos´   ; vCR := ´nomsgru´   ; end;
                  end;
                  cdsTemp := TClientDataSet( DM.FindComponent(´cds_´+vNT ));
                  cdstemp.Close ;
                  sqlTemp := TSQLDataSet( DM.FindComponent(´sql_´+vNT ));
                  case nCadastro of
                     1:    sqlTemp.CommandText := ´select codmar,nommar   from Marcas  where CodEmp=:CodEmp AND codmar=:codmar´ ;
                     2:    sqlTemp.CommandText := ´select codgru,nomgru   from Grupos  where CodEmp=:CodEmp AND codgru=:codgru´ ;
                     3:    sqlTemp.CommandText := ´select codvol,nomvol   from Volumes where CodEmp=:CodEmp AND codvol=:codvol´ ;
                     4:    sqlTemp.CommandText := ´select codsgru,nomsgru from sGrupos where CodEmp=:CodEmp AND codsgru=:codsgru´ ;
                  end;
                  sqlTemp := TSQLDataSet( DM.FindComponent(´sql_´+vNT ));
                  sqltemp.ParamByName(´CodEmp´).AsInteger  := fMenu.CodEmpSelec ;
                  sqltemp.ParamByName(vCC).AsCurrency := VarCodigo;
                  cdsTemp := TClientDataSet( DM.FindComponent(´cds_´+vNT ));
                  cdstemp.open ;
                  if (not cdstemp.eof) then Result:=True;
               end;
         finally
            NomeEdit.Value := cdstemp.FieldByName(vCC).asCurrency;
            if MostraRet then NomeLabel.Caption := cdstemp.FieldByName(vCR).asstring ;
         end;
         if not Result then
            NomeEdit.SetFocus ;
      end;
end;


Até


Responder

Gostei + 0

14/07/2006

Aldus

Esqueci a chamada a função:



ValidaCadastro(2,EditCodGru.Value,EditCodGru,LabNomGru);



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar