Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Tablas planas (https://www.clubdelphi.com/foros/forumdisplay.php?f=20)
-   -   Consulta con campos devueltos NULL (https://www.clubdelphi.com/foros/showthread.php?t=47859)

FGarcia 09-09-2007 20:42:57

Consulta con campos devueltos NULL
 
Hola!

Esta consulta se realiza cada hora mediante un timer o tambien se puede hacer mediante un boton pasandole los parametros iHora y eHora

Código Delphi [-]
procedure TfrmMain.ActualizaCadaHora(iHora: string; eHora: string) ;
var
  estaHora: Tdatetime;
  consultaVacia: Boolean;
begin
  consultaVacia := False;
  with QyHora do
    begin
      Close ;
      SQL.Clear ;
      SQL.Add('SELECT COUNT(*) AS HSacos, ' +
                'SUM (Peso) AS [HTotal] ' +
                'FROM Captura ' +
                'WHERE (HoraFecha BETWEEN :FIni AND :FFin)');
      Parameters.ParamByName('FIni').Value := iHora;
      Parameters.ParamByName('FFin').Value := eHora;
      Open ;
      if (QyHora.RecordCount = 0) then
        consultaVacia := True;
    end;
  if not consultaVacia then
    begin
      //Convierto la cadena iHora a un formato de datetime
      estaHora := StrToDateTime(iHora);
      DecodeTime(estaHora, miHora,miMin,miSeg,miMseg);
      case miHora of
      0:  begin
            StringGrid1.Cells[1,1] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,1] := QyHora.FieldValues['HTotal'];
          end;
      1:  begin
            StringGrid1.Cells[1,2] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,2] := QyHora.FieldValues['HTotal'];
          end;
      2:  begin
            StringGrid1.Cells[1,3] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,3] := QyHora.FieldValues['HTotal'];
          end;
      3:  begin
            StringGrid1.Cells[1,4] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,4] := QyHora.FieldValues['HTotal'];
          end;
      4:  begin
            StringGrid1.Cells[1,5] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,5] := QyHora.FieldValues['HTotal'];
          end;
      5:  begin
            StringGrid1.Cells[1,6] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,6] := QyHora.FieldValues['HTotal'];
          end;
      6:  begin
            StringGrid1.Cells[1,7] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,7] := QyHora.FieldValues['HTotal'];
            Stringgrid1.TopRow := 7;
          end;
      7:  begin
            StringGrid1.Cells[1,8] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,8] := QyHora.FieldValues['HTotal'];
          end;
      8:  begin
            StringGrid1.Cells[1,9] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,9] := QyHora.FieldValues['HTotal'];
          end;
      9:  begin
            StringGrid1.Cells[1,10] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,10] := QyHora.FieldValues['HTotal'];
          end;
      10: begin
            StringGrid1.Cells[1,11] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,11] := QyHora.FieldValues['HTotal'];
          end;
      11: begin
            StringGrid1.Cells[1,12] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,12] := QyHora.FieldValues['HTotal'];
          end;
      12: begin
            StringGrid1.Cells[1,13] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,13] := QyHora.FieldValues['HTotal'];
          end;
      13: begin
            StringGrid1.Cells[1,14] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,14] := QyHora.FieldValues['HTotal'];
          end;
      14: begin
            StringGrid1.Cells[1,15] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,15] := QyHora.FieldValues['HTotal'];
            Stringgrid1.TopRow := 15;
          end;
      15: begin
            StringGrid1.Cells[1,16] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,16] := QyHora.FieldValues['HTotal'];
          end;
      16: begin
            StringGrid1.Cells[1,17] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,17] := QyHora.FieldValues['HTotal'];
          end;
      17: begin
            StringGrid1.Cells[1,18] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,18] := QyHora.FieldValues['HTotal'];
          end;
      18: begin
            StringGrid1.Cells[1,19] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,19] := QyHora.FieldValues['HTotal'];
          end;
      19: begin
            StringGrid1.Cells[1,20] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,20] := QyHora.FieldValues['HTotal'];
          end;
      20: begin
            StringGrid1.Cells[1,21] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,21] := QyHora.FieldValues['HTotal'];
          end;
      21: begin
            StringGrid1.Cells[1,22] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,22] := QyHora.FieldValues['HTotal'];
          end;
      22: begin
            StringGrid1.Cells[1,23] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,23] := QyHora.FieldValues['HTotal'];
            Stringgrid1.TopRow := 23;
          end;
      23: begin
            StringGrid1.Cells[1,24] := QyHora.FieldValues['HSacos'];
            StringGrid1.Cells[2,24] := QyHora.FieldValues['HTotal'];
          end;
      end;
    end;
end;

El problema radica en que si dentro de el periodo comprendido entre iHora y eHora no hay ningun registro el campo HTotal es null por lo tanto cuando quiero asignar el valor correspondiente a la rejilla (StringGrid1) me devuelve un error de "Cast"

---------------------------
Jugoso
---------------------------
Could not convert variant of type (Null) into type (String)
---------------------------
Aceptar
---------------------------

Estuve buscando en el foro y no encontre alguna respuesta para ACCESS :( ya se que no les gusta access pero ... a ver si alguien me ayuda.

Saludos!

eduarcol 09-09-2007 20:52:45

Hola se que esta no es la solucion, pero porq en lugar de stringgrid no usas un dbgrid, te ahorrarias mas trabajo

FGarcia 09-09-2007 21:11:02

Si eso lo se, lo que pasa es que estoy tabulando por horas y quiero saber los datos en cada hora

____________________
Hora | Sacos | Total |
____________________
10 - 11 | 1550 | 9876 |
____________________
11 - 12 | 2340 | 10657|
____________________
12 - 13 | 0 | 0|
____________________
13 - 14 | 12| 480|
____________________

Usar un dbgrid para la columna hora seria mas engorroso ¿no crees?

eduarcol 09-09-2007 21:26:14

nop, ese valor se lo puedes dar en el evento ongettext del field hora de la consulta, engorroso es el stringgrid, bueno por lo menos a mi me da comezon utilizarlo

FGarcia 10-09-2007 23:46:12

Bueno se convirtio en capricho usar el stringgrid y la unica solucion que encontre fue esta

Código Delphi [-]
//cree una variable local
esteTotal: string;
 
//antes de asignar los datos al string grid pruebo el valor del campo devuelto HTotal
if QyHora.Fields.FieldByName('HTotal').IsNull  then
        esteTotal := '0'
      else
        esteTotal := QyHora.FieldValues['HTotal'];
 
//Asigno el valor de esteTotal al stringgrid
case of estaHora of
0: begin
       StringGrid1.Cells[1,18] := QyHora.FieldValues['HSacos'];
       StringGrid1.Cells[2,18] := esteTotal;
    end;
.
.
.
.
23: begin
       StringGrid1.Cells[1,18] := QyHora.FieldValues['HSacos'];
       StringGrid1.Cells[2,18] := esteTotal;
     end;
end;

Aun asi, si alguien conoce un modo mas elegante de hacer esto se agradece.


La franja horaria es GMT +2. Ahora son las 14:49:18.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi