Option Explicit On
Option Strict On
'importamos la libreia Microsoft Access 14.0 library
'Imports Access = Microsoft.Office.Interop.Access
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Windows.Forms
Public Class Archivos
'La conexión
Dim connString As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
& "D:\BASE DATOS\Access y System.OleDb en VB.NET 2012\Carpeta\Mi_base.accdb" & ";Persist Security Info=False;") 'CAMBIA LA RUTA
Dim Conexión1 As OleDbConnection = New OleDbConnection
'para buscar
Public dr As OleDbDataReader
'Para el DataGribView(DGV) en inicio
Dim ds As DataSet = New DataSet
Dim Oda As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim ruta As String = "D:\BASE DATOS\Access y System.OleDb en VB.NET 2012\Carpeta\Mi_base.accdb" 'CAMBIA LA RUTA
Dim Nombre As String
Dim a As Boolean = True, b As Boolean = False
'Sub para cargar los datos en el DataGribView
Sub cargar()
Try
'Conectamos y abrimos
Conexión1.ConnectionString = connString
Conexión1.Open()
ds = New DataSet
Dim table As DataTableCollection = ds.Tables
Oda = New OleDbDataAdapter("Select [Clave], [Nombres], [medidas], [peso], [long], [sup], [corp], [kam], [prom], [manr], [bau], [madd], [peris], [fecha] from Sujeto", Conexión1)
Oda.Fill(ds, "Sujeto")
'Asignamos
DGV.DataSource = New DataView(table(0))
DGV.Columns(1).Width = 180 'Aumentamos el ancho de la columna Nombres
DGV.Refresh()
'Liberamos
ds.Dispose()
Oda.Dispose()
Conexión1.Close() 'Cerramos
Catch ex As Exception
ex.Message.ToUpper()
End Try
End Sub
'Habilitar controles según los registros
Sub comprueba()
If DGV.Rows.Count > -1 Then
TextBox1.Enabled = True
ComboBox1.Enabled = True
Buscar.Enabled = True
Editar.Enabled = False
Guardar.Enabled = False
Eliminar.Enabled = False
Registrar.Enabled = True
Label4.Text = "Al buscar y encontrar el registro, puedes editar todo, excepto la Clave."
End If
If DGV.Rows.Count < 1 Then
TextBox1.Enabled = False
ComboBox1.Enabled = False
Buscar.Enabled = False
Editar.Enabled = False
Guardar.Enabled = False
Eliminar.Enabled = False
Registrar.Enabled = True
Label4.Text = "Registre información"
End If
End Sub
'Habilita controles
Sub habilita()
DGV.ReadOnly = True
Editar.Text = "Editar"
Editar.Enabled = False
Guardar.Enabled = False
Buscar.Enabled = True
Eliminar.Enabled = False
Registrar.Enabled = True
TextBox1.Enabled = True
TextBox1.Text = ""
ComboBox1.Enabled = True
End Sub
'El dataset
Sub Datas()
With ds.Tables("Sujeto")
'Indicar en la propiedad Sort el campo por el cual buscar
.DefaultView.Sort = "Clave"
'Asignar el origen de datos a la grilla
DGV.DataSource = .DefaultView
End With
End Sub
'Actualizar
Sub Actualizar()
ds.Clear()
DGV.Refresh()
cargar()
Datas()
comprueba()
TextBox1.Text = ""
ComboBox1.Text = "Clave"
End Sub
'Si cerramos
Private Sub Archivos_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Registro.Show()
End Sub
'Cofig.. de inicio
Private Sub Archivos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: esta línea de código carga datos en la tabla 'Mi_baseDataSet.Sujeto' Puede moverla o quitarla según sea necesario.
Me.SujetoTableAdapter.Fill(Me.Mi_baseDataSet.Sujeto)
Try
Me.CenterToScreen()
ComboBox1.Text = "Clave"
'Llenamos el DGV
cargar()
comprueba()
With DGV
.ReadOnly = True
'Para seleccionar todo, pero en este caso solo la fila a buscar
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
'Evitamos que se agregue la última fila(Fila vacía)
.AllowUserToAddRows = False
End With
Datas()
Catch ex As Exception
ex.Message.ToUpper()
End Try
End Sub
'Buscamos el registro y nos posicionamos en la fila correspondiente en el Dgv
Private Sub Buscar_Click(sender As Object, e As EventArgs) Handles Buscar.Click
Try
If TextBox1.Text = "" Then Exit Sub
If DGV.Rows.Count < 1 Then Exit Sub
With ds.Tables("Sujeto")
'Buscamos por la Clave
If ComboBox1.Text = "Clave" Then
.DefaultView.Sort = "Clave"
If DGV.Rows.Count > 0 Then
Label4.Text = "Al buscar y encontrar el registro, puedes editar todo, excepto la Clave."
End If
End If
'Buscamos por los nombres
If ComboBox1.Text = "Nombres" Then
.DefaultView.Sort = "Nombres"
If DGV.Rows.Count > 0 Then
Label4.Text = "Al buscar y encontrar el registro, puedes editar todo, excepto los Nombres."
End If
End If
End With
' buscar y retornar el índice de la fila a buscar
Dim fila As Integer = ds.Tables("Sujeto").DefaultView.Find(TextBox1.Text)
' si el índice es -1 no se encontró
If fila <> -1 Then
b = True
Editar.Enabled = True
Eliminar.Enabled = True
Registrar.Enabled = False
With DGV
' Establecer el índice de la primera fila del DataGridview para visualizarlo primero
.FirstDisplayedScrollingRowIndex = fila
.CurrentCell = .Rows(fila).Cells(0) 'Asignar la celda al CurrentCell para seleccionarla
.Select() 'Seleccionar el control
End With
Else
MsgBox("No se pudo encontrar el registro!", vbInformation, "Buscar registro")
End If
Catch ex As Exception
ex.Message.ToUpper()
End Try
End Sub
'Editar
Private Sub Editar_Click(sender As Object, e As EventArgs) Handles Editar.Click
If TextBox1.Text <> "" And b = True Then
If a = True Then
DGV.ReadOnly = False
Editar.Text = "Cancelar"
Guardar.Enabled = True
Buscar.Enabled = False
Eliminar.Enabled = False
Registrar.Enabled = False
TextBox1.Enabled = False
ComboBox1.Enabled = False
a = False
ElseIf a = False Then
habilita()
'Limpiamos para que no se repitan los datos
ds.Clear()
DGV.Refresh()
cargar()
a = True
b = False
End If
End If
End Sub
'Actualizamos los datos
Private Sub Guardar_Click(sender As Object, e As EventArgs) Handles Guardar.Click
Conexión1.ConnectionString = connString
Conexión1.Open()
'
Dim row As DataGridViewRow = DGV.CurrentRow
Dim str As String
Convert.ToString(row.Cells("Nombres").Value)
'Si buscamos por la clave
If ComboBox1.Text = "Clave" Then
End If
str = "update [Sujeto] set [Nombres] = '" & Convert.ToString(row.Cells("Nombres").Value) & _
"' , [medidas] = '" & Convert.ToString(row.Cells("medidas").Value) & _
"', [peso] = '" & Convert.ToString(row.Cells("peso").Value) & _
"', [long] = '" & Convert.ToString(row.Cells("long").Value) & _
"', [sup] = '" & Convert.ToString(row.Cells("sup").Value) & _
"', [corp] = '" & Convert.ToString(row.Cells("corp").Value) & _
"', [kam] = '" & Convert.ToString(row.Cells("kam").Value) & _
"', [prom] = '" & Convert.ToString(row.Cells("prom").Value) & _
"', [manr] = '" & Convert.ToString(row.Cells("manr").Value) & _
"', [bau] = '" & Convert.ToString(row.Cells("bau").Value) & _
"', [madd] = '" & Convert.ToString(row.Cells("madd").Value) & _
"', [peris] = '" & Convert.ToString(row.Cells("peris").Value) & _
"', [fecha] = '" & Convert.ToString(row.Cells("fecha").Value) & "' Where [Clave] = '" & TextBox1.Text & "'"
Dim cmd1 As OleDbCommand = New OleDbCommand(str, Conexión1)
Try
cmd1.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message.ToUpper)
End Try
'Si se busco por el nombre
If ComboBox1.Text = "Clave" Then
str = "update [Sujeto] set [Nombres] = '" & Convert.ToString(row.Cells("Nombres").Value) & _
"' , [medidas] = '" & Convert.ToString(row.Cells("medidas").Value) & _
"', [peso] = '" & Convert.ToString(row.Cells("peso").Value) & _
"', [long] = '" & Convert.ToString(row.Cells("long").Value) & _
"', [sup] = '" & Convert.ToString(row.Cells("sup").Value) & _
"', [corp] = '" & Convert.ToString(row.Cells("corp").Value) & _
"', [kam] = '" & Convert.ToString(row.Cells("kam").Value) & _
"', [prom] = '" & Convert.ToString(row.Cells("prom").Value) & _
"', [manr] = '" & Convert.ToString(row.Cells("manr").Value) & _
"', [bau] = '" & Convert.ToString(row.Cells("bau").Value) & _
"', [madd] = '" & Convert.ToString(row.Cells("madd").Value) & _
"', [peris] = '" & Convert.ToString(row.Cells("peris").Value) & _
"', [fecha] = '" & Convert.ToString(row.Cells("fecha").Value) & "' Where [Clave] = '" & TextBox1.Text & "'"
End If
Conexión1.Close()
Conexión1.Close()
'Actualizamos
ds.Clear()
DGV.Refresh()
cargar()
Datas()
comprueba()
habilita()
a = True
b = False
End Sub
'Eliminamos el registro que se busco
Private Sub Eliminar_Click(sender As Object, e As EventArgs) Handles Eliminar.Click
If DGV.Rows.Count < 1 Then Exit Sub
If MsgBox("Deseas eliminar el registro?", CType(vbQuestion + vbYesNo, MsgBoxStyle), _
"Eliminar registro") = vbNo Then Exit Sub
Conexión1.ConnectionString = connString
Conexión1.Open()
Dim str As String
str = "Delete from Sujeto Where Clave = '" & TextBox1.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, Conexión1)
TextBox1.Text = ""
Try
cmd.ExecuteNonQuery()
MsgBox("Registro eliminado", MsgBoxStyle.Exclamation, "Eliminar registro")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Conexión1.Close()
'Actualizamos
Actualizar()
End Sub
'Nos movemos al Form registro
Private Sub Registrar_Click(sender As Object, e As EventArgs) Handles Registrar.Click
Registro.Show()
Me.Hide()
End Sub
'Actualizamos o restablecemos
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Actualizar()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Registro.Show()
Me.Hide()
End Sub
End Class