delete record that are within a range of dates

0

I am trying to delete records that are within a range of dates I have a small error in my code:

begin 
  query.close;
  query.sql.clear
  query.sql.text := (' delete from tabla where (campo_fecha between :fecha1 and :fecha2);
  query.Parameters.ParamByName('fecha1').DataType := ftDateTime; //aqui tengo el error
  query.Parameters.ParamByName('fecha2').DataType := ftDateTime; // aqui tambien
  query.Parameters.ParamByName('fecha1').Value := FormatDateTime('yyyy/mm/dd',Datetimepicker1.Date);
  query.Parameters.ParamByName('fecha2').Value := FormatDateTime('yyyy/mm/dd',Datetimepicker1.Date);
  query.ExecSQL;
  query.refresh;
end;

Work with Delphi and MS ACCESS and components ADO .

This is my error: Undeclared identifier: 'ftDateTime'

    
asked by pablo 12.10.2018 в 15:50
source

2 answers

1

The identifier ftDateTime belongs to the enumeration TFieldTipe , which is declared on the unit Data.DB (in old versions of Delphi simply DB ). It would be necessary that you declare this unit within the use of your unit, so that the identifier is visible, if you do not have any unit in the uses clause, it would be something similar to:

implementation
uses Data.DB;

If you already have units declared, then add it to the list, separated by a comma, for example:

implementation
uses MiUnidad1, MiUnidad2, Data.DB;

On the other hand, the code that you publish does not compile, but beyond that, I find some problems that I am commenting you with each line:

query.close;

This has a bad smell. It can be derived from a bad practice, if you need to start by closing the object query , I have seen code that has an object for general use and in this it leaves open datasets with any result that remain that way in memory and from which the programmer loses control over the data that they contain or for what they are used. I suggest that you create and destroy a query object whenever you need it, this keeps the code cleaner and, as the application gets more complex, you avoid annoying side effects and errors that are difficult to debug.

  query.sql.clear
  query.sql.text := (' delete from tabla where (campo_fecha between :fecha1 and :fecha2);

You assign a value to the property Text of SQL , which is a TStrings . The previous statement that cleans the SQL is, therefore, superfluous. It is not that this has a high cost, but the fact that you have written (or copied) this code implies that you do not fully understand the operation of the classes, nor the code you are writing. This also smells bad to me. In addition, in the code you publish, the string of characters is poorly constructed, and therefore, the compiler will give an error on this line.

  query.Parameters.ParamByName('fecha1').DataType := ftDateTime; //aqui tengo el error
  query.Parameters.ParamByName('fecha2').DataType := ftDateTime; // aqui tambien
  query.Parameters.ParamByName('fecha1').Value := FormatDateTime('yyyy/mm/dd',Datetimepicker1.Date);
  query.Parameters.ParamByName('fecha2').Value := FormatDateTime('yyyy/mm/dd',Datetimepicker1.Date);

Here you assign the data type, such as DateTime, and then assign a string of characters to the property. There is a bit of the above. The property Value of the parameters can directly accept the date or date / time of the DateTimePicker and, if necessary, will automatically adjust the type of the parameter.

  query.ExecSQL;
  query.refresh;

You execute the SQL, which contains an erase statement, and then you refresh the Query object, which since it executed a delete , does not contain data, why do you refresh it?

    
answered by 15.10.2018 в 05:13
-1

Yes, my code stayed this way

begin
query.close;
query.sql.clear;
query.SQL.Add('delete from tabla where (fecha BETWEEN :fecha1 AND :fecha2 ');
query.Parameters.ParamByName('fecha1').DataType := ftdate;
query.Parameters.ParamByName('fecha1').Value := Datetimepicker1.Date;
query.Parameters.ParamByName('fecha2').DataType := ftdate;
query.Parameters.ParamByName('fecha2').Value := Datetimepicker1.Date;
query.ExecSQL;
end;

On the other hand I have a problem that occurs when I want to associate a query with the query what is this

SELECT *
FROM tabla
WHERE Fecha >= :'01/03/2018'
AND Fecha<=:'31/03/2018';

to activate the query gives me an error that is this 'do not match the data types in the expression of the criteria' if I take the quotes to the dates the query is activated but does not return any results in the grid

    
answered by 17.10.2018 в 00:32