sábado, 15 de marzo de 2014

CODIGO FUENTE: FORMULARIO TRABAJADOR

Imports System.Data.OleDb
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Este código realiza conexión directa a la base de datos creada en Access 2010

Public Class FrmTrabajador
    Dim CN As New OleDbConnection("PROVIDER=MICROSOFT.ACE.OLEDB.12.0;DATA SOURCE=C:\TRABAJADOR.accdb")
    Public VALOR As Integer = 0
    Dim CONSULTA As String
    Dim N As Integer = 0
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub MOSTRAR()
        Dim DA As New OleDbDataAdapter("SELECT * FROM TRABAJADOR", CN)
        Dim DS As New DataSet
        DA.Fill(DS, "TRABAJADOR")
        DataGridView1.DataSource = DS.Tables("TRABAJADOR")

    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub BLOQUEAR()
        BTNBUSCAR.Enabled = True
        BTNNEW.Enabled = True
        BTNGUARDAR.Enabled = False
        BTNMODIFIC.Enabled = True
        BTNELIMIN.Enabled = True
        BTNCANCELAR.Enabled = False
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------     Sub DESBLOQUEAR()
        BTNBUSCAR.Enabled = False
        BTNNEW.Enabled = False
        BTNGUARDAR.Enabled = True
        BTNMODIFIC.Enabled = False
        BTNELIMIN.Enabled = False
        BTNCANCELAR.Enabled = True
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call MOSTRAR()
        Call CARGAR()
        Call BLOQUEAR()
        Call CARGAR_CBO()

    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub CARGAR()
        Dim DA As New OleDbDataAdapter("SELECT * FROM TRABAJADOR", CN)
        Dim DS As New DataSet
        DA.Fill(DS, "TRABAJADOR")
        CBOCOD.DataSource = DS.Tables("TRABAJADOR")
        CBOCOD.DisplayMember = "COD"
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------     Sub CARGAR_CBO()
        CBOCARGO.Items.Add("ADMINISTRADOR")
        CBOCARGO.Items.Add("SUPERVISOR")
        CBOCARGO.Items.Add("CONTADOR")
        CBOCARGO.Items.Add("ASISTENTE")
        CBOCARGO.Items.Add("PROGRAMADOR")

        CBOTURNO.Items.Add("MAÑANA")
        CBOTURNO.Items.Add("TARDE")
        CBOTURNO.Items.Add("NOCHE")
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------     Sub LIMPIAR()
        TXTAPE.Clear()
        TXTDNI.Clear()
        TXTNOM.Clear()
        TXTSUELDO.Clear()
        TXTTELF.Clear()
        CBOCARGO.Text = Nothing
        CBOCOD.Text = Nothing
        CBOTURNO.Text = Nothing
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub BUSCAR_COD()
        Dim CMD As New OleDb.OleDbCommand("SELECT * FROM TRABAJADOR WHERE COD ='" & CBOCOD.Text & "'", CN)
        Dim DR As OleDb.OleDbDataReader
        CN.Open()
        DR = CMD.ExecuteReader
        If DR.Read Then
            TXTDNI.Text = DR(1)
            TXTNOM.Text = DR(2)
            TXTAPE.Text = DR(3)
            CBOCARGO.Text = DR(4)
            CBOTURNO.Text = DR(5)
            TXTSUELDO.Text = DR(6)
            TXTTELF.Text = DR(7)
        Else
            MsgBox("Error")
        End If
        CN.Close()

    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------     Sub NUEVO()
        Dim CMD As New OleDb.OleDbCommand("INSERT INTO TRABAJADOR VALUES ('" & CBOCOD.Text & "','" & TXTDNI.Text & "','" & TXTNOM.Text & "','" & TXTAPE.Text & "','" & CBOCARGO.Text & "','" & CBOTURNO.Text & "','" & TXTSUELDO.Text & "','" & TXTTELF.Text & "')", CN)
        CN.Open()
        CMD.ExecuteNonQuery()
        CN.Close()
        MOSTRAR()
        MsgBox("REGISTRO AGREGADO")
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub MODIFICAR()
        Dim CMD As New OleDb.OleDbCommand("UPDATE TRABAJADOR SET COD='" & CBOCOD.Text & "' , DNI='" & TXTDNI.Text & "', NOM = '" & TXTNOM.Text & "',APE='" & TXTAPE.Text & "', CARGO ='" & CBOCARGO.Text & "', TURNO = '" & CBOTURNO.Text & "', SUELDO='" & TXTSUELDO.Text & "', TELF = '" & TXTTELF.Text & "' WHERE COD='" & CBOCOD.Text & "'", CN)
        CN.Open()
        CMD.ExecuteNonQuery()
        CN.Close()
        MOSTRAR()
        MsgBox("REGISTRO ACTUALIZADO")
    End Sub
------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Buscar

Private Sub BTNBUSCAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNBUSCAR.Click
        BUSCAR_COD()
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Nuevo

Private Sub BTNNEW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNNEW.Click
        DESBLOQUEAR()
        LIMPIAR()
        N = 1
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Modificar

Private Sub BTNMODIFIC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNMODIFIC.Click
        DESBLOQUEAR()
        N = 2
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Guardar

Private Sub BTNGUARDAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNGUARDAR.Click
        If N = 1 Then
            NUEVO()
        ElseIf N = 2 Then
            MODIFICAR()
        End If
        BLOQUEAR()
        LIMPIAR()
        CARGAR()
        N = 0
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Cancelar

Private Sub BTNCANCELAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNCANCELAR.Click
        BLOQUEAR()
        LIMPIAR()
        N = 0
    End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Código para el Botón Eliminar

Private Sub BTNELIMIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNELIMIN.Click

        Dim CMD As New OleDb.OleDbCommand("DELETE FROM TRABAJADOR WHERE COD='" & CBOCOD.Text & "'", CN)
        CN.Open()
        CMD.ExecuteNonQuery()
        CN.Close()
        LIMPIAR()
        CARGAR()
        MOSTRAR()
        MsgBox("EL REGISTRO SE HA ELIMINADO")
    End Sub
End Class

No hay comentarios: