¿Como mostrar filtrados los registros en un formulario según los 3 primeros caracteres a través de un botón?

Mi duda es cómo conseguir a través del código VBA que una macro me muestre en el formulario solamente los registros que empiecen por 3 caracteres que eliga.

El código que tengo es este pero no me funciona, no sé si es porque la base de datos al que lo quiero adaptar es ACCESS 2010.

' Buscar_reguistro1
'------------------------------------------------------------
Function Buscar_reguistro1()
On Error GoTo Buscar_reguistro1_Err

With CodeContextObject
' Adjunta al formulario Lista de teléfonos de clientes.
' Adjunta al evento Después de actualizar del grupo de opciones FiltrosNombreCompañía.
If (.FiltrosClave = 1) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6J0*""", ""
End If
If (.FiltrosClave = 2) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6J3*""", ""
End If
If (.FiltrosClave = 3) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6J4*""", ""
End If
If (.FiltrosClave = 4) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5P0*""", ""
End If
If (.FiltrosClave = 5) Then
DoCmd.ApplyFilter "", "[Clave] Like ""1B*""", ""
End If
If (.FiltrosClave = 6) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6Q0.803.*""", ""
End If
If (.FiltrosClave = 7) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6Q0.80[24].[25]*[!5]B""", ""
End If
If (.FiltrosClave = 8) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6Q0.809.*""", ""
End If
If (.FiltrosClave = 9) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6Q[06].81[34].1[14][!9]*""", ""
End If
If (.FiltrosClave = 10) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6R0*""", ""
End If
If (.FiltrosClave = 11) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6R4*""", ""
End If
If (.FiltrosClave = 12) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6R6*""", ""
End If
If (.FiltrosClave = 13) Then
DoCmd.ApplyFilter "", "[Clave] Like ""6J8*""", ""
End If
If (.FiltrosClave = 14) Then
DoCmd.ApplyFilter "", "[Clave] Like ""8E*""", ""
End If
If (.FiltrosClave = 15) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5F0*""", ""
End If
If (.FiltrosClave = 16) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5F3*""", ""
End If
If (.FiltrosClave = 17) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5F9*""", ""
End If
If (.FiltrosClave = 18) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5F4*""", ""
End If
If (.FiltrosClave = 19) Then
DoCmd.ApplyFilter "", "[Clave] Like ""5P8*""", ""
End If
If (.FiltrosClave = 20) Then
DoCmd.ApplyFilter "", "[Clave] Like ""3R*""", ""
End If
If (.FiltrosClave = 21) Then
DoCmd.ApplyFilter "", "[Clave] Like ""8U0*""", ""
End If
If (.FiltrosClave = 26) Then
DoCmd.ApplyFilter "", "[Clave] Like ""1[KQ]*""", ""
End If
If (.FiltrosClave = 27) Then
' Mostrar todos los registros.
DoCmd.ShowAllRecords
End If
If (.RecordsetClone.RecordCount > 0) Then
' Si se devuelven registros para la letra seleccionada, ir al control NombreCompañía.
DoCmd.GoToControl "Clave"
' Detener la macro.
Exit Function
End If
If (.RecordsetClone.RecordCount = 0) Then
' Si no se devuelve ningún registro para la letra seleccionada, mostrar un mensaje.
Beep
MsgBox "No hay registros para esa letra.", vbInformation, "No Records Returned"
' Mostrar todos los registros.
DoCmd. ShowAllRecords
' Presionar el botón Todos.
.FiltrosClave = 27
End If
End With

Buscar_reguistro1_Exit:
Exit Function

Buscar_reguistro1_Err:
MsgBox Error$
Resume Buscar_reguistro1_Exit

End Function

1 Respuesta

Respuesta
1

Sin profundizar en el código y asì a primera vista se me ocurre que puede ser:

Tu pones:

DoCmd.ApplyFilter "", "[Clave] Like ""6J3*""", ""

No veo el sentido de la coma y las comillas finales,""

Prueba con:

DoCmd.ApplyFilter "", "[Clave] Like ""6J3*"""

Este formato lo aplicas a todos y si no te funciona me miro más profundamente el conjunto

Ya me comentarás: Jacinto

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas