filtering dates

2

I'm doing a program that filters dates from three tables in my database, the problem is when I do a search, it only shows me a table and not the three tables that I want. This is my code

query.Close;
 query.SQL.Clear;
 query.SQL.Text := ('select campo1,campo2 from tabla1 where campo3 >= :fecha1 and campo3 <= :fecha2 '+ ' union all '+
 'select campo1,campo2 from tabla2 where campo3 >= :fecha1 and campo3 <= :fecha2 '+ 'union all '+   'select campo1,campo2 from tabla3 where campo3 >= :fecha1 and campo3 <= :fecha2 order by coberturafechahasta ');
 query.Parameters.ParamByName('fecha1').Value:= FormatDateTime('dd/mm/yyyy',Datetimepicker1.Date);
 query.Parameters.ParamByName('fecha2').Value:= FormatDateTime('dd/mm/yyyy',Datetimepicker2.Date);
query.Open;

use composenetes ado and MS ACCESS as a database

    
asked by pablo 02.10.2018 в 01:46
source

1 answer

1

In some connection components, if you need to use the same parameter several times you must use different names. In the code that you have placed would be as follows:

  query.Close;
  query.SQL.Clear;
  query.SQL.Text := ('select campo1,campo2 from tabla1 where campo3 >= :fecha1 and campo3 <= :fecha2 '  + ' union all '+
                     'select campo1,campo2 from tabla2 where campo3 >= :fecha3 and campo3 <= :fecha4 '+ 'union all '+
                     'select campo1,campo2 from tabla3 where campo3 >= :fecha5 and campo3 <= :fecha6');
  query.Parameters.ParamByName('fecha1').Value := FormatDateTime('dd/mm/yyyy',edtFechaIni.Date);
  query.Parameters.ParamByName('fecha3').Value := FormatDateTime('dd/mm/yyyy',edtFechaIni.Date);
  query.Parameters.ParamByName('fecha5').Value := FormatDateTime('dd/mm/yyyy',edtFechaIni.Date);

  query.Parameters.ParamByName('fecha2').Value := FormatDateTime('dd/mm/yyyy',edtFechaFin.Date);
  query.Parameters.ParamByName('fecha4').Value := FormatDateTime('dd/mm/yyyy',edtFechaFin.Date);
  query.Parameters.ParamByName('fecha6').Value := FormatDateTime('dd/mm/yyyy',edtFechaFin.Date);
  query.Open;

You do not specify what you are using, so you must try it with your code. For example, with ADO, the correct way to use it is like this.

    
answered by 02.10.2018 / 10:14
source