Buscar

Empty - validando se a variável ou campo está vazio

Código

Essa função salva muitos programadores de 'longos' códigos, pois ela valida uma variável, campo, ou seja lah o que for.

Retorna FALSE se naum houver conteúdo ou TRUE casa haja valor.

Nota: valida também datas, inclusive a data maluca do DELPHI.

{*****
*   Empty( xValue );
*   Retorna True se a variável está vazia.
*
*   Autor: Jociel Eloy de Almeida
*}


function Empty(xValue: Variant): Boolean;
var
  sTmp: String;
  iDia, iMes, iAno: Word;
begin
  {Pré-Definição do Retorno}
  Result := False;

  if xValue = Null then begin
    Result := True;
    Exit;
  end;

  if VarType(xValue) = VarEmpty then
    Result := True;

  if VarType(xValue) = varNull then
    Result := True;

  if VarType(xValue) = varSmallInt then
    Result := (xValue = 0);

  if VarType(xValue) = varInteger then
    Result := (xValue = 0);

  if VarType(xValue) = varSingle then
    Result := (xValue = 0);

  if VarType(xValue) = varDouble then
    Result := (xValue = 0);

  if VarType(xValue) = varCurrency then
    Result := (xValue = 0);

  if VarType(xValue) = varDate then begin
    Result := (xValue = Null);
    if not Result then begin
      DecodeDate(xValue,iAno,iMes,iDia);
      if ((iDia = 30) and (iMes = 12) and (iAno = 1899)) then
        Result := True;
    end;

    if not Result then begin
      sTmp := DateToStr(xValue);
      sTmp := StrTran(sTmp,'/','');
      sTmp := StrTran(sTmp,'-','');
      sTmp := StrTran(sTmp,'.','');
      Result := Empty(sTmp);
    end;
  end;
 
  if VarType(xValue) = varString then
    Result := (Len(StrTran(xValue, ' ', '')) = 0);

  if VarType(xValue) = varOleStr then
    Result := (xValue = Null);

  if VarType(xValue) = varDispatch then
    Result := (xValue = Null);

  if VarType(xValue) = varByte then
    Result := (xValue = Null);

end;

Publicidade

Vote na dica




Quantidade de votos: 1 voto
Aceitação: 20%


Detalhes da dica

Categoria: Banco de dados
Adicionada dia: 16/11/04
Por: Jociel Eloy De Almeida
Visualizada: 22118 vezes

Planeta Delphi - Tudo sobre programação Delphi Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009