Jumat, 21 Juni 2013

INPUT DATA KE ACCESS MELALUI VB.NET



Imports System.Data.OleDb
Public Class Dt_pegawai
    Sub kosongkan()
        TNip.Text = ""
        TNama.Text = ""
        TJabatan.Text = ""
        Tgllahir.Text = ""
        Alamat.Text = ""
        Telepon.Text = ""
        Umur.Text = ""
        Kjabatan.Text = ""
        JK.Text = ""
        Spegawai.Text = ""
        BtRadio.Text = ""
        RadioButton1.Checked = Enabled
        RadioButton2.Checked = Enabled
        Pendidikan.Text = ""
        Cabang.Text = ""
        Ktunjangan.Text = ""
        TNip.Focus()
    End Sub
    Sub DataBaru()
        TNip.Text = ""
        TNama.Text = ""
        TJabatan.Text = ""
        Tgllahir.Text = ""
        Alamat.Text = ""
        Telepon.Text = ""
        Umur.Text = ""
        Kjabatan.Text = ""
        JK.Text = ""
        Spegawai.Text = ""
        BtRadio.Text = ""
        Pendidikan.Text = ""
        Cabang.Text = ""
        Ktunjangan.Text = ""
        TNip.Focus()
    End Sub
    Sub Tampilkan()
        da = New OleDbDataAdapter("select*from Datapegawai", conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "Datapegawai")
        DGV.DataSource = (ds.Tables("Datapegawai"))
    End Sub
    Private Sub Pegawai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Me.ControlBox = False
        DT.Value = Format(DT.Value, "dd/MM/yy")
        DT2.Value = Format(DT.Value, "dd/MM/yy")
        Call koneksi()
        Call Tampilkan()
    End Sub
    Private Sub TNip_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar = Chr(13) Then
            Try
                cmd = New OleDbCommand("select*from Datapegawai where Nip='" & TNip.Text & "'", conn)
                rd = cmd.ExecuteReader
                rd.Read()
                If rd.HasRows = True Then
                    TNama.Text = rd.GetString(1)
                    TJabatan.Text = rd.GetString(2)
                    Alamat.Text = rd.GetString(4)
                    Telepon.Text = rd.GetString(5)
                    Umur.Text = rd.GetString(6)
                    Kjabatan.Text = rd.GetString(7)
                    Ktunjangan.Text = rd.GetString(8)
                    JK.Text = rd.GetString(9)
                    Spegawai.Text = rd.GetString(10)
                    BtRadio.Text = rd.GetString(11)
                    DT2.Value = rd.GetString(12)
                Else
                    Call DataBaru()
                    TNama.Focus()
                End If
            Catch ex As Exception
            End Try
        End If

    End Sub
    Private Sub TNama_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TNama.KeyPress
        If Not ((Char.IsLetter(e.KeyChar)) Or (e.KeyChar = ControlChars.Back) Or (Char.IsSeparator(e.KeyChar))) Then
            e.Handled = True

        End If
        If e.KeyChar = Chr(13) Then TNip.Focus()
    End Sub
    Private Sub TNip_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not ((Char.IsNumber(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then Alamat.Focus()
    End Sub
    Private Sub Alamat_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Alamat.KeyPress
        If Not ((Char.IsLetterOrDigit(e.KeyChar)) Or (Char.IsSeparator(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then Tgllahir.Focus()
    End Sub
    Private Sub TJabatan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not ((Char.IsLetter(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then DT2.Focus()
    End Sub
    Private Sub Tgllahir_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tgllahir.KeyPress
        If e.KeyChar = Chr(13) Then Umur.Focus()
    End Sub
    Private Sub Umur_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Umur.KeyPress
        If Not ((Char.IsNumber(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then JK.Focus()
    End Sub
    Private Sub JK_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles JK.KeyPress
        If e.KeyChar = Chr(13) Then Telepon.Focus()
    End Sub
    Private Sub Telepon_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not ((Char.IsNumber(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then Spegawai.Focus()
    End Sub
    Private Sub Dep_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Kjabatan.KeyPress
        If Not ((Char.IsLetterOrDigit(e.KeyChar)) Or (Char.IsSeparator(e.KeyChar)) Or (e.KeyChar = ControlChars.Back)) Then
            e.Handled = True
        End If
        If e.KeyChar = Chr(13) Then TNama.Focus()
    End Sub
    Private Sub diterima_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar = Chr(13) Then Kjabatan.Focus()
    End Sub
    Private Sub DT_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DT.ValueChanged
        Dim tglVar As String
        tglVar = Format(DT.Value, "dd-MM-yyyy")
    End Sub
    Private Sub Dep_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Spegawai.SelectedIndexChanged
        Dim BRadio As String
        If RadioButton1.Checked = True Then
            BRadio = "Aktif"
        Else
            RadioButton2.Checked = True
            BRadio = "NonAktif"
        End If
        BtRadio.Text = BRadio
    End Sub
    Private Sub TNama_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TNama.TextChanged
        Dim i As Integer = TNama.SelectionStart
        TNama.Text = StrConv(TNama.Text, VbStrConv.ProperCase)
        TNama.SelectionStart = i
    End Sub
    Private Sub Alamat_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Alamat.TextChanged
        Dim i As Integer = Alamat.SelectionStart
        Alamat.Text = StrConv(Alamat.Text, VbStrConv.ProperCase)
        Alamat.SelectionStart = i
    End Sub

    Private Sub CmbSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbSimpan.Click
        If TNip.Text = "" Or TNama.Text = "" Or TJabatan.Text = "" Or Tgllahir.Text = "" Or Alamat.Text = "" Or Telepon.Text = "" Or Umur.Text = "" Or Kjabatan.Text = "" Or JK.Text = "" Then
            MsgBox("data belum lengkap")
            Exit Sub
        Else
            cmd = New OleDbCommand("select*from Datapegawai where Nip='" & TNip.Text & "'", conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If Not rd.HasRows Then
                Dim sqltambah As String = "insert into Datapegawai(Nip, Nama, Jabatan, Tanggal_lahir, Alamat, Telepon, Umur, Kode_jabatan, Kode_tunjangan, Jenis_kelamin, Status_pegawai, Status_mengajar, Tanggal_aktif, Pendidikan_terakhir, Bimbel_cabang)values " & _
                "('" & TNip.Text & "','" & TNama.Text & "','" & TJabatan.Text & "', '" & DT.Value & "', '" & Alamat.Text & "','" & Telepon.Text & "','" & Umur.Text & "','" & Kjabatan.Text & "','" & Ktunjangan.Text & "','" & JK.Text & "', '" & Spegawai.Text & "','" & BtRadio.Text & "','" & DT2.Value & "', '" & Pendidikan.Text & "', '" & Cabang.Text & "')"
                cmd = New OleDbCommand(sqltambah, conn)
                cmd.ExecuteNonQuery()
                Call kosongkan()
                Call Tampilkan()
            Else
                MsgBox("Data dengan NIP tersebut sudah ada")
                Call kosongkan()
                Call Tampilkan()
            End If
        End If
    End Sub
    Private Sub CmdBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdBatal.Click
        Call kosongkan()
    End Sub

    Private Sub CmbHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbHapus.Click
        If TNip.Text = "" Then
            MsgBox("isi Nip untuk menghapus data pegawai")
            TNip.Focus()
            Exit Sub
        Else
            If MessageBox.Show("yakin akan dihapus", "",
                              MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                cmd = New OleDbCommand("delete*from Datapegawai where Nip='" & TNip.Text & "'", conn)
                cmd.ExecuteNonQuery()
                Call kosongkan()
                Call Tampilkan()
            Else
                Call kosongkan()
            End If
        End If
    End Sub

    Private Sub CmbTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbTutup.Click
        Me.Close()
    End Sub
End Class

1 komentar: