Option Strict Off
Imports System.Data.Odbc
Public Class frmSubKriteria
    Dim ckode_kriteria As String
    Dim valid As Boolean
    Sub databaru()
        txtsubKriteria.Text = ""
        txtsubKriteria.Focus()
    End Sub
    Sub kosongkan()
        Call tidaksiapisi()
        txtKode.Text = ""
        txtsubKriteria.Text = ""
        cboKriteria.Enabled = True
        txtKode.Enabled = True
        txtKode.Focus()
        Call Aturnomor()
    End Sub
    Sub siapisi()
        txtsubKriteria.Enabled = True
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnSimpan.Enabled = True
    End Sub
    Sub tidaksiapisi()
        txtsubKriteria.Enabled = False
        btnSimpan.Enabled = False
        btnHapus.Enabled = False
        btnEdit.Enabled = False
    End Sub
    Sub Tampilkan()
        da = New OdbcDataAdapter("Select kode_sub,sub_kriteria " & _
                                 "from tbsub_kriteria where kode_kriteria = '" & ckode_kriteria & "' order by kode_sub", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbsub_kriteria")
        DGV.DataSource = (ds.Tables("tbsub_kriteria"))
        DGV.ReadOnly = True
        DGV.AllowUserToAddRows = False
        DGV.Columns(0).Width = 100
        DGV.Columns(1).Width = 330


        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(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).HeaderText = "Kode Sub"
        DGV.Columns(1).HeaderText = "Sub Kriteria"

    End Sub
    Sub TampilAtribut()
        cboKriteria.Items.Clear()
        cmd = New OdbcCommand("select distinct nama_kriteria,kode_kriteria from tbkriteria " & _
                              " order by kode_kriteria", Conn)
        rd = cmd.ExecuteReader
        While rd.Read
            cboKriteria.Items.Add(rd.Item(0))
        End While
    End Sub
    Sub CekData()
        cmd = New OdbcCommand("select * from tbsub_kriteria where kode_sub='" & txtKode.Text & "' " & _
                              "and kode_kriteria = '" & ckode_kriteria & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows = True Then
            txtsubKriteria.Text = rd.Item(2)
            Call tidaksiapisi()
            btnEdit.Enabled = True
            btnHapus.Enabled = True
            txtKode.Enabled = False
            cboKriteria.Enabled = False
        Else
            Call databaru()
            Call siapisi()


        End If
    End Sub

    Private Sub cboTingkat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboKriteria.SelectedIndexChanged
        cmd = New OdbcCommand("Select * from tbkriteria where nama_kriteria='" & cboKriteria.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            ckode_kriteria = rd.Item(0)
        End If
        Call Tampilkan()
        Call Aturnomor()
    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 tbsub_kriteria where kode_sub='" & txtKode.Text & "' and kode_kriteria = '" & ckode_kriteria & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If Not rd.HasRows Then
                Dim sqltambah As String = "Insert into tbsub_kriteria (kode_kriteria,kode_sub,sub_kriteria) values " & _
                "('" & ckode_kriteria & "','" & txtKode.Text & "','" & Trim(txtsubKriteria.Text) & "')"
                cmd = New OdbcCommand(sqltambah, Conn)

            Else
                Dim sqledit As String = "Update tbsub_kriteria set " & _
                "sub_kriteria='" & Trim(txtsubKriteria.Text) & "' " & _
                " where kode_sub='" & txtKode.Text & "' and kode_kriteria = '" & ckode_kriteria & "'"
                cmd = New OdbcCommand(sqledit, Conn)

            End If
            cmd.ExecuteNonQuery()
            Call Tampilkan()
            Call kosongkan()
        End If
    End Sub
    Sub Validasi()
        If txtsubKriteria.Text = "" Then
            MsgBox("Sub Kriteria Belum diisi", , "Perhatian...!")
            txtsubKriteria.Focus()
            valid = False
            Exit Sub
        Else
            valid = True
        End If

    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()
        txtsubKriteria.Focus()
    End Sub

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        cmd = New OdbcCommand("Select * from tbprioritas where kode_sub='" & txtKode.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            MsgBox("Maaf, Sub 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 tbsub_kriteria where kode_sub='" & txtKode.Text & "' and kode_kriteria = '" & ckode_kriteria & "'", Conn)
                cmd.ExecuteNonQuery()

            End If
        End If
        Call Tampilkan()
        Call kosongkan()
    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 frmtbsub_kriteria_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 frmtbsub_kriteria_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Tampilkan()
        Call databaru()
        Call tidaksiapisi()
        Call kosongkan()
        Call TampilAtribut()
    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 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
    Sub Aturnomor()
        Dim na As Integer
        Dim cno, cekNomor As String
        cno = ""
        If cboKriteria.Text = "" Then Exit Sub
        cmd = New OdbcCommand("Select * from tbsub_kriteria where  kode_kriteria = '" & ckode_kriteria & "' order by kode_sub 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 = Trim(ckode_kriteria) + cno
    End Sub

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

    End Sub
End Class