Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/03/2008, 19:51
Avatar de u_goldman
u_goldman
Moderador
 
Fecha de Ingreso: enero-2002
Mensajes: 8.031
Antigüedad: 23 años, 3 meses
Puntos: 98
Re: ¿¿Como hacer un BUSCADOR en ASP...??

Tiene un rato que no toco ASP, pero una de las formas mas sencillas para hacer un buscador es la siguiente, se asume que tienes ya una conexión activa:

Código:
<form bla bla bla>
 Buscar: <input type="text" name="buscar" value="" />
 <hr />
Criterio:<br />
<checkbox name="buscarid" value="1" />ID<br />
<checkbox name="buscarnombre" value="1" />Nombre<br />
<checkbox name="buscarapellido" value="1" />Apellido<br />
<checkbox name="buscartelefono" value="1" />Telefono<br />
Boton Submit
</form>

En base a los criterios de busqueda crearás dinámicamente tu consulta, solamente te vales de una variable argCount que te dice si ya tiene argumentos dicha consulta, si no los tiene, entonces concatena la condición necesaria (WHERE) si los tiene entonces concatena el operador lógico (OR), el campo y su respectivo valor.

Una vez que lo tengas hecho convendrá agregar seguridad para evitar el uso de inyección de SQL porque de esta manera está vulnerable, pero esa es otra historia.
Código:
Dim argCount, termino
termino  = Request.Form("buscar")
argCount = 0
strSQL = "SELECT campos FROM tabla "

if len(Request.Form("buscarid")) > 0 then
   if argCount > 0 then
       strSQL = strSQL & " OR "
   else
      strSQL = strSQL & " WHERE "
   end if
   strSQL = strSQL & " id =" & termino 
   argCount = argCount + 1
end if

if len(Request.Form("buscarnombre")) > 0 then
   if argCount > 0 then
       strSQL = strSQL & " OR "
   else
      strSQL = strSQL & " WHERE "
   end if
   strSQL = strSQL & " nombre like '%" & termino  & "%' "
   argCount = argCount + 1
end if


if len(Request.Form("buscarapellido")) > 0 then
   if argCount > 0 then
       strSQL = strSQL & " OR "
   else
      strSQL = strSQL & " WHERE "
   end if
   strSQL = strSQL & " apellido like '%" & termino  & "%' "
   argCount = argCount + 1
end if


if len(Request.Form("buscartelefono")) > 0 then
   if argCount > 0 then
       strSQL = strSQL & " OR "
   else
      strSQL = strSQL & " WHERE "
   end if
   strSQL = strSQL & " telefono like '%" & termino  & "%' "
   argCount = argCount + 1
end if

Set rs = Conn.Execute(strSQL)
Saludos
__________________
"El hombre que ha empezado a vivir seriamente por dentro, empieza a vivir más sencillamente por fuera."
-- Ernest Hemingway