Búsqueda
Hola a todos:
Tengo un formulario en el que pretendo hacer una búsqueda en 5 control data a la vez pasando como parametros n campos comunes y me da un error en la Function criterios donde dice que no coinciden los tipos;creo que la busqueda que la hago por Sql está mal pero no se bien donde a continuacion pego todo el codigo del formulario para ver si alguien me da con el problema.GRACIAS
Option Explicit
Dim i As Integer
Private Sub Check1_LostFocus()
Txtinterface.Enabled = Check1
If Txtinterface.Enabled = True Then
Txtinterface.SetFocus
Else
Txtcircuito.SetFocus
End If
End Sub
Private Sub Cmdaceptar_Click()
Dim aux As Integer
For i = 0 To 5
If Check(i).Value = 1 Then
aux = Check(i).Value
End If
Next
If aux = 0 Then
MsgBox "No ha marcado ninguna cuadrÃcula.Marque al menos una", vbInformation + vbOKOnly, "Atención :"
Check(0).SetFocus
Exit Sub
Else
activar_datos
'Con este código habilito los datas de los check marcados para realizar la búsqueda
Data(0).Enabled = Check(0) 'Data para Hohner
Data(1).Enabled = Check(1) 'Givi
Data(2).Enabled = Check(2) 'Elgo
Data(3).Enabled = Check(3) 'Posital Fraba
Data(4).Enabled = Check(4) 'Scancon
Data(5).Enabled = Check(5) 'WayCon
End If
End Sub
Private Sub CmdAceptar2_Click()
For i = 0 To 5
If Data(i).Enabled = True Then
'Puse una comilla simple antes del where
Data(i).RecordSource = "select * from " & TABLA & "'where" & Criterio
Data(i).Refresh
End If
Next
DBG.Visible = True
End Sub
Private Sub Cmdbuscar_Click()
marco_opciones.Enabled = False
marco_botones.Enabled = False
marco_datos.Enabled = True
End Sub
Private Sub Cmdcancelar_Click()
For i = 0 To 5
Data(i).Enabled = False
Next
desactivar_datos
Cmdsalir.SetFocus
End Sub
Private Sub CmdCancelar2_Click()
activar_datos
Cmdbuscar.SetFocus
End Sub
Private Sub Cmdsalir_Click()
Unload Me
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
SendKeys "{tab}"
Case 27
Unload Me
End Select
End Sub
Private Sub activar_datos()
marco_opciones.Enabled = False
marco_datos.Enabled = False
marco_botones.Enabled = True
End Sub
Private Sub desactivar_datos()
marco_opciones.Enabled = True
marco_datos.Enabled = False
marco_botones.Enabled = False
End Sub
Private Sub Form_Load()
desactivar_datos
End Sub
Private Sub Form_Unload(Cancel As Integer)
If marco_datos.Enabled = True Then
Cancel = True
End If
End Sub
Private Sub Option1_LostFocus()
If Option1.Value = True Then
Txt(2).Enabled = True
Txtgiro.Enabled = False
Txtcodigo.Enabled = False
Txt(2).SetFocus
End If
End Sub
Private Sub Option2_LostFocus()
If Option2.Value = True Then
Txt(2).Enabled = False
Txtgiro.Enabled = True
Txtcodigo.Enabled = True
Txtgiro.SetFocus
End If
End Sub
Private Sub Txtbrida_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtcircuito_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtcodigo_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtconexion_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txteje_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtfuente_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtgiro_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtinterface_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Function TABLA() As String
If Data(i) = Data(0) Then TABLA = "Encoder_Hohner"
If Data(i) = Data(1) Then TABLA = "Encoder_Givi"
If Data(i) = Data(2) Then TABLA = "Encoder_Elgo"
If Data(i) = Data(3) Then TABLA = "Encoder_Posital"
If Data(i) = Data(4) Then TABLA = "Encoder_Scancon"
If Data(i) = Data(5) Then TABLA = "Encoder_WayCon"
End Function
Function Criterio() As String
'Cambie la i por la j
Dim j As Integer
'Criterio = ""
For j = 0 To 16
If Txt(j) <> "" Then
Criterio = Criterio & Campos(j) = "' & Txt(j) & " And ""
End If
Next
'Elijo los campos de los ComboBox y DataCombo
If Txtgiro <> "" Then Criterio = Criterio & "Giro" = "' & Txtgiro & " And ""
If Txtcodigo <> "" Then Criterio = Criterio & "Tipcod" = "' & Txtcodigo & " And ""
If Txteje <> "" Then Criterio = Criterio & "Tipeje" = "' & Txteje & " And ""
If Txtbrida <> "" Then Criterio = Criterio & "Brida" = "' & Txtbrida & " And ""
If Txtconexion <> "" Then Criterio = Criterio & "Tipcon" = "' & Txtconexion & " And ""
If Txtinterface <> "" Then Criterio = Criterio & "Inter" = "' & Txtinterface & " And ""
If Txtcircuito <> "" Then Criterio = Criterio & "CS" = "' & Txtcircuito & " And ""
If Txtfuente <> "" Then Criterio = Criterio & "Fuente" = "' & Txtfuente & " And ""
'Elimino el ultimo And de la consulta
'Valido la consulta
On Error GoTo Valida
Criterio = Trim(Mid(Criterio, 1, Len(Criterio) - 5))
Valida:
MsgBox "Debe cubrir al menos un campo para la búsqueda", vbInformation + vbOKOnly
Txt(0).SetFocus
End Function
Function Campos(indice As Integer) As String
If indice = 0 Then Campos = "Refprov"
If indice = 1 Then Campos = "Ref"
If indice = 2 Then Campos = "PasC"
If indice = 3 Then Campos = "Tencod"
If indice = 4 Then Campos = "Teje"
If indice = 5 Then Campos = "Cons"
If indice = 6 Then Campos = "CAxial"
If indice = 7 Then Campos = "CRadial"
If indice = 8 Then Campos = "Nrev"
If indice = 9 Then Campos = "Nimp"
If indice = 10 Then Campos = "RV"
If indice = 11 Then Campos = "RC"
If indice = 12 Then Campos = "FT"
If indice = 13 Then Campos = "TA"
If indice = 14 Then Campos = "TF"
If indice = 15 Then Campos = "IP"
If indice = 16 Then Campos = "Hum"
End Function
Tengo un formulario en el que pretendo hacer una búsqueda en 5 control data a la vez pasando como parametros n campos comunes y me da un error en la Function criterios donde dice que no coinciden los tipos;creo que la busqueda que la hago por Sql está mal pero no se bien donde a continuacion pego todo el codigo del formulario para ver si alguien me da con el problema.GRACIAS
Option Explicit
Dim i As Integer
Private Sub Check1_LostFocus()
Txtinterface.Enabled = Check1
If Txtinterface.Enabled = True Then
Txtinterface.SetFocus
Else
Txtcircuito.SetFocus
End If
End Sub
Private Sub Cmdaceptar_Click()
Dim aux As Integer
For i = 0 To 5
If Check(i).Value = 1 Then
aux = Check(i).Value
End If
Next
If aux = 0 Then
MsgBox "No ha marcado ninguna cuadrÃcula.Marque al menos una", vbInformation + vbOKOnly, "Atención :"
Check(0).SetFocus
Exit Sub
Else
activar_datos
'Con este código habilito los datas de los check marcados para realizar la búsqueda
Data(0).Enabled = Check(0) 'Data para Hohner
Data(1).Enabled = Check(1) 'Givi
Data(2).Enabled = Check(2) 'Elgo
Data(3).Enabled = Check(3) 'Posital Fraba
Data(4).Enabled = Check(4) 'Scancon
Data(5).Enabled = Check(5) 'WayCon
End If
End Sub
Private Sub CmdAceptar2_Click()
For i = 0 To 5
If Data(i).Enabled = True Then
'Puse una comilla simple antes del where
Data(i).RecordSource = "select * from " & TABLA & "'where" & Criterio
Data(i).Refresh
End If
Next
DBG.Visible = True
End Sub
Private Sub Cmdbuscar_Click()
marco_opciones.Enabled = False
marco_botones.Enabled = False
marco_datos.Enabled = True
End Sub
Private Sub Cmdcancelar_Click()
For i = 0 To 5
Data(i).Enabled = False
Next
desactivar_datos
Cmdsalir.SetFocus
End Sub
Private Sub CmdCancelar2_Click()
activar_datos
Cmdbuscar.SetFocus
End Sub
Private Sub Cmdsalir_Click()
Unload Me
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 13
SendKeys "{tab}"
Case 27
Unload Me
End Select
End Sub
Private Sub activar_datos()
marco_opciones.Enabled = False
marco_datos.Enabled = False
marco_botones.Enabled = True
End Sub
Private Sub desactivar_datos()
marco_opciones.Enabled = True
marco_datos.Enabled = False
marco_botones.Enabled = False
End Sub
Private Sub Form_Load()
desactivar_datos
End Sub
Private Sub Form_Unload(Cancel As Integer)
If marco_datos.Enabled = True Then
Cancel = True
End If
End Sub
Private Sub Option1_LostFocus()
If Option1.Value = True Then
Txt(2).Enabled = True
Txtgiro.Enabled = False
Txtcodigo.Enabled = False
Txt(2).SetFocus
End If
End Sub
Private Sub Option2_LostFocus()
If Option2.Value = True Then
Txt(2).Enabled = False
Txtgiro.Enabled = True
Txtcodigo.Enabled = True
Txtgiro.SetFocus
End If
End Sub
Private Sub Txtbrida_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtcircuito_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtcodigo_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtconexion_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txteje_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtfuente_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtgiro_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub Txtinterface_KeyPress(KeyAscii As Integer)
'Bloqueo teclado
If InStr(KeyAscii, Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Function TABLA() As String
If Data(i) = Data(0) Then TABLA = "Encoder_Hohner"
If Data(i) = Data(1) Then TABLA = "Encoder_Givi"
If Data(i) = Data(2) Then TABLA = "Encoder_Elgo"
If Data(i) = Data(3) Then TABLA = "Encoder_Posital"
If Data(i) = Data(4) Then TABLA = "Encoder_Scancon"
If Data(i) = Data(5) Then TABLA = "Encoder_WayCon"
End Function
Function Criterio() As String
'Cambie la i por la j
Dim j As Integer
'Criterio = ""
For j = 0 To 16
If Txt(j) <> "" Then
Criterio = Criterio & Campos(j) = "' & Txt(j) & " And ""
End If
Next
'Elijo los campos de los ComboBox y DataCombo
If Txtgiro <> "" Then Criterio = Criterio & "Giro" = "' & Txtgiro & " And ""
If Txtcodigo <> "" Then Criterio = Criterio & "Tipcod" = "' & Txtcodigo & " And ""
If Txteje <> "" Then Criterio = Criterio & "Tipeje" = "' & Txteje & " And ""
If Txtbrida <> "" Then Criterio = Criterio & "Brida" = "' & Txtbrida & " And ""
If Txtconexion <> "" Then Criterio = Criterio & "Tipcon" = "' & Txtconexion & " And ""
If Txtinterface <> "" Then Criterio = Criterio & "Inter" = "' & Txtinterface & " And ""
If Txtcircuito <> "" Then Criterio = Criterio & "CS" = "' & Txtcircuito & " And ""
If Txtfuente <> "" Then Criterio = Criterio & "Fuente" = "' & Txtfuente & " And ""
'Elimino el ultimo And de la consulta
'Valido la consulta
On Error GoTo Valida
Criterio = Trim(Mid(Criterio, 1, Len(Criterio) - 5))
Valida:
MsgBox "Debe cubrir al menos un campo para la búsqueda", vbInformation + vbOKOnly
Txt(0).SetFocus
End Function
Function Campos(indice As Integer) As String
If indice = 0 Then Campos = "Refprov"
If indice = 1 Then Campos = "Ref"
If indice = 2 Then Campos = "PasC"
If indice = 3 Then Campos = "Tencod"
If indice = 4 Then Campos = "Teje"
If indice = 5 Then Campos = "Cons"
If indice = 6 Then Campos = "CAxial"
If indice = 7 Then Campos = "CRadial"
If indice = 8 Then Campos = "Nrev"
If indice = 9 Then Campos = "Nimp"
If indice = 10 Then Campos = "RV"
If indice = 11 Then Campos = "RC"
If indice = 12 Then Campos = "FT"
If indice = 13 Then Campos = "TA"
If indice = 14 Then Campos = "TF"
If indice = 15 Then Campos = "IP"
If indice = 16 Then Campos = "Hum"
End Function
Naveguito, estás cometiendo varios fallos en la función criterio:
examinemos el for j =0 to 16
cuando pones
Criterio = Criterio & Campos(j) = "' & Txt(j) & " And ""
estás usando una comparación, ese signo de = no se está incluyendo en la cadena Criterio. para que funcionara serÃa:
Criterio = Criterio & Campos(j) & "= "' & Txt(j) & "'" And ""
Por otro lado, al hacer Criterio=criterio . . ., estás convirtiendo sin querer la función en recursiva, o sea, que vuelves a llamar a la función criterio.
En el resto de las lÃneas haces cosas parecidas. Yo creo que le estás dando vueltas a una idea que tienes en la cabeza que no es correcta.
Por ejemplo, la función TABLA, se resolverÃa muy facilmente haciendo:
Function TABLA(indice) As String
If indice=0 Then TABLA = "Encoder_Hohner"
If indice=1 Then TABLA = "Encoder_Givi"
.
.
.
End Function
Tal y como has hecho con la función campos.
Por supuesto que se puede mejorar con un select case, pero haciéndolo asà tampoco serÃa un error.
Saludos
examinemos el for j =0 to 16
cuando pones
Criterio = Criterio & Campos(j) = "' & Txt(j) & " And ""
estás usando una comparación, ese signo de = no se está incluyendo en la cadena Criterio. para que funcionara serÃa:
Criterio = Criterio & Campos(j) & "= "' & Txt(j) & "'" And ""
Por otro lado, al hacer Criterio=criterio . . ., estás convirtiendo sin querer la función en recursiva, o sea, que vuelves a llamar a la función criterio.
En el resto de las lÃneas haces cosas parecidas. Yo creo que le estás dando vueltas a una idea que tienes en la cabeza que no es correcta.
Por ejemplo, la función TABLA, se resolverÃa muy facilmente haciendo:
Function TABLA(indice) As String
If indice=0 Then TABLA = "Encoder_Hohner"
If indice=1 Then TABLA = "Encoder_Givi"
.
.
.
End Function
Tal y como has hecho con la función campos.
Por supuesto que se puede mejorar con un select case, pero haciéndolo asà tampoco serÃa un error.
Saludos
