Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   suma de campos de acuerdo a la fecha (https://www.clubdelphi.com/foros/showthread.php?t=75194)

lisc_dla 04-08-2011 02:20:11

suma de campos de acuerdo a la fecha
 
en mi aplicación con base de datos estoy haciendo una búsqueda de productos vendidos y una suma de las ventas dependiendo de la fecha que se seleccione en el DateTimePicker, para no complicarme al momento de seleccionar la fecha esta se pasa a un label(lbl_fecha), la busqueda la se realiza perfectamente pero la suma no me la realiza es decir el cuadro de texto(txt_total) donde se debe de visualizar aparece en 0.
para la conexion utilizo un ADOConnection, adoquery, ADOTable, DataSource, DBgrid
el código es:
Código Delphi [-]
function SumarTotales(Grid: TDBGrid; const AFieldName: string): Currency;
var
  BM: TBookMarkStr;
begin
  Result:= 0;
  with Grid.DataSource.DataSet do
  begin
    BM:= Bookmark;
    DisableControls;
    while not Eof do
    begin
      Result:= Result + FieldByName(AFieldName).AsCurrency;
      Next;
    end;
    BookMark:= BM;
    EnableControls;
  end;
end;

procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
var
   Filtro : String;
begin
  lbl_fecha.Caption:=DateToStr(DateTimePicker1.date);
  adotable1.Active:=true;
  if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = false then
  begin
   // si no encuentra productos no hace nada
  end 
  else
  begin
    AdoTable1.Filtered := False;
    AdoTable1.Filtered := False;
    Filtro := 'fecha = '+lbl_fecha.Caption;
    AdoTable1.Filter := Filtro;
    AdoTable1.Filtered := True;
    AdoTable1.Open;
   //en caso de encontrar productos vendidos realiza la suma
    AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                         ' where fecha = '+lbl_fecha.Caption;
   AdoQuery1.Active := true;
   txt_total.Text := IntToStr(AdoQuery1.Fields[0].AsInteger);
  end;
end;

Caral 04-08-2011 02:32:48

Hola
Te encanta complicarte la vida.:D
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '
 AdoQuery1.Parameters[0].Value:=  DateToStr(DateTimePicker1.date);
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
Saludos

lisc_dla 04-08-2011 02:41:33

me marca error Missing operator or semicolon

en esta linea
AdoQuery1.Parameters[0].Value:= DateToStr(DateTimePicker1.date);

Caral 04-08-2011 02:44:37

Hola
Pues es por eso, por que te falta un semicolon.
En ESpanglich punto y coma......
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '; // aqui esta tu semicolon
 AdoQuery1.Parameters[0].Value:=  DateToStr(DateTimePicker1.date);
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
Anda que te lo damos todo masticado......:D:p
Saludos

lisc_dla 04-08-2011 02:50:43

ya puse el ; pero no me hace la suma ahora el text me aparece vació :(:(:(

Caral 04-08-2011 02:53:17

Hola
No te marca error pero te aparece vacío, verdad ?????
Por que sera ??.
Piensa, piensa......
Saludos
PD: Yo lo se y estoy seguro que tu también puedes saberlo.

lisc_dla 04-08-2011 02:58:46

No pos no :o:o:o:o

Caral 04-08-2011 03:03:30

Hola
Te voy a dar pistas:
Haces un locate del adotable a un label.
Es correcto el dato de la fecha en comparacion con el dato que hay en la BD ?.
La sistaxis del label (caption) es correcta ?.
Estas seguro que pasa lo mismo con el Datetimepicker ?.
MySql acepta el string antes ?.
As colocado un showmessage() para verificar el dato ?.
Saludos

lisc_dla 04-08-2011 03:14:17

hice esto:

if Adotable1.Locate('fecha',DateToStr(DateTimePicker1.date),[]) = True then

pero el text sigue apareciendo vacio

Caral 04-08-2011 03:21:07

Hola
Estoy seguro que el problema esta en el filtro, prueba esto:
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 Showmessage(AdoTable1.Fields[0].AsString); // aqui revisas que informacion te trae
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '; // aqui esta tu semicolon
 AdoQuery1.Parameters[0].Value:=  DateToStr(DateTimePicker1.date);
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
O esto:
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 {adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 Showmessage(AdoTable1.Fields[0].AsString); // aqui revisas que informacion te trae }
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '; // aqui esta tu semicolon
 AdoQuery1.Parameters[0].Value:=  DateToStr(DateTimePicker1.date);
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
Me cuentas como te va.
Saludos

lisc_dla 04-08-2011 03:24:10

en el primero el mensaje sale 1 pero sigue apareciendo en blanco el cuadro de texto :(:(:(

Caral 04-08-2011 03:26:41

Hola
Me quieres volver loco verdad ?????:D:D:D
Que muestra el mensaje ?.
Probaste los dos codigos ?
Saludos

Caral 04-08-2011 03:35:06

Hola
Osea, el numero 1 es el primer registro que envia.
Trata esto y me dices que envia el mensaje:
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 Showmessage(Filtro); // aqui revisas que informacion te trae
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '; // aqui esta tu semicolon
 AdoQuery1.Parameters[0].Value:=  DateToStr(DateTimePicker1.date);
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
No se que mostrara, ya me lo diras.
Saludos

lisc_dla 04-08-2011 03:38:24

me manda el mensaje con la fecha que se selecciona pero en el text no quiere :o:o:o:o

Caral 04-08-2011 03:41:51

Hola
Eso me dice que es posible que la fecha no la lea del datetimepicker ya que la lee como string, me parece.
Cambia esto a ver si envia un error:
Código Delphi [-]
procedure Tfrm_cortedecaja.DateTimePicker1Change(Sender: TObject);
 var
 Filtro : String;
 begin
 adotable1.Active:=true;
 if Adotable1.Locate('fecha',lbl_fecha.Caption,[]) = True then
 begin
 AdoTable1.Filtered := False;
 Filtro := 'fecha = '+DateToStr(DateTimePicker1.date);
 AdoTable1.Filter := Filtro;
 AdoTable1.Filtered := True;
 AdoTable1.Open;
 Showmessage(Filtro); // aqui revisas que informacion te trae
 //en caso de encontrar productos vendidos realiza la suma
 AdoQuery1.SQL.Text := 'Select sum(importe) as total from caja '+
                       ' where fecha = :Fec '; // aqui esta tu semicolon
 AdoQuery1.Parameters[0].Value:=  Filtro;
 AdoQuery1.Active := true;
 txt_total.Text := AdoQuery1.Fields[0].AsString;
 end;
end;
Saludos

lisc_dla 04-08-2011 03:44:11

sigue mandando el mensaje con la fecha, ningún error

Caral 04-08-2011 03:45:11

Hola
No lleno el text con nada ?.
Saludos

lisc_dla 04-08-2011 03:46:25

NO el text sigue en blanco :o

Caral 04-08-2011 03:49:43

Hola
Estas completamente seguro que CAJA es la tabla que contiene la fecha ?.
Si no envia ningun error el sql esta bien, el problema es o que no existe el dato o que no es la misma tabla.
Algo esta pasando, no entiendo.
Que ganas de estar frente a tu ordenador....
Saludos

lisc_dla 04-08-2011 03:54:25

es la misma tabla, de hecho si me salio la suma solo que no ocupaba la fecha, ocupaba otro campo.

el campo de fecha en mysql es de tipo date

si se visualiza la búsqueda en el dbgrid pero la suma es la que no funciona :confused::confused:


La franja horaria es GMT +2. Ahora son las 15:33:54.

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