Option Strict Off
Imports System.Data.Odbc
Public Class frmKriteria
    Dim valid As Boolean
    Sub databaru()
        txtNama.Text = ""
        txtNama.Focus()
    End Sub
    Sub kosongkan()
        Call tidaksiapisi()
        txtKode.Text = ""
        txtNama.Text = ""
        txtKode.Enabled = True
        Call Aturnomor()
        txtKode.Focus()
    End Sub
    Sub siapisi()
        txtNama.Enabled = True
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnSimpan.Enabled = True
    End Sub
    Sub tidaksiapisi()
        txtNama.Enabled = False
        btnSimpan.Enabled = False
        btnHapus.Enabled = False
        btnEdit.Enabled = False
    End Sub
    Sub Tampilkan()
        da = New OdbcDataAdapter("Select Kode_Kriteria,Nama_Kriteria from tbkriteria order by kode_kriteria", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbkriteria")
        DGV.DataSource = (ds.Tables("tbkriteria"))
        DGV.ReadOnly = True
        DGV.AllowUserToAddRows = False
        DGV.Columns(0).Width = 70
        DGV.Columns(1).Width = 250
        DGV.GridColor = Color.Blue
        DGV.DefaultCellStyle.ForeColor = Color.Blue
        DGV.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DGV.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).HeaderText = "Kode Kriteria"
        DGV.Columns(1).HeaderText = "Nama Kriteria"


    End Sub
    Sub CekData()
        cmd = New OdbcCommand("select * from tbkriteria where kode_kriteria='" & txtKode.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows = True Then
            txtNama.Text = rd.Item(1)
            Call tidaksiapisi()
            btnEdit.Enabled = True
            btnHapus.Enabled = True
            txtKode.Enabled = False
        Else
            Call databaru()
            Call siapisi()

        End If
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        Call Validasi()
        If valid = True Then
            cmd = New OdbcCommand("Select * from tbkriteria where kode_kriteria='" & txtKode.Text & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If Not rd.HasRows Then
                Dim sqltambah As String = "Insert into tbkriteria (kode_kriteria,nama_kriteria) values " & _
                "('" & txtKode.Text & "','" & txtNama.Text & "')"
                cmd = New OdbcCommand(sqltambah, Conn)

            Else
                Dim sqledit As String = "Update tbkriteria set " & _
                "nama_kriteria='" & txtNama.Text & "' " & _
                " where kode_kriteria='" & txtKode.Text & "'"
                cmd = New OdbcCommand(sqledit, Conn)

            End If
            cmd.ExecuteNonQuery()
            Call Tampilkan()
            Call kosongkan()
        End If


    End Sub

    Private Sub txtKode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtKode.KeyPress
        If e.KeyChar = Chr(13) Then
            Call CekData()
        End If
    End Sub

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        cmd = New OdbcCommand("Select * from tbsub_kriteria where kode_kriteria='" & txtKode.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            MsgBox("Maaf, Kriteria tersebut tidak dapat di Hapus...", , "Perhatian...!")
        Else

            If MessageBox.Show("Yakin akan dihapus..?", "Hapus Data", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                cmd = New OdbcCommand("Delete  from tbkriteria where kode_kriteria='" & txtKode.Text & "'", Conn)
                cmd.ExecuteNonQuery()

            End If
        End If
        Call Tampilkan()
        Call kosongkan()
    End Sub

    Private Sub frmkriteria_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub frmAtribut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Tampilkan()
        Call databaru()
        Call tidaksiapisi()
        Call kosongkan()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        Call siapisi()
        txtNama.Focus()
    End Sub

    Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
        Call kosongkan()
    End Sub

    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        Me.Dispose()
    End Sub

    Private Sub DGV_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellDoubleClick
        txtKode.Text = DGV.Rows(e.RowIndex).Cells(0).Value
        Call CekData()
    End Sub

    Private Sub txtKode_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtKode.Validated
        Call CekData()
    End Sub
    Sub Validasi()
        If txtNama.Text = "" Then
            MsgBox("Nama Kriteria Belum diisi", , "Perhatian...!")
            txtNama.Focus()
            valid = False
            Exit Sub
        Else
            valid = True
        End If
      
    End Sub

    Private Sub txtBobot_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = "," Or e.KeyChar = vbBack) Then e.Handled() = True
    End Sub

    Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub txtNama_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNama.TextChanged

    End Sub
    Sub Aturnomor()
        Dim na As Integer
        Dim cno, cekNomor As String
        cno = ""

        cmd = New OdbcCommand("Select * from tbkriteria  order by kode_kriteria desc", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            na = 1
        Else
            cekNomor = rd.Item(0)
            na = Val(Microsoft.VisualBasic.Right(cekNomor, 1)) + 1
        End If
        cno = Trim(Microsoft.VisualBasic.Str(na))
        txtKode.Text = "C" + cno
    End Sub
End Class