Está en la página 1de 4

Conexión a DB Access Visual Basic 2012

Creación del proyecto [FORMULARIO DE BUSQUEDA]


Vamos a crear ahora el formulario que nos servirá para buscar un
paciente específico y presentar sus datos en el formulario que
hace la llamada de búsqueda. Para saber cómo llamar al formulario
de búsqueda vea el apartado “Para llamar al formulario de búsqueda
del paciente.” Presentado anteriormente en este instructivo.

Al finalizar los siguientes pasos su formulario de búsqueda deberá


quedar masa o menos así:

1. Proyecto >> Agregar Windows Form


a. En nombre poner FFormulario
2. Vamos a la propiedad llamada ControlBox y el valor True lo
cambiamos por false
3. Agregamos 1 GroupBox, 1 Label, 1 TextBox, 5 Radio Button, 2
Buttons y 1 DataGridView, y lo arreglamos de manera que quede
como en la imagen, los nombres los ponemos de la siguiente
forma:

TextBox1 TxtIndicio
RadioButton1 RBNombre
RadioButton2 RBApellido
RadioButton3 RBDireccion
RadioButton4 RBTelefono
RadioButton5 RBCedula
Button1 BtnBuscar
Button2 BtnVolver
DataGridView1 DGVDatos

Ricky Anthonelly Jiménez Sosa


Conexión a DB Access Visual Basic 2012

4. Ya teniendo el diseño formulado empezamos a agregar el código


5. Damos clic derecho sobre el Form y presionamos ver código,
encima de [Public Class…] agregamos:
Imports System.Data.OleDb

6. En el evento Load del form ponemos:


conexion.Open()
sql = "SELECT * FROM Paciente ORDER by ID_Paciente"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
7. En el evento Click del [BtnBuscar] ponemos:

If RBNombre.Checked = True Then


conexion.Open()
sql = "SELECT * FROM Paciente ORDER by Nombre"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBApellido.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente ORDER by Apellidos"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBDireccion.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente ORDER by Direccion"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If

Ricky Anthonelly Jiménez Sosa


Conexión a DB Access Visual Basic 2012

If RBTelefono.Checked = True Then


conexion.Open()
sql = "SELECT * FROM Paciente ORDER by Telefono"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBCedula.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente ORDER by Cedula"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If

8. En el evento CellContentDoubleClick del [DGVDatos] ponemos:


Dim celda As DataGridViewRow = DataGridView1.CurrentRow
dataindent = CStr(celda.Cells(0).Value)
Pacientes.TxtCodigo.Text = dataindent
Pacientes.TxtCodigo_Validated(sender, e)
Me.Close()

9. En el evento TextChanged del [TxtIndicio] ponemos:


Dim Likeit As String
Likeit = TxtIndicio.Text
If RBNombre.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente WHERE Nombre LIKE '%" & Likeit & "%' ORDER by
Nombre"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBApellido.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente WHERE Apellidos LIKE '%" & Likeit & "%' ORDER
by Apellidos"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")

Ricky Anthonelly Jiménez Sosa


Conexión a DB Access Visual Basic 2012

DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBDireccion.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente WHERE Direccion LIKE '%" & Likeit & "%' ORDER
by Direccion"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBTelefono.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente WHERE Telefono LIKE '%" & Likeit & "%' ORDER by
Telefono"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If
If RBCedula.Checked = True Then
conexion.Open()
sql = "SELECT * FROM Paciente WHERE Cedula LIKE '%" & Likeit & "%' ORDER by
Cedula"
adaptar = New OleDbDataAdapter(sql, conexion)
conexion.Close()
almacen = New DataSet
adaptar.Fill(almacen, "Paciente")
DGVDatos.DataSource = almacen
DGVDatos.DataMember = "Paciente"
DGVDatos.Refresh()
DGVDatos.ReadOnly = True
End If

10. En el evento Click de [BtnVolver]


Me.Close()
Pacientes.Show()

Ricky Anthonelly Jiménez Sosa

También podría gustarte