Vous êtes sur la page 1sur 5

# Option Explicit # # '***************************************************************************** ****** # ' Ejemplo para buscar en un DataGrid con el mtodo Find del recordset # # 'Controles

: 1 - La referencia a Ado _ # 2 - Un control DataGrid ( DataGRid1 ) _ # 3 - Un control Textbox (Text1) _ # 4 - Dos CommandButon ( Command1 y Command2 ) _ # 5 - Un Combobox ( Combo1 - Para los campos ) _ # 6 - Un CheckBox _ # 7 - Indicar en la constante s_CONNECTION_STRING la _ # cadena de conexin para la base de datos _ # 8 - Indicar en el FormLoad, la consulta Sql para el recordset # # # # '***************************************************************************** ****** # # '***************************************************************************** ****** # # ' ConnectionString # Private Const s_CONNECTION_STRING As String = "Provider=Microsoft.Jet.OLEDB.4. 0;" & _ # "Data Source=C:\Archivos de progr ama" & _ # "\Microsoft Visual Studio\VB98\" & _ # "NWIND.MDB;Persist Security Info= False" # # ' Colores de fondo para los textbox mientras se busca # Private Const COLOR_TEXTBOX_NO_FOUND As Long = &H8080FF # Private Const COLOR_TEXTBOX_FOUND As Long = &HC0FFFF # Private Const COLOR_TEXTBOX_NORMAL As Long = vbWhite # '***************************************************************************** ****** # # # ' Variable de tipo Recordset y con evento # Private WithEvents Recordset As ADODB.Recordset # 'Botn para buscar hacia atrs # Private Sub Command1_Click() # # ' Si llega al final posiciona el recordset en el ltimo registro # If Recordset.EOF Then # Recordset.MoveLast # End If # ' Habilita y deshabilita los command de buscar # If Recordset.BOF Or Recordset.AbsolutePosition <= 1 Then # Command1.Enabled = False # Command2.Enabled = True # Command2.SetFocus # Exit Sub # End If # # Dim Anterior As Long

# ' guarda la posicin del Registro anterior # Anterior = Recordset.AbsolutePosition # ' Mueve el cursor # Recordset.Move 0, Recordset.Bookmark - 1 # # ' Busca en cualquier parte de la cadena # If Check1.Value = 0 Then # Recordset.Find Combo1.Text & " LIKE '*" + Text1.Text + "*'", , adSearc hBackward # ' Busca la cadena completa # ElseIf Check1.Value = 1 Then # Recordset.Find Combo1.Text & "='" + Text1.Text + "'", , adSearchBackwa rd # End If # # Command2.Enabled = True # ' Si llega al principio , selecciona la ltima fila encontrada # If Recordset.BOF Then # Recordset.AbsolutePosition = Anterior # Command1.Enabled = False # Command2.SetFocus # End If # # End Sub # 'Botn para buscar hacia adelante # Private Sub Command2_Click() # ' Si llega al principio posiciona el recordset en el primer registro # If Recordset.BOF Then # Recordset.MoveFirst # End If # ' Habilita y deshabilita los botones para buscar # If Recordset.EOF Or Recordset.AbsolutePosition >= Recordset.RecordCount Th en # Command2.Enabled = False # Command1.Enabled = True # Command1.SetFocus # Exit Sub # End If # # Dim Anterior As Long # ' Almacena la fila actual # Anterior = Recordset.AbsolutePosition # ' Mueve un registro hacia atrs # Recordset.Move 0, Recordset.Bookmark + 1 # # ' Busca en cualquier parte de la cadena # If Check1.Value = 0 Then # Recordset.Find Combo1.Text & " LIKE '*" + Text1.Text + "*'", , adSearc hForward # ' Busca la cadena completa # ElseIf Check1.Value = 1 Then # Recordset.Find Combo1.Text & "='" + Text1.Text + "'", , adSearchForwar d # End If # # Command1.Enabled = True # # If Recordset.EOF Then # Command2.Enabled = False # Command1.SetFocus

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Recordset.AbsolutePosition = Anterior End If End Sub Private Sub Form_Load() ' Variable para la conexin de Ado Dim db As ADODB.Connection ' Nuevo objeto Connection Set db = New ADODB.Connection db.CursorLocation = adUseClient ' Abre la base de datos pasando la cadena de conexin db.Open s_CONNECTION_STRING ' crea un Recordset Set Recordset = New Recordset ' Abre el Recordset con la consulta Sql Recordset.Open "Select [NombreContacto]," & _ "[NombreCompaa]," & _ "[CargoContacto] " & _ "From Proveedores Order By NombreContacto Asc", _ db, adOpenStatic, adLockOptimistic Dim i As Integer 'Recorre los campos del recordset para aadirlos al combobox 'Nota: los campos deben ser de tipo String For i = 0 To Recordset.Fields.Count - 1 Combo1.AddItem Recordset.Fields.Item(i).Name Next i ' Selecciona el campo 1 Combo1.ListIndex = 0 ' Engancha el recordset al datagrid Set DataGrid1.DataSource = Recordset ' Opcional . esto hace que se seleccione la fila completa en el DataGrid DataGrid1.MarqueeStyle = dbgHighlightRowRaiseCell Text1 = "" 'caption de los controles Command1.Caption = "Quitar Filtro" Command1.Caption = " << Buscar Anterior " Command2.Caption = " Buscar Siguiente >> " Check1.Caption = " Buscar palabra completa" End Sub Private Sub Text1_Change() If Text1 <> "" Then

