' Prepara una query en base a los parámetros que utilicemos, sean estos cuales sean... si se omiten todos los parámetros, se recogerían todos los registros de dicha tabla.
public function PreparaQuery(byref optional Materia as vaiant, byref optional Fecha as variant, byref optional Grado as variant, optional Cuantos as variant) as string
dim b as string, w as string, m as string, g as string, f as string
if isMissing(cuantos) then
if isNumeric(cuantos) then
b = " TOP " & cstr(cuantos) 'seleccionará los x primeros, esto lo puedes modificar a tu gusto, se expone sólo como ejemplo de sugerencia
else
b= " *" ' seleccionará todos
end if
else
b= " *" ' seleccionará todos
end if
' se usan los 3 campos (siempre que no se haya omitido alguno), se prepara esta parte en base a este hecho
m= Subwhere(Materia, "Materia")
f= Subwhere(Fecha, "Fecha")
g= Subwhere(Grado, "Grado")
' Si materia no fue omitido
if m <>"" then w = " WHERE " & m
if w <>"" then
if f <>"" then w = w " AND " & f ' si fecha no fue omitido
else ' materia fue omitido
if f <>"" then w = " WHERE " & f ' si fecha no fue omitido
end if
' si al menos materia o fecha no fueron omitidos
if w <>"" then
if g <>"" then w = w " AND " & g ' si grado no fue omitido
else ' materia y fecha fueron omitidos
if g <>"" then w = " WHERE " & g ' si grado no fue omitido
end if
PreparaQuery = "Select" & b & " FROM tblDatosCurso" & iif( w<>"", w,"")
end function
' compone parte de los campos para el filtro.
private function Subwhere( byref Valor as variant, byval Campo as string) as string
dim w as string
on local error goto SaleWhere
if not ismissing(Valor ) then
w = ucase$(trim$((Valor ))
end if
if w <> "" then
w = " " & chr(34) & Campo & chr(34) & " = " & w
end if
Subwhere=w
exit function
SaleWhere:
Subwhere=""
end function
' supongamos que utilizamos por debajo un control adodc...
public function GetDatosCurso(byval query as string) as ADODB.recordset
Dim Cmd As ADODB.Command, Conex As ADODB.Connection
if query <> "" then
Set Conex = New ADODB.Connection
Conex.Open Adodc1.ConnectionString ' utilizamos la conexión establecida con el objeto Adodc1
Set Cmd = New ADODB.Command
With Cmd
Set .ActiveConnection = Conex
.CommandType = adCmdText
.CommandText = query
set GetDatosCurso = .Execute(, , adCmdText)
End With
end if
end function