Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
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;

Última edición por ecfisa fecha: 04-08-2011 a las 03:42:34. Razón: Etiquetas [DELPHI] [/DELPHI]
Responder Con Cita
  #2  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Te encanta complicarte la vida.
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
__________________
Siempre Novato
Responder Con Cita
  #3  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
me marca error Missing operator or semicolon

en esta linea
AdoQuery1.Parameters[0].Value:= DateToStr(DateTimePicker1.date);
Responder Con Cita
  #4  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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......
Saludos
__________________
Siempre Novato
Responder Con Cita
  #5  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
ya puse el ; pero no me hace la suma ahora el text me aparece vació
Responder Con Cita
  #6  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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.
__________________
Siempre Novato
Responder Con Cita
  #7  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
No pos no
Responder Con Cita
  #8  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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
__________________
Siempre Novato
Responder Con Cita
  #9  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
hice esto:

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

pero el text sigue apareciendo vacio
Responder Con Cita
  #10  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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
__________________
Siempre Novato
Responder Con Cita
  #11  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
en el primero el mensaje sale 1 pero sigue apareciendo en blanco el cuadro de texto

Última edición por lisc_dla fecha: 04-08-2011 a las 04:29:30.
Responder Con Cita
  #12  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
Me quieres volver loco verdad ?????
Que muestra el mensaje ?.
Probaste los dos codigos ?
Saludos
__________________
Siempre Novato
Responder Con Cita
  #13  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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
__________________
Siempre Novato
Responder Con Cita
  #14  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
me manda el mensaje con la fecha que se selecciona pero en el text no quiere
Responder Con Cita
  #15  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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
__________________
Siempre Novato
Responder Con Cita
  #16  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
sigue mandando el mensaje con la fecha, ningún error
Responder Con Cita
  #17  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
Hola
No lleno el text con nada ?.
Saludos
__________________
Siempre Novato
Responder Con Cita
  #18  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
NO el text sigue en blanco
Responder Con Cita
  #19  
Antiguo 04-08-2011
Avatar de Caral
[Caral] Caral is offline
Miembro Premium
 
Registrado: ago 2006
Posts: 7.659
Poder: 25
Caral Va por buen camino
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
__________________
Siempre Novato
Responder Con Cita
  #20  
Antiguo 04-08-2011
lisc_dla lisc_dla is offline
Miembro
NULL
 
Registrado: jul 2011
Posts: 98
Poder: 13
lisc_dla Va por buen camino
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
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Suma por campos aanil SQL 45 11-02-2010 03:00:50
suma de campos calculados pabloparra Conexión con bases de datos 0 12-03-2008 23:14:12
Suma de dos Campos en un BDGrid esimon SQL 6 14-09-2005 21:50:12
Suma de campos b3nshi Conexión con bases de datos 1 13-04-2005 04:13:40
suma de campos novato cesarjbf SQL 4 01-09-2004 11:12:33


La franja horaria es GMT +2. Ahora son las 19:39:28.


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
Copyright 1996-2007 Club Delphi