Os indicamos, paso a paso, con capturas de pantalla, explicaciones y código fuente, cómo crear e implementar una ventana de selección de registro. Útil para aquellas tablas / formularios que tengan enlaces a otras tablas (por ejemplo, en una ventana de alta de factura, cómo seleccionar el cliente).
Cómo crear una ventana de selección de registro en Delphi
COMO CREAR UNA VENTANA DE SELECCIÓN DE REGISTRO EN DELPHI
La teoría: a veces nos encontramos con aplicaciones que, cuando hay que seleccionar algún registro "foráneo" (en la ventana de facturas seleccionar el cliente, en la ventana de albaranes seleccionar el proveedor, en la ventana de detalle de factura seleccionar el artículo, etc.) nos muestran un simple desplegable, con el inconveniente de que sólo es posible buscar un elemento por el comienzo del nombre. Esto hace que la selección sea más rápida si se sabe cómo empieza el nombre, pero cuando hay muchos registros es imposible aprenderse los nombres, por lo que se hace inviable.
En nuestro caso, os explicararemos cómo hacer una ventana de selección de registros que sirva para todos los formularios / ventanas de la aplicación. Se trata de una ventana de selección donde el usuario podrá buscar por una o varias palabras (independientemente de la posición donde se encuentren), también podrá buscar por varios campos (nombre, código, población, etc). Todo ello configurable en tiempo de diseño, para poder utilizar la misma ventana.
Esta ventana tendrá el siguiente aspecto:
Se trata de una ventana con un grid que mostrará los campos que le indiquemos en el SELECT (consulta SQL que ejecutemos). Esta ventana tendrá un campo de edición para que el ususuario pueda escribir las palabras de búsqueda. También tendrá cuatro botones de opción para seleccionar por qué campo quiere buscar (son personalizables por código). A su vez, esta ventana incluye una barra de botones con las siguientes opciones:
-
Botón de selección de elemento actual: selecciona el registro actual y cierra la ventana.
-
Botón de inserción de nuevo elemento: muestra una ventana para añadir un nuevo registro.
-
Botón de refrescar: vuelve a ejecutar la consulta SQL, para actualizar los datos.
-
Botón de cerrar: cierra la ventana sin seleccionar ningún elemento.
La ventaja de esta ventana de selección es que permite realizar las búsquedas de forma automática. Es decir, conforme el usuario va escribiendo las palabras a buscar se van mostrando los registros que cumplan la condición de búsqueda.
Un ejemplo de utilización de esta ventana podría ser en el caso de una aplicación de gestión de cobros, la ventana de alta de nuevo cobro sería:
Como se puede observar, en el campo "Cliente" aparece un cuadro de edición (por si el usuario sabe el código, para que lo introduzca directamente) y un botón. Este botón muestra la ventana de selección de clientes:
Es suficiente con escribir un apellido, por ejemplo, y automáticamente aparecerán todos los clientes con ese apellido.
Al seleccionar el cliente, la ventana de cobro quedará de la siguiente forma:
Lo que hace la ventana de selección cuando se selecciona un elemento es guardar el código de dicho registro en una variable global. Luego en el código del botón seleccionar se obtiene dicho valor y se inserta en el campo correspondiente del registro actual.
A continuación explicaremos paso a paso cómo se crea y configura (en tiempo de diseño) la ventana de selección:
- Utilizaremos un TForm con las siguientes características:
- BorderIcons: [biSystemMenu,biMaximize].
- AutoSize = False.
- BorderStyle = bsSizeable.
- Position = poScreenCenter.
- WindowState = wsNormal
- El TForm contendrá los siguientes componentes: TControlBar, TGroupBox, TRadioButton, TLabel, TMaskEdit, TDBGrid, TTimer, TActionList, TPopupMenu:
- El código de las funciones y procedimientos que utilizaremos será:
procedure TformSeleccion.arreglarCampos ();
begin
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 6;
if (tag = vtNumProveedor) then //proveedor
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 7;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
end;
if (tag = vtNumArticulo) then //artículos
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 3;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Cód.';
md.TCSeleccion.FieldByName('nombre').DisplayWidth := 45;
md.TCSeleccion.FieldByName('nombre').DisplayLabel := 'Artículo';
md.TCSeleccion.FieldByName('Familia').DisplayWidth := 10;
md.TCSeleccion.FieldByName('Familia').DisplayLabel := 'Familia';
md.TCSeleccion.FieldByName('Marca').DisplayWidth := 10;
md.TCSeleccion.FieldByName('Marca').DisplayLabel := 'Marca';
md.TCSeleccion.FieldByName('Modelo').DisplayWidth := 5;
md.TCSeleccion.FieldByName('Modelo').DisplayLabel := 'Mod.';
md.TCSeleccion.FieldByName('precioventa').DisplayWidth := 5;
md.TCSeleccion.FieldByName('precioventa').DisplayLabel := 'PVP';
end;
end;
En esta función, que será la encargada de mostrar adecuadamente los resultados (nombre del título de columna, tamaño, etc) hemos puesto un par de ejemplos, lógicamente tendréis que adaptarlos a vuestras necesidades.
- En el evento "FormShow" del TForm pondremos el siguiente código:
procedure TformSeleccion.FormShow(Sender: TObject);
begin
md.TCSeleccion.Close;
vtClaveElegida := 0;
ActRefrescarexecute(nil);
Timer1.Enabled := False;
txtbuscar.setfocus;
end;
- En el evento "FormClose" del TForm colocaremos el siguiente código:
procedure TformSeleccion.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
md.TCSeleccion.close;
screen.cursor := crdefault;
action := cafree;
end;
- El procedimiento "Click" del botón de refrescar tendrá el siguiente código:
procedure TformSeleccion.ActRefrescarExecute(Sender: TObject);
begin
txtBuscar.EditMask := '';
md.TCSeleccion.close;
md.TCSeleccion.sql.clear;
case tag of
vtNumProveedor : //proveedor
begin
Caption := 'Selección de proveedor...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' +
vtTablaTercero +
' WHERE tipo = "Proveedor" or tipo = "Otro"');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Cif/Nif';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Cif/Nif';
md.TCSeleccion.sql.add('Order by cif');
end;
end;
vtNumCliente : //cliente
begin
Caption := 'Selección de cliente...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero +
' WHERE tipo = "Cliente" or tipo = "Otro"');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Cif/Nif';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Cif/Nif';
md.TCSeleccion.sql.add('Order by cif');
end;
end;
try
md.TCSeleccion.open;
except
raise;
end;
arreglarCampos ();
txtbuscar.setfocus;
end;
Como ejemplo, en este procedimiento, hemos puesto dos tipos de registros (cliente y proveedor) . Tendréis que ajustarlo a vuestras necesidades. Como se puede observar hay dos consultas SQL, una para clientes y otra para proveedores, que muestran los campos que hemos considerado importantes (código, nombre, CIF). La constante "vtTablaTercero" ha sido declarada de forma pública y contiene el nombre de la tabla "Terceros" (clientes y proveedores). También hay que mencionar que en nuestro caso los clientes y proveedores se almacenan en la misma tabla, sólo se diferencian por el campo "Tipo", de ahí el
WHERE tipo = "Cliente".
Las constantes vtNumCliente, vtNumProveedor son declaradas públicamente, son números que identifican cada ventana.
- En el evento OnChange del componente TEdit colocaremos el siguiente código:
procedure TformSeleccion.TxtBuscarChange(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 500;
Timer1.Enabled := True;
end;
De esta forma activaremos el temporizador para que, cada medio segundo, realice la búsqueda en la base de datos. Optamos por retardar este medio segundo para que no hubiera un exceso en las consultas SQL a ejecutar (se ejecutaría una consulta SQL sobre la base de datos por cada palabra introducida por el usuario). Ahora estamos barajando la posibilidad de poder permitir que el usuario pueda configurar si desea que las búsquedas sean automáticas o no (se buscaría al hacer intro). Esta opción de búsqueda automática, a veces (con bases de datos conectadas por Internet) puede ser un poco lenta.
- En el evento OnTimer del TTimer (temporizador) podremos el siguiente código:
procedure TformSeleccion.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if (tag in [vtNumproveedor]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
md.TCSeleccion.sql.add('WHERE (tipo = "Proveedor" ' +
'or tipo = "Otro") and nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumCliente]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
md.TCSeleccion.sql.add('WHERE (tipo = "Cliente" ' +
'or tipo = "Otro") and nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
arreglarCampos;
end;
En este procedimiento, realizaremos la consulta de selección (SELECT) con el LIKE y los porcentajes ("%") para buscar por palabras coincidentes independientemente de la posición en la que se encuentren. Una vez más utilizamos las constantes vtNumCliente y vtNumProveedor para diferenciar la consulta SQL a ejecutar y demás. Al final del procedmiento ejecutaremos "arreglarCampos" para mostrar las columnas correctamente.
procedure TformSeleccion.ActNuevoExecute (Sender: TObject);
begin
if (tag = vtNumCliente) or (Tag = vtNumProveedor) or
(Tag = vtNumTercero) then //cliente y proveedor
begin
vtAccion := 'nuevo';
application.createform(tformGCliente, formGCliente);
formGCliente.ShowModal;
end;
if tag = vtNumArticulo then //cliente
begin
vtAccion := 'nuevo';
application.createform(tformGArticulo, formGArticulo);
formGArticulo.ShowModal;
end;
if tag = vtNumMarca then //marca - material
begin
vtAccion := 'nuevo';
application.createform(tformGMarca, formGMarca);
formGMarca.ShowModal;
end;
if tag = vtNumModelo then //modelo - material
begin
vtAccion := 'nuevo';
application.createform(tformGModelo, formGModelo);
formGModelo.ShowModal;
end;
if tag = vtNumTecnico then //Técnico
begin
vtAccion := 'nuevo';
{ DONE : Falta el formulario FormGTecnico }
application.createform(tformGTecnico, formGTecnico);
formGTecnico.ShowModal;
end;
if tag = vtNumContacto then //Contacto
begin
vtAccion := 'nuevo';
application.createform(tformGContacto, formGContacto);
formGContacto.ShowModal;
end;
if tag = vtNumRecurso then //Recurso
begin
vtAccion := 'nuevo';
application.createform(tformGRecurso, formGRecurso);
formGRecurso.ShowModal;
end;
ActRefrescarExecute(nil);
end;
Lógicamente, para que este procedimiento funcione tendremos que tener las ventanas correspondientes de alta de: recurso, cliente, proveedor, artículo, marca, modelo, técnico y contacto. Este procedimiento es un ejemplo aplicable a un desarrollo de un programa de Gestión, cada uno lo adaptará a sus necesidades
- En la ventana que hace la llamada a la ventana de selección (en nuestro ejemplo FormGCobro), el botón de abrir selección tendrá el siguiente código:
procedure TformGCobro.BSeleccionarClick(Sender: TObject);
begin
txtCodigoCliente.SetFocus;
application.createform(tformseleccion, formseleccion);
formseleccion.ActNuevo.Enabled := true;
formseleccion.tag := vtNumCliente;
formseleccion.ShowModal;
if vtClaveElegida <> 0 then
tTabla.FieldByName ('codigocliente').AsInteger := vtClaveElegida;
end;
En este botón, para diferenciar el tipo de registro que queramos mostrar en la ventana de selección, utilizamos el "tag" del formulario selección, pasándole el valor de la constante que identifica los registros a mostrar. En este ejemplo "vtNumCliente". Tras cerrarse la ventana de selección, este procedimiento comprueba si se ha seleccionado algún valor, de ser así se lo asignará al campo "codigocliente" de la tabla de cobros que estamos modificando. Como siempre os indicamos esto deberéis adaptarlo a vuestras necesidades.
NOTAS ADICIONALES
En nuestro caso, el componente de acceso a base de datos, lo hemos colocado en un módulo de datos (Data Module), aunque se podría colocar en el propio formulario. Por ello, cuando nos referimos al componente TQuery ponemos:
md.TCSeleccion
donde "md" es el nombre del módulo de datos y "TCSeleccion" es el nombre del "TQuery".
Os mostramos el código completo de una ventana de selección de una de nuestras aplicaciones, AjpdSoft Gestión Integral:
unit UnidadSeleccion;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, ComCtrls, ToolWin, Grids, DBGrids, Db, DBTables,
ActnList, Menus,variants, Mask;
type
TformSeleccion = class(TForm)
Tabla: TDBGrid;
Panel1: TPanel;
ActionList1: TActionList;
ActSeleccionar: TAction;
ActCerrar: TAction;
LB: TLabel;
GroupBox1: TGroupBox;
Op1: TRadioButton;
Op2: TRadioButton;
Op3: TRadioButton;
Op4: TRadioButton;
PopupMenu1: TPopupMenu;
Seleccionar1: TMenuItem;
Salir1: TMenuItem;
N1: TMenuItem;
CB: TControlBar;
BB: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ActNuevo: TAction;
ActRefrescar: TAction;
Nuevo1: TMenuItem;
Refrescar1: TMenuItem;
actMostrarTodos: TAction;
ToolButton4: TToolButton;
Mostrartodos1: TMenuItem;
Timer1: TTimer;
txtBuscar: TMaskEdit;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure TablaKeyPress(Sender: TObject; var Key: Char);
procedure FormShow(Sender: TObject);
procedure seleccionar ();
procedure ActCerrarExecute(Sender: TObject);
procedure TxtBuscarChange(Sender: TObject);
procedure TxtBuscarKeyPress(Sender: TObject; var Key: Char);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure TxtBuscarKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ActNuevoExecute(Sender: TObject);
procedure ActRefrescarExecute(Sender: TObject);
procedure ActSeleccionarExecute(Sender: TObject);
procedure actMostrarTodosExecute(Sender: TObject);
procedure arreglarCampos ();
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
formSeleccion: TformSeleccion;
implementation
uses UnidadMenuPrincipal, UnidadProcedimientos, UnidadModuloDatos,
UnidadGCliente, UnidadGArticulo, UnidadGFamilia, UnidadGMarca,
UnidadGModelo, UnidadGTecnico, UnidadGParte, UnidadGAccion,
UnidadGContacto, UnidadGRecurso;
{$R *.DFM}
procedure TformSeleccion.arreglarCampos ();
begin
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 6;
if (tag = vtNumProveedor)then //proveedor
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 7;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
end;
if (tag = vtNumContacto)then //contacto
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 7;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('emailempresa1').DisplayWidth := 15;
md.TCSeleccion.FieldByName('emailempresa1').DisplayLabel := 'Email empresa';
md.TCSeleccion.FieldByName('movilempresa1').DisplayWidth := 15;
md.TCSeleccion.FieldByName('movilempresa1').DisplayLabel := 'Móvil empresa';
end;
if (tag = vtNumArticulo) then //artículos
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 3;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Cód.';
md.TCSeleccion.FieldByName('nombre').DisplayWidth := 45;
md.TCSeleccion.FieldByName('nombre').DisplayLabel := 'Artículo';
md.TCSeleccion.FieldByName('Familia').DisplayWidth := 10;
md.TCSeleccion.FieldByName('Familia').DisplayLabel := 'Familia';
md.TCSeleccion.FieldByName('Marca').DisplayWidth := 10;
md.TCSeleccion.FieldByName('Marca').DisplayLabel := 'Marca';
md.TCSeleccion.FieldByName('Modelo').DisplayWidth := 5;
md.TCSeleccion.FieldByName('Modelo').DisplayLabel := 'Mod.';
md.TCSeleccion.FieldByName('precioventa').DisplayWidth := 5;
md.TCSeleccion.FieldByName('precioventa').DisplayLabel := 'PVP';
end;
if (tag = vtNumParte) then //partes
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 5;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('numero').DisplayWidth := 5;
md.TCSeleccion.FieldByName('numero').DisplayLabel := 'Nº';
md.TCSeleccion.FieldByName('fecha').DisplayWidth := 12;
md.TCSeleccion.FieldByName('fecha').DisplayLabel := 'Fecha';
md.TCSeleccion.FieldByName('totalhoras').DisplayWidth := 5;
md.TCSeleccion.FieldByName('totalhoras').DisplayLabel := 'T.Horas';
md.TCSeleccion.FieldByName('tecnico1').DisplayWidth := 20;
md.TCSeleccion.FieldByName('tecnico1').DisplayLabel := 'Técnico 1';
md.TCSeleccion.FieldByName('tecnico2').DisplayWidth := 20;
md.TCSeleccion.FieldByName('tecnico2').DisplayLabel := 'Técnico 2';
end;
if (tag = vtNumIncidencia)then //incidencia
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 5;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('asunto').DisplayWidth := 30;
md.TCSeleccion.FieldByName('asunto').DisplayLabel := 'Asunto';
md.TCSeleccion.FieldByName('tecnico').DisplayWidth := 15;
md.TCSeleccion.FieldByName('tecnico').DisplayLabel := 'Técnico';
md.TCSeleccion.FieldByName('cliente').DisplayWidth := 15;
md.TCSeleccion.FieldByName('cliente').DisplayLabel := 'Cliente';
md.TCSeleccion.FieldByName('fecha').DisplayWidth := 10;
md.TCSeleccion.FieldByName('fecha').DisplayLabel := 'Fecha';
end;
if (tag = vtNumNota)then //Nota
begin
md.TCSeleccion.FieldByName('Codigo').DisplayWidth := 5;
md.TCSeleccion.FieldByName('Codigo').DisplayLabel := 'Código';
md.TCSeleccion.FieldByName('asunto').DisplayWidth := 50;
md.TCSeleccion.FieldByName('asunto').DisplayLabel := 'Asunto';
md.TCSeleccion.FieldByName('fechaalta').DisplayWidth := 10;
md.TCSeleccion.FieldByName('fechaalta').DisplayLabel := 'Fecha';
end;
end;
procedure TformSeleccion.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
md.TCSeleccion.close;
screen.cursor := crdefault;
action := cafree;
end;
procedure TformSeleccion.TablaKeyPress(Sender: TObject; var Key: Char);
begin
if (key = 'c') or (key = 'C') or (key = chr(13))then
actseleccionarexecute(nil);
end;
procedure TformSeleccion.FormShow(Sender: TObject);
begin
md.TCSeleccion.Close;
vtClaveElegida := 0;
ActRefrescarexecute(nil);
Timer1.Enabled := False;
txtbuscar.setfocus;
end;
procedure tformSeleccion.seleccionar ();
begin
if (tag in [vtNumProveedor, vtNumCliente, vtNumArticulo,
vtNumFamilia, vtNumMarca, vtNumModelo, vtNumAccion, vtNumTecnico,
vtNumParte, vtNumContacto, vtNumRecurso, vtNumTercero,
vtNumIncidencia, vtNumNota]) then
begin
if not (md.TCSeleccion.fieldbyname('Codigo').AsInteger = 0) then
vtClaveElegida := md.TCSeleccion.fieldbyname('Codigo').AsInteger;
end;
if tag = vtNumContacto then
begin
vtClaveElegidaT := md.tcSeleccion.FieldByName('nombre').AsString;
vtClaveElegidaT2 := 'Email: ' +
md.tcSeleccion.FieldByName('emailempresa1').AsString +
' - Móvil: ' +
md.tcSeleccion.FieldByName('movilempresa1').AsString;
end;
if tag = vtNumTercero then
begin
vtClaveElegidaT := md.tcSeleccion.FieldByName('nombre').AsString;
end;
close;
end;
procedure TformSeleccion.ActCerrarExecute(Sender: TObject);
begin
close;
end;
procedure TformSeleccion.TxtBuscarChange(Sender: TObject);
begin
Timer1.Enabled := False;
Timer1.Interval := 500;
Timer1.Enabled := True;
end;
procedure TformSeleccion.TxtBuscarKeyPress(Sender: TObject; var Key: Char);
begin
if key = chr(13) then
begin
key := #0;
tabla.setfocus;
end;
end;
procedure TformSeleccion.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key = chr(27) then //escape
ActCerrarExecute(nil);
end;
procedure TformSeleccion.TxtBuscarKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 40 then //tecla cursor abajo
tabla.setfocus;
end;
procedure TformSeleccion.ActNuevoExecute(Sender: TObject);
begin
if (tag = vtNumCliente) or (Tag = vtNumProveedor) or
(Tag = vtNumTercero) then //cliente y proveedor
begin
vtAccion := 'nuevo';
application.createform(tformGCliente, formGCliente);
formGCliente.ShowModal;
end;
if tag = vtNumArticulo then //cliente
begin
vtAccion := 'nuevo';
application.createform(tformGArticulo, formGArticulo);
formGArticulo.ShowModal;
end;
if tag = vtNumFamilia then //familia - material
begin
vtAccion := 'nuevo';
application.createform(tformGFamilia, formGFamilia);
formGFamilia.ShowModal;
end;
if tag = vtNumMarca then //marca - material
begin
vtAccion := 'nuevo';
application.createform(tformGMarca, formGMarca);
formGMarca.ShowModal;
end;
if tag = vtNumModelo then //modelo - material
begin
vtAccion := 'nuevo';
application.createform(tformGModelo, formGModelo);
formGModelo.ShowModal;
end;
if tag = vtNumParte then //parte
begin
vtAccion := 'nuevo';
application.createform(tformGParte, formGParte);
formGModelo.ShowModal;
end;
if tag = vtNumAccion then //Tarea
begin
vtAccion := 'nuevo';
application.createform(tformGAccion, formGAccion);
formGAccion.ShowModal;
end;
if tag = vtNumTecnico then //Técnico
begin
vtAccion := 'nuevo';
{ DONE : Falta el formulario FormGTecnico }
application.createform(tformGTecnico, formGTecnico);
formGTecnico.ShowModal;
end;
if tag = vtNumContacto then //Contacto
begin
vtAccion := 'nuevo';
application.createform(tformGContacto, formGContacto);
formGContacto.ShowModal;
end;
if tag = vtNumRecurso then //Recurso
begin
vtAccion := 'nuevo';
application.createform(tformGRecurso, formGRecurso);
formGRecurso.ShowModal;
end;
ActRefrescarExecute(nil);
end;
procedure TformSeleccion.ActRefrescarExecute(Sender: TObject);
begin
txtBuscar.EditMask := '';
md.TCSeleccion.close;
md.TCSeleccion.sql.clear;
case tag of
vtNumProveedor : //proveedor
begin
Caption := 'Selección de proveedor...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero +
' WHERE tipo = "Proveedor" or tipo = "Otro"');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Cif/Nif';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Cif/Nif';
md.TCSeleccion.sql.add('Order by cif');
end;
end;
vtNumCliente : //cliente
begin
Caption := 'Selección de cliente...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero +
' WHERE tipo = "Cliente" or tipo = "Otro"');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Cif/Nif';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Cif/Nif';
md.TCSeleccion.sql.add('Order by cif');
end;
end;
vtNumTercero : //tercero
begin
Caption := 'Selección de tercero...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Cif/Nif';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Cif/Nif';
md.TCSeleccion.sql.add('Order by cif');
end;
end;
vtNumArticulo : //artículo
begin
Caption := 'Selección de artículo...';
md.TCSeleccion.sql.add('SELECT a.Codigo, a.Nombre, ' +
'f.Nombre Familia, m.Nombre Marca, ' +
'md.Nombre Modelo, a.precioventa');
md.TCSeleccion.sql.add('FROM ' +
vtTablaArticulo + ' a, ' + vtTablaFamilia + ' f, ' +
vtTablaMarca + ' m, ' + vtTablaModelo + ' md');
md.TCSeleccion.sql.add('WHERE a.codigofamilia=f.codigo ' +
'AND a.codigomarca=m.codigo AND a.codigomodelo=md.codigo');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op4.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
op3.Caption := 'Familia';
op4.Caption := 'Marca';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by a.nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by a.codigo');
end;
if Op3.Checked then //familia
begin
md.TCSeleccion.sql.add('Order by f.nombre');
lb.Caption := '&Buscar por Familia';
end;
if Op4.Checked then //marca
begin
md.TCSeleccion.sql.add('Order by m.nombre');
lb.Caption := '&Buscar por marca';
end;
end;
vtNumParte : //Partes de trabajo
begin
Caption := 'Selección de parte de trabajo...';
md.TCSeleccion.sql.add('SELECT p.codigo, p.numero, ' +
' p.fecha, p.totalhoras, t.Nombre Tecnico1, t2.Nombre Tecnico2');
md.TCSeleccion.sql.add('FROM ' + vtTablaParte + ' p, '
+ vtTablaTecnico + ' t, ' + vtTablaTecnico + ' t2');
md.TCSeleccion.sql.add('WHERE p.tecnico1 = ' +
't.codigo and p.tecnico2 = t2.codigo');
md.TCSeleccion.sql.add('and p.codigocliente = ' +
' :pCodigoCliente and p.facturado <> :pFacturado');
md.TCSeleccion.ParamByName('pFacturado').DataType := ftString;
md.TCSeleccion.ParamByName('pFacturado').Value := 'S';
md.TCSeleccion.ParamByName('pCodigoCliente').DataType := ftInteger;
md.TCSeleccion.ParamByName('pCodigoCliente').Value := vtCodigoSel;
op1.visible := true;
op2.visible := true;
op3.visible := true;
op4.visible := true;
op1.caption := '&Número';
op2.caption := 'Códig&o';
op3.Caption := 'Técnico 1';
op4.Caption := 'Fecha';
if Op1.Checked then //numero
begin
md.TCSeleccion.sql.add('Order by numero');
lb.Caption := '&Buscar por número';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
if Op3.Checked then //tecnico1
begin
md.TCSeleccion.sql.add('Order by t.Nombre');
lb.Caption := '&Buscar por Técnico 1';
end;
if Op4.Checked then //fecha
begin
md.TCSeleccion.sql.add('Order by fecha');
lb.Caption := '&Buscar por fecha';
txtBuscar.EditMask := '!99/99/0000;1;_';
end;
end;
vtNumFamilia : //Familia - material
begin
Caption := 'Selección de Familia...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaFamilia);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumModelo : //Modelo - material
begin
Caption := 'Selección de Modelo...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaModelo);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumMarca : //Marca - material
begin
Caption := 'Selección de Marca...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaMarca);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumTecnico : //Técnico
begin
Caption := 'Selección de Técnico...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaTecnico);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumAccion : //Acción
begin
Caption := 'Selección de Acción...';
md.TCSeleccion.sql.add('SELECT codigo, nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaAccion);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumContacto : //Contacto
begin
Caption := 'Selección de Contacto...';
md.TCSeleccion.sql.add('SELECT codigo, nombre, ' +
'movilempresa1, emailempresa1');
md.TCSeleccion.sql.add('FROM ' + vtTablaContacto);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumRecurso : //Recurso
begin
Caption := 'Selección de Recurso...';
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaRecurso);
op1.visible := true;
op2.visible := true;
op1.caption := '&Nombre';
op2.caption := 'Códig&o';
if Op1.Checked then //nombre
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por nombre';
end;
if Op2.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
vtNumIncidencia : //incidencias
begin
Caption := 'Búsqueda de incidencia...';
md.TCSeleccion.sql.add('SELECT i.codigo, i.fecha, i.asunto, '
't.nombre Cliente, tn.nombre Tecnico');
md.TCSeleccion.sql.add('FROM ' + vtTablaIncidencia + ' i,');
md.TCSeleccion.sql.add( vtTablaTercero + ' t, ' +
vtTablaTecnico + ' tn');
md.TCSeleccion.sql.add('WHERE i.codigocliente = ' +
't.codigo and i.codigotecnico = tn.codigo');
op1.visible := true;
op2.visible := true;
op3.visible := true;
op4.visible := true;
op1.caption := '&Incidencia';
op2.caption := '&Resolución';
op3.Caption := 'Contacto';
op4.Caption := 'Asunto';
if Op1.Checked then //descripción
begin
md.TCSeleccion.sql.add('Order by i.incidencia');
lb.Caption := '&Buscar por Incidencia';
end;
if Op2.Checked then //resolución
begin
md.TCSeleccion.sql.add('Order by i.incidenciaresolucion');
lb.Caption := '&Buscar por Resolución';
end;
if Op3.Checked then //contacto
begin
md.TCSeleccion.sql.add('Order by i.contacto');
lb.Caption := '&Buscar por Contacto';
end;
if Op4.Checked then //asunto
begin
md.TCSeleccion.sql.add('Order by i.asunto');
lb.Caption := '&Buscar por Asunto';
end;
end;
vtNumNota : //nota
begin
Caption := 'Selección de Nota...';
md.TCSeleccion.sql.add('SELECT codigo, asunto, fechaalta');
md.TCSeleccion.sql.add('FROM ' + vtTablaNota);
op1.visible := true;
op2.visible := true;
op3.visible := true;
op1.caption := '&Contenido';
op2.caption := '&Asunto';
op3.caption := 'Códig&o';
if Op1.Checked then //contenido
begin
md.TCSeleccion.sql.add('Order by nombre');
lb.Caption := '&Buscar por contenido';
end;
if Op2.Checked then //asunto
begin
md.TCSeleccion.sql.add('Order by asunto');
lb.Caption := '&Buscar por asunto';
end;
if Op3.Checked then //codigo
begin
lb.Caption := '&Buscar por Código';
md.TCSeleccion.sql.add('Order by codigo');
end;
end;
end;
try
md.TCSeleccion.open;
except
raise;
end;
arreglarCampos ();
txtbuscar.setfocus;
end;
procedure TformSeleccion.ActSeleccionarExecute(Sender: TObject);
begin
seleccionar();
end;
procedure TformSeleccion.actMostrarTodosExecute(Sender: TObject);
begin
tag := 50;
ActRefrescarExecute(nil);
end;
procedure TformSeleccion.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
if (tag in [vtNumproveedor]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
md.TCSeleccion.sql.add('WHERE (tipo = "Proveedor" ' +
'or tipo = "Otro") and nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString := '%' +
TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo',
txtbuscar.text, [loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumCliente]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
md.TCSeleccion.sql.add('WHERE (tipo = "Cliente" or ' +
tipo = "Otro") and nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString := '%' +
TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumTercero]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre, cif');
md.TCSeleccion.sql.add('FROM ' + vtTablaTercero);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString := '%' +
TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumArticulo]) then
begin
if Op1.Checked or op3.checked or
op4.checked then //Nombre, familia, marca
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT a.Codigo, a.Nombre, ' +
'f.Nombre Familia, m.Nombre Marca, ' +
'md.Nombre Modelo, a.precioventa');
md.TCSeleccion.sql.add('FROM ' + vtTablaArticulo + ' a, '
+ vtTablaFamilia + ' f, ' + vtTablaMarca + ' m, '
+ vtTablaModelo + ' md');
md.TCSeleccion.sql.add('WHERE a.codigofamilia=f.codigo ' +
' AND a.codigomarca=m.codigo AND a.codigomodelo=md.codigo');
if Op1.Checked then
md.TCSeleccion.sql.add('and a.nombre like :pValor');
if Op3.Checked then
md.TCSeleccion.sql.add('and f.nombre like :pValor');
if Op4.Checked then
md.TCSeleccion.sql.add('and m.nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString := '%' +
TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumParte]) then
begin
if Op1.Checked or op3.checked or
op4.Checked then //número, técnico, fecha
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT p.codigo, p.numero, ' +
'p.fecha, p.totalhoras, t.Nombre Tecnico1, ' +
't2.Nombre Tecnico2');
md.TCSeleccion.sql.add('FROM ' + vtTablaParte +
' p, ' + vtTablaTecnico + ' t, ' +
vtTablaTecnico + ' t2');
md.TCSeleccion.sql.add('WHERE p.tecnico1 = t.codigo ' +
'and p.tecnico2 = t2.codigo');
md.TCSeleccion.sql.add('and p.codigocliente = ' +
':pCodigoCliente and p.facturado <> :pFacturado');
if Op1.Checked then
md.TCSeleccion.sql.add('and p.numero like :pValor');
if Op3.Checked then
md.TCSeleccion.sql.add('and t.nombre like :pValor');
if Op4.Checked then
md.TCSeleccion.sql.add('and p.fecha = ' +
'STR_TO_DATE(:pValor, "%d/%c/%Y %H:%i:%s")');
md.TCSeleccion.ParamByName('pFacturado').DataType := ftString;
md.TCSeleccion.ParamByName('pFacturado').Value := 'S';
md.TCSeleccion.ParamByName('pCodigoCliente').DataType := ftInteger;
md.TCSeleccion.ParamByName('pCodigoCliente').Value := vtCodigoSel;
if Op4.Checked then
begin
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
if Length (TxtBuscar.Text) = 10 then
md.TCSeleccion.ParamByName('pValor').AsString := TxtBuscar.Text;
end
else
begin
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
end;
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumFamilia]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaFamilia);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumAccion]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaAccion);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumMarca]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaMarca);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumModelo]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaModelo);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumTecnico]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaTecnico);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumRecurso]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT Codigo, Nombre');
md.TCSeleccion.sql.add('FROM ' + vtTablaRecurso);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumContacto]) then
begin
if Op1.Checked then //Nombre
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT codigo, nombre, ' +
'movilempresa1, emailempresa1');
md.TCSeleccion.sql.add('FROM ' + vtTablaContacto);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
if (tag in [vtNumIncidencia]) then
begin
if Op1.Checked then //incidencia
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT i.codigo, i.fecha, ' +
'i.asunto, t.nombre Cliente, tn.nombre Tecnico');
md.TCSeleccion.sql.add('FROM ' +
vtTablaIncidencia + ' i,');
md.TCSeleccion.sql.add( vtTablaTercero + ' t, '
+ vtTablaTecnico + ' tn');
md.TCSeleccion.sql.add('WHERE i.codigocliente = ' +
't.codigo and i.codigotecnico = tn.codigo');
md.TCSeleccion.sql.add('and i.incidencia like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //resolución
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT i.codigo, i.fecha, ' +
'i.asunto, t.nombre Cliente, tn.nombre Tecnico');
md.TCSeleccion.sql.add('FROM ' +
vtTablaIncidencia + ' i,');
md.TCSeleccion.sql.add( vtTablaTercero + ' t,
' + vtTablaTecnico + ' tn');
md.TCSeleccion.sql.add('WHERE i.codigocliente = ' +
't.codigo and i.codigotecnico = tn.codigo');
md.TCSeleccion.sql.add('and i.incidenciaresolucion like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op3.Checked then //contacto
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT i.codigo, i.fecha, ' +
'i.asunto, t.nombre Cliente, tn.nombre Tecnico');
md.TCSeleccion.sql.add('FROM ' +
vtTablaIncidencia + ' i,');
md.TCSeleccion.sql.add( vtTablaTercero + ' t, ' +
vtTablaTecnico + ' tn');
md.TCSeleccion.sql.add('WHERE i.codigocliente = ' +
't.codigo and i.codigotecnico = tn.codigo');
md.TCSeleccion.sql.add('and i.contacto like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op4.Checked then //asunto
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT i.codigo, i.fecha, ' +
'i.asunto, t.nombre Cliente, tn.nombre Tecnico');
md.TCSeleccion.sql.add('FROM ' +
vtTablaIncidencia + ' i,');
md.TCSeleccion.sql.add( vtTablaTercero + ' t, ' +
vtTablaTecnico + ' tn');
md.TCSeleccion.sql.add('WHERE i.codigocliente =' +
' t.codigo and i.codigotecnico = tn.codigo');
md.TCSeleccion.sql.add('and i.asunto like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
end;
if (tag in [vtNumNota]) then
begin
if Op1.Checked then //contenido
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT codigo, asunto, fechaalta');
md.TCSeleccion.sql.add('FROM ' + vtTablaNota);
md.TCSeleccion.sql.add('WHERE nombre like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op2.Checked then //asunto
begin
try
md.tcSeleccion.Close;
md.TCSeleccion.SQL.Clear;
md.TCSeleccion.sql.add('SELECT codigo, asunto, fechaalta');
md.TCSeleccion.sql.add('FROM ' + vtTablaNota);
md.TCSeleccion.sql.add('WHERE asunto like :pValor');
md.TCSeleccion.ParamByName('pValor').DataType := ftString;
md.TCSeleccion.ParamByName('pValor').AsString :=
'%' + TxtBuscar.Text + '%';
md.TCSeleccion.Open;
except
//silenciosa
end;
end;
if Op3.Checked then //codigo
begin
try
md.tcSeleccion.Locate ('Codigo', txtbuscar.text,
[loCaseInsensitive, loPartialKey]);
except
//silenciosa
end;
end;
end;
arreglarCampos;
end;
end.
Para realizar este artículo hemos utilizado:
- MySQL Server 4.1.
- Borland Delphi 6.
Créditos:
- Diseño de la ventana y código por AjpdSoft.
- Algoritmo de búsqueda automático (mediante temporizador y LIKE) por Antonio RN de RS.
Anuncios