quinta-feira, 11 de outubro de 2012

on


Imports System.Data.OleDb

Public Class frmEmpregados
    Inherits System.Windows.Forms.Form
    Dim intAccion As Integer

    Public Function newr() As String
        Try
            Dim dbCommand As OleDbCommand
            Dim DBConn As OleDbConnection
            Dim strFN, strLN, strAdd, strCty, strSt As String
            Dim strDept, strPath As String
            Dim DBInsert As New OleDbCommand()

            strFN = txtPrimeiroNome.Text
            strLN = txtSegundoNome.Text
            strAdd = txtEnderecoCompleto.Text
            strCty = txtCidade.Text
            strSt = CmbEstado.Text
            strDept = txtDepartamento.Text

            strPath = txtPath.Text
            DBConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=" & strPath)

            DBInsert.CommandText = "Insert Into Empregados " _
            & "(PrimeiroNome,SegundoNome,Endereco,Cidade,Estado,Departamento) " _
            & "Values (" _
            & "'" & strFN & "', " _
            & "'" & strLN & "', " _
            & "'" & strAdd & "', " _
            & "'" & strCty & "', " _
            & "'" & strSt & "', " _
            & "'" & strDept & "')"
            DBInsert.Connection = DBConn
            DBInsert.Connection.Open()
            DBInsert.ExecuteNonQuery()
            DBConn.Close()

            txtError.Text = "Inserido com sucesso."

        Catch err As System.Exception
            txtError.Text = err.Message
        End Try
    End Function

    Public Function find() As String
        Try
            Dim dbConn As OleDbConnection
            Dim dbCommand As New OleDbCommand()
            Dim strPath As String

            strPath = txtPath.Text
            dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=" & strPath)

            dbCommand.CommandText = "SELECT * FROM Empregados WHERE IdEmp=" & CmbID.Text
            dbCommand.Connection = dbConn
            dbConn.Open()
            Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader

            While dbDR.Read
                txtPrimeiroNome.Text = dbDR("PrimeiroNome".ToString)
                txtSegundoNome.Text = dbDR("SegundoNome".ToString)
                txtEnderecoCompleto.Text = dbDR("Endereco".ToString)
                txtCidade.Text = dbDR("Cidade".ToString)
                CmbEstado.Text = dbDR("Estado".ToString)
                txtDepartamento.Text = dbDR("Departamento".ToString)
            End While

            dbConn.Close()

            btnEdit.Enabled = True
            btnDelete.Enabled = True

        Catch err As System.Exception
            txtError.Text = err.Message
        End Try
    End Function

    Public Function loadCBO() As String
        Try
            Dim dbConn As OleDbConnection
            Dim dbCommand As New OleDbCommand()
            Dim strPath As String

            strPath = txtPath.Text

            dbConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=" & strPath)

            dbCommand.CommandText = "SELECT idEmp FROM Empregados"
            dbCommand.Connection = dbConn
            dbConn.Open()
            Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
            CmbID.Items.Clear()
            While dbDR.Read
                CmbID.Items.Add(dbDR("idEmp"))
            End While

            dbConn.Close()

        Catch err As System.Exception
            txtError.Text = err.Message
        End Try
    End Function

    Public Function edit() As String
        Try
            Dim DBConn As OleDbConnection
            Dim dbCommand As New OleDbCommand()
            Dim strFN, strLN, strAdd, strCty, strSt As String
            Dim strDept, strPath As String

            strFN = txtPrimeiroNome.Text
            strLN = txtSegundoNome.Text
            strAdd = txtEnderecoCompleto.Text
            strCty = txtCidade.Text
            strSt = CmbEstado.Text
            strDept = txtDepartamento.Text
            strPath = txtPath.Text

            DBConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=" & strPath)

            dbCommand.CommandText = "Update Empregados Set " _
            & "PrimeiroNome='" & strFN & "', " _
            & "SegundoNome='" & strLN & "', " _
            & "Endereco='" & strAdd & "', " _
            & "Cidade='" & strCty & "', " _
            & "Estado='" & strSt & "', " _
            & "Departamento='" & strDept & "' " _
            & "WHERE idEmp=" & CmbID.Text

            dbCommand.Connection = DBConn
            dbCommand.Connection.Open()
            dbCommand.ExecuteNonQuery()
            DBConn.Close()
            txtError.Text = "Alterado com sucesso."
        Catch err As System.Exception
            txtError.Text = err.Message
        End Try
    End Function

    Public Function delete() As String
        Try
            Dim DBConn As OleDbConnection
            Dim dbCommand As New OleDbCommand()
            Dim strPath As String

            strPath = txtPath.Text

            DBConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=" & strPath)

            dbCommand.CommandText = "Delete From Empregados " _
                                    & "WHERE idEmp=" _
                                    & CmbID.Text

            dbCommand.Connection = DBConn
            dbCommand.Connection.Open()
            dbCommand.ExecuteNonQuery()
            DBConn.Close()
            txtError.Text = "Excluído com sucesso.."
        Catch err As System.Exception
            txtError.Text = err.Message
        End Try
    End Function

    Private Sub EnabledField()
        txtPrimeiroNome.ReadOnly = False
        txtSegundoNome.ReadOnly = False
        txtEnderecoCompleto.ReadOnly = False
        txtCidade.ReadOnly = False
        'CmbEstado.ReadOnly = False
        txtDepartamento.ReadOnly = False
    End Sub

    Private Sub DisabledField()
        txtPrimeiroNome.ReadOnly = True
        txtSegundoNome.ReadOnly = True
        txtEnderecoCompleto.ReadOnly = True
        txtCidade.ReadOnly = True
        'CmbEstado.ReadOnly = True
        txtDepartamento.ReadOnly = True
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        intAccion = 1
        txtPrimeiroNome.Text = ""
        txtSegundoNome.Text = ""
        txtEnderecoCompleto.Text = ""
        txtCidade.Text = ""
        CmbEstado.Text = ""
        txtDepartamento.Text = ""
        btnNew.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        txtPrimeiroNome.Focus()
        CmbID.Items.Clear()
        CmbID.Enabled = False
        EnabledField()
        txtError.Text = ""
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If MsgBox("Salvar as alterações?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "") = MsgBoxResult.No Then
            Exit Sub
        End If

        If intAccion = 1 Then
            newr()
        ElseIf intAccion = 2 Then
            edit()
        End If

        btnSave.Enabled = False
        btnCancel.Enabled = False
        btnNew.Enabled = True
        txtPrimeiroNome.Text = ""
        txtSegundoNome.Text = ""
        txtEnderecoCompleto.Text = ""
        txtCidade.Text = ""
        CmbEstado.SelectedIndex = -1
        txtDepartamento.Text = ""
        CmbID.Enabled = True
        loadCBO()
        DisabledField()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        btnSave.Enabled = False
        btnCancel.Enabled = False
        btnNew.Enabled = True
        txtPrimeiroNome.Text = ""
        txtSegundoNome.Text = ""
        txtEnderecoCompleto.Text = ""
        txtCidade.Text = ""
        CmbEstado.SelectedIndex = -1
        txtDepartamento.Text = ""
        CmbID.Enabled = True
        loadCBO()
        DisabledField()
    End Sub

    Private Sub FEmp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadCBO()
    End Sub

    Private Sub cboId_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbID.SelectedIndexChanged
        find()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        intAccion = 2
        btnNew.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        CmbID.Enabled = False
        EnabledField()
        txtError.Text = ""
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If MsgBox("Tem certeza que deseja excluir o registro " & Me.txtPrimeiroNome.Text & "?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "") = MsgBoxResult.No Then
            CmbID.Focus()
            Exit Sub
        End If

        delete()
        btnSave.Enabled = False
        btnCancel.Enabled = False
        btnNew.Enabled = True
        txtPrimeiroNome.Text = ""
        txtSegundoNome.Text = ""
        txtEnderecoCompleto.Text = ""
        txtCidade.Text = ""
        CmbEstado.SelectedIndex = -1
        txtDepartamento.Text = ""
        CmbID.Enabled = True
        loadCBO()
        DisabledField()
    End Sub
End Class

Pegue o projeto neste link: http://www.mediafire.com/?wpowpxvvp9hnmqd
Espero ter ajudado... Até mais, abraços....

0 comentários:

Postar um comentário