Sunteți pe pagina 1din 5

Insertar

Actualizar

Eliminar

Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load conectarse() llenaCombo() End Sub Public Sub llenaCombo() Me.cmbEliminar.Items.Clear() sql = "SELECT IDPERSONA FROM PERSONA" cmd.CommandType = CommandType.Text cmd.Connection = conn cmd.CommandText = sql dr = cmd.ExecuteReader If dr.HasRows = True Then While dr.Read() Me.cmbEliminar.Items.Add(dr.GetValue(0)) End While End If dr.Close() End Sub Private Sub btnRegistro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegistro.Click If (Me.txtIdentificacion.Text = "") Then MsgBox("El campo Identificacion no puede estar vacio", MsgBoxStyle.Critical, "Atencion") Me.txtIdentificacion.Select() Else Dim identificacion As Integer Dim nombres As String = "" Dim apellidos As String = "" Dim correo As String = "" Dim direccion As String = "" identificacion = Me.txtIdentificacion.Text nombres = Me.txtNombres.Text apellidos = Me.txtApellido.Text correo = Me.txtCorreo.Text direccion = Me.txtDireccion.Text cmd.CommandType = CommandType.Text cmd.Connection = conn sql = "INSERT INTO PERSONA (IDPERSONA, NOMBRES, APELLIDOS, CORREO, DIRECCION)" sql += "VALUES (" & identificacion & ",'" & nombres & "','" & apellidos & "', '" & correo & "', " sql += "'" & direccion & "')" cmd.CommandText = sql Try cmd.ExecuteNonQuery() MsgBox("Registro insertado correctamente") Catch ex As Exception If ex.ToString.Contains("valores duplicados") Then MsgBox("El registro ya existe en la base de datos") Else MsgBox(ex.ToString) End If End Try End If End Sub Private Sub btnAcRegistro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcRegistro.Click If (Me.txtIdentificacion1.Text = "") Then MsgBox("El campo Identificacion no puede estar vacio", MsgBoxStyle.Critical, "Atencion") Me.txtIdentificacion1.Select() Else Dim identificacion As Integer Dim nombres As String = "" Dim apellidos As String = "" Dim correo As String = "" Dim direccion As String = "" identificacion = Me.txtIdentificacion1.Text nombres = Me.txtNombres1.Text apellidos = Me.txtApellidos1.Text correo = Me.txtCorreo1.Text direccion = Me.txtDireccion1.Text cmd.CommandType = CommandType.Text cmd.Connection = conn sql = "UPDATE PERSONA SET " sql += "NOMBRES = '" & nombres & "', " sql += "APELLIDOS = '" & apellidos & "', " sql += "CORREO = '" & correo & "', " sql += "DIRECCION = '" & direccion & "' "

sql += "WHERE IDPERSONA = " & identificacion & " " cmd.CommandText = sql Try cmd.ExecuteNonQuery() Me.txtIdentificacion1.Text = "" Me.txtNombres1.Text = "" Me.txtApellidos1.Text = "" Me.txtCorreo1.Text = "" Me.txtDireccion1.Text = "" MsgBox("Registro actualizado correctamente") Catch ex As Exception MsgBox(ex.ToString) End Try End If End Sub Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click Dim identificacion As Integer identificacion = cmbEliminar.SelectedItem.ToString cmd.CommandType = CommandType.Text cmd.Connection = conn sql = "DELETE FROM PERSONA WHERE IDPERSONA = " & identificacion & " " cmd.CommandText = sql Try cmd.ExecuteNonQuery() Me.cmbEliminar.Text = "" Me.txtNombres2.Text = "" Me.txtApellidos2.Text = "" Me.txtCorreo2.Text = "" Me.txtDireccion2.Text = "" llenaCombo() MsgBox("Registro eliminado correctamente") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub txtIdentificacion1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdentificacion1.Leave If Me.txtIdentificacion1.Text <> "" Then consultarPersona(Me.txtIdentificacion1.Text) If dr.Read Then Me.txtNombres1.Text = dr(0).ToString Me.txtApellidos1.Text = dr(1).ToString Me.txtCorreo1.Text = dr(2).ToString Me.txtDireccion1.Text = dr(3).ToString 'cerrar datareader Me.btnAcRegistro.Enabled = True Else Me.txtIdentificacion1.Text = "" Me.txtNombres1.Text = "" Me.txtApellidos1.Text = "" Me.txtCorreo1.Text = "" Me.txtDireccion1.Text = "" MsgBox("No se encontro el registro en la base de datos") Me.btnAcRegistro.Enabled = False End If Else Me.btnAcRegistro.Enabled = False End If dr.Close() End Sub 'Private Sub txtIdentificacion2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdentificacion2.Leave ' If Me.txtIdentificacion2.Text <> "" Then ' consultarPersona(Me.txtIdentificacion2.Text) ' If dr.Read Then ' Me.txtNombres2.Text = dr(0).ToString ' Me.txtApellidos2.Text = dr(1).ToString ' Me.txtCorreo2.Text = dr(2).ToString ' Me.txtDireccion2.Text = dr(3).ToString ' 'cerrar datareader ' Me.btnEliminar.Enabled = True ' Else ' Me.txtIdentificacion2.Text = "" ' Me.txtNombres2.Text = "" ' Me.txtApellidos2.Text = "" ' Me.txtCorreo2.Text = "" ' Me.txtDireccion2.Text = "" ' MsgBox("No se encontro el registro en la base de datos") ' Me.btnEliminar.Enabled = False

' End If ' Else ' Me.btnEliminar.Enabled = False ' End If ' dr.Close() 'End Sub Private Sub cmbEliminar_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbEliminar.SelectedValueChanged ' muestra en el textbox el item del combobox seleccionado Me.txtIdentificacion2.Text = Me.cmbEliminar.SelectedItem.ToString If Me.cmbEliminar.SelectedItem.ToString <> "" Then consultarPersona(Me.cmbEliminar.SelectedItem.ToString) If dr.Read Then Me.txtNombres2.Text = dr(0).ToString Me.txtApellidos2.Text = dr(1).ToString Me.txtCorreo2.Text = dr(2).ToString Me.txtDireccion2.Text = dr(3).ToString 'cerrar datareader Me.btnEliminar.Enabled = True Else 'Me.txtIdentificacion2.Text = "" Me.txtNombres2.Text = "" Me.txtApellidos2.Text = "" Me.txtCorreo2.Text = "" Me.txtDireccion2.Text = "" MsgBox("No se encontro el registro en la base de datos") Me.btnEliminar.Enabled = False End If Else Me.btnEliminar.Enabled = False End If dr.Close() End Sub End Class Module funcionesdb Public conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=cogigoFacilito.accdb;Persist Security Info=False") Public cmd As New OleDb.OleDbCommand Public dr As OleDb.OleDbDataReader Public sql As String Public Sub conectarse() Try conn.Open() MsgBox("***Conexion exitosa***") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Public Sub consultar(ByRef identificacion As String) cmd.Connection = conn cmd.CommandType = CommandType.Text If identificacion <> "" Then cmd.CommandText = "SELECT NOMBRES, APELLIDOS, CORREO, DIRECCION FROM PERSONA WHERE IDPERSONA=" + identificacion Else cmd.CommandText = "SELECT NOMBRES, APELLIDOS, CORREO, DIRECCION FROM PERSONA" End If Try dr = cmd.ExecuteReader() If dr.HasRows Then While dr.Read() MsgBox(dr(0).ToString + " " + dr(1).ToString + " " + dr(2).ToString + " " + dr(3).ToString) End While Else MsgBox("No existen registros para la consulta") End If dr.Close() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Public Sub consultarPersona(ByRef identificacion As String) cmd.Connection = conn cmd.CommandType = CommandType.Text cmd.CommandText = "SELECT NOMBRES, APELLIDOS, CORREO, DIRECCION FROM PERSONA WHERE IDPERSONA=" + identificacion

Try dr = cmd.ExecuteReader Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Module La base de datos se llama cogigofacilito y esta guardada en la ruta: C:\Documents and Settings\jorge\Escritorio\Codigofacilito\Registros\Registros\bin\Debug

S-ar putea să vă placă și