CODIGO DE FUNCIONAMIENTO DE UN FORMULARIO, GUARDAR – BUSCAR – ACTUALIZAR - ELIMINAR Imports System.Data Imports System.Data.OleDb Public Class Acciones Dim conexion As New OleDbConnection Dim comandos As New OleDbCommand Dim adaptador As New OleDbDataAdapter Dim registro As New DataSet Dim lector As OleDbDataReader Dim consulta As String Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnSalir.Click If txtCodigo.Text = "" And txtNombres.Text = "" And txtApellidos.Text = "" And txtTelefono.Text = "" And txtEmail.Text = "" Then MsgBox("No debe dejar campos vacios", vbCritical, "Vacios!!!!!") Else Try comandos = New OleDbCommand("INSERT INTO Contactos (Codigo,Nombres,Apellidos,Telefono,Email) VALUES(txtCodigo,txtNombres,txtApellidos,txtTelefono,txtEmail)", conexion) comandos.Parameters.AddWithValue("@Codigo", txtCodigo.Text) comandos.Parameters.AddWithValue("@Nombres", txtNombres.Text) comandos.Parameters.AddWithValue("@Apellidos", txtApellidos.Text) comandos.Parameters.AddWithValue("@Telefono", txtTelefono.Text) comandos.Parameters.AddWithValue("@Email", txtEmail.Text) comandos.ExecuteNonQuery() MsgBox("Los datos han sido guardados", vbInformation, "Exito!!!") datagridContacto() limpiar() Catch ex As Exception MsgBox("No fue posible guardar los datos", vbCritical, "Error!!!") End Try End If End Sub Private Sub limpiar() txtCodigo.Clear() txtNombres.Clear() txtApellidos.Clear() txtTelefono.Clear() txtEmail.Clear() txtCodigo.Focus() End Sub Private Sub Acciones_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed conexion.Close() End Sub Private Sub datagridContacto() Dim adaptador As New OleDbDataAdapter Dim registros As New DataSet Dim consulta As String consulta = "SELECT * FROM Contactos" adaptador = New OleDbDataAdapter(consulta, conexion) registros.Tables.Add("Contactos") adaptador.Fill(registros.Tables("Contactos")) dgvcontactos.DataSource = registros.Tables("Contactos") End Sub Private Sub Acciones_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try conexion.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=listado.accdb" conexion.Open() datagridContacto() Catch ex As Exception MsgBox("Error en la conexion a la BD", vbCritical, "Error de Conexion") End Try End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click conexion.Close() End End Sub Private Sub btnBuscar_Click(sender As Object, e As EventArgs) Handles btnBuscar.Click Dim buscar As String Dim lista As Byte buscar = "SELECT * FROM Contactos WHERE codigo ='" & txtCodigo.Text & "'" adaptador = New OleDbDataAdapter(buscar, conexion) registro = New DataSet adaptador.Fill(registro, "Contactos") lista = registro.Tables("Contactos").Rows.Count If lista <> 0 Then dgvcontactos.DataSource = registro dgvcontactos.DataMember = "Contactos" txtCodigo.Text = registro.Tables("Contactos").Rows(0).Item("Codigo") txtNombres.Text = registro.Tables("Contactos").Rows(0).Item("Nombres") txtApellidos.Text = registro.Tables("Contactos").Rows(0).Item("Apellidos") txtTelefono.Text = registro.Tables("Contactos").Rows(0).Item("Telefono") txtEmail.Text = registro.Tables("Contactos").Rows(0).Item("Email") Else MsgBox("El dato no existe en la BD", vbCritical, "Error!!!!") limpiar() End If End Sub Private Sub btnActualizar_Click(sender As Object, e As EventArgs) Handles btnActualizar.Click Dim actualizar As String txtApellidos.Enabled = True txtNombres.Enabled = True txtTelefono.Enabled = True txtEmail.Enabled = True actualizar = "UPDATE Contactos SET Nombres = '" & txtNombres.Text & "', Apellidos= '" & txtApellidos.Text & "', Telefono = '" & txtTelefono.Text & "', Email= '" & txtEmail.Text & "' WHERE Codigo = '" & txtCodigo.Text & "'" comandos = New OleDbCommand(actualizar, conexion) comandos.ExecuteNonQuery() MsgBox("Datos Actualizados Correctamente", vbInformation, "Correcto!!!") datagridContacto() limpiar() End Sub Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click Dim eliminar As String Dim si As Byte si = MsgBox("Esta seguro de Eliminar este registro", vbYesNo, "??????") If vbYes Then eliminar = "DELETE FROM Contactos Where Codigo = '" & txtCodigo.Text & "'" comandos = New OleDbCommand(eliminar, conexion) comandos.ExecuteNonQuery() MsgBox("El registro se ha eliminado", vbInformation, "Borrado!!!!!") datagridContacto() limpiar() Else MsgBox("Cancelo la acción", vbInformation, "Cancelo!!!!") limpiar() End If End Sub End Class