# # ' Habilita los botones para buscar hacia atrs y hacia adelante # Command1.Enabled = True # Command2.Enabled = True # # Dim Actual As Long # ' almacena la fila actual, por si no so se encontr vuelve a posicionar _ # el recordset en dicha fila # If Not Recordset.EOF And Not Recordset.BOF Then # Actual = Recordset.AbsolutePosition # End If # # ' Busca en cualquier parte de la cadena # If Check1.Value = 0 Then # Recordset.Find Combo1.Text & " LIKE '*" & Text1.Text & "*'", , adS earchForward # ' Busca la Cadena completa # ElseIf Check1.Value = 1 Then # Recordset.Find Combo1.Text & "='" & Text1.Text & "'", , adSearchFo rward # End If # # ' Color del fondo del textbox # If Not Recordset.EOF And Not Recordset.BOF Then # Text1.BackColor = COLOR_TEXTBOX_FOUND # Else # Text1.BackColor = COLOR_TEXTBOX_NO_FOUND # End If # # If Recordset.BOF Or Recordset.EOF Then # Recordset.AbsolutePosition = Actual # End If # ' Si el textbox est vaco, posiciona el recordset en el primer registro # Else # # Command1.Enabled = False # Command2.Enabled = False # # Recordset.MoveFirst # # Set DataGrid1.DataSource = Recordset # ' Color de fondo del textbox cuando est vaco # Text1.BackColor = COLOR_TEXTBOX_NORMAL # # End If # # End Sub # # ' Evento que se dispara cuando se cambia de posicin en el recordset # Private Sub Recordset_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, _ # ByVal pError As ADODB.Error, adStatus As ADODB .EventStatusEnum, _ # ByVal pRecordset As ADODB.Recordset) # # ' Muestra en el caption del formulario el nmero registro actual # Me.Caption = " Registro actual: " & CStr(Recordset.AbsolutePosition) # # End Sub

# # # _ # # _ # # # # # # # # # # # # # # # # # #

' Cuando se produce un error en el recordset se dispara este evento Private Sub Recordset_Error(ByVal ErrorNumber As Long, Description As String, ByVal Scode As Long, ByVal Source As String, _ ByVal HelpFile As String, ByVal HelpContext As Long, fCancelDisplay As Boolean) ' Mostramos el error MsgBox " Descripcin del Error :" & Description, vbCritical End Sub ' Cierra el recordset y Descarga la referencia Private Sub Form_Unload(Cancel As Integer) ' Cierra If Recordset.State = adStateOpen Then Recordset.Close End If ' descarga If Not Recordset Is Nothing Then Set Recordset = Nothing End If End Sub

Vous aimerez peut-être aussi