Option Strict Off
Imports System.Data.Odbc
Public Class frmImportir
    Dim valid As Boolean
    Sub databaru()
        txtNama.Text = ""
        txtAsal.Text = ""
        txtNama.Focus()
    End Sub
    Sub kosongkan()
        Call tidaksiapisi()
        txtNo_Importir.Text = ""
        txtNama.Text = ""
        txtAsal.Text = ""
        txtNo_Importir.Enabled = True
        txtNo_Importir.Focus()
        Call Aturnomor()
    End Sub
    Sub siapisi()
        txtNama.Enabled = True
        txtAsal.Enabled = True
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnSimpan.Enabled = True
    End Sub
    Sub tidaksiapisi()
        txtNama.Enabled = False
        txtAsal.Enabled = False
        btnSimpan.Enabled = False
        btnHapus.Enabled = False
        btnEdit.Enabled = False
    End Sub
    Sub TampilGrid()
        da = New OdbcDataAdapter("Select no_importir,nama_importir,Asal_Negara from tbimportir  order by no_importir", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbimportir")
        DGV.DataSource = (ds.Tables("tbimportir"))
        DGV.ReadOnly = True
        DGV.AllowUserToAddRows = False
        DGV.Columns(0).Width = 90
        DGV.Columns(1).Width = 170
        DGV.Columns(2).Width = 160


        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 = "No. Importir"
        DGV.Columns(1).HeaderText = "Nama Importir"
        DGV.Columns(2).HeaderText = "Asal Negara"

    End Sub

    Sub CekData()
        cmd = New OdbcCommand("select * from tbimportir where no_importir='" & txtNo_Importir.Text & "' ", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows = True Then
            txtNama.Text = rd.Item("nama_importir")
            txtAsal.Text = rd.Item("Asal_Negara")
            Call tidaksiapisi()
            btnEdit.Enabled = True
            btnHapus.Enabled = True
            txtNo_Importir.Enabled = False

        Else
            Call databaru()
            Call siapisi()

        End If
    End Sub
    Sub Validasi()
        If txtNo_Importir.Text = "" Then
            MsgBox("NIK Belum diisi", , "Perhatian...!")
            txtNo_Importir.Focus()
            valid = False
            Exit Sub
        Else
            valid = True
        End If
        If txtNama.Text = "" Then
            MsgBox("Nama Importir Belum diisi", , "Perhatian...!")
            txtNama.Focus()
            valid = False
            Exit Sub
        Else
            valid = True
        End If
        If txtAsal.Text = "" Then
            MsgBox("Asal Negara Belum diisi", , "Perhatian...!")
            txtAsal.Focus()
            valid = False
            Exit Sub
        Else
            valid = True
        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 tbimportir where no_importir='" & txtNo_Importir.Text & "' ", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If Not rd.HasRows Then
                Dim sqltambah As String = "Insert into tbimportir (no_importir,nama_importir,Asal_Negara) values " & _
                "('" & txtNo_Importir.Text & "','" & txtNama.Text & "','" & txtAsal.Text & "')"
                cmd = New OdbcCommand(sqltambah, Conn)

            Else
                Dim sqledit As String = "Update tbimportir set " & _
                "nama_importir='" & txtNama.Text & "', " & _
                "Asal_Negara='" & txtAsal.Text & "' " & _
                " where no_importir='" & txtNo_Importir.Text & "'  "
                cmd = New OdbcCommand(sqledit, Conn)

            End If
            cmd.ExecuteNonQuery()
            Call TampilGrid()
            Call kosongkan()
        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()
        txtNama.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 tbnilaialternatif where no_importir='" & txtNo_Importir.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            MsgBox("Maaf, Data Importir 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 tbimportir where no_importir='" & txtNo_Importir.Text & "'", Conn)
                cmd.ExecuteNonQuery()

            End If
        End If
        Call TampilGrid()
        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 DGV_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellDoubleClick

        txtNo_Importir.Text = DGV.SelectedRows.Item(0).Cells(0).Value()
        Call CekData()
    End Sub

    Private Sub frmtbPemohon_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 frmtbPemohon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call TampilGrid()
        Call databaru()
        Call tidaksiapisi()
        Call kosongkan()
    End Sub

    Private Sub txtId_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNo_Importir.KeyPress
        If e.KeyChar = Chr(13) Then
            Call CekData()
        End If
    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 nudPeriode_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        On Error Resume Next
        Call TampilGrid()
    End Sub

    Sub Aturnomor()
        Dim na As Integer
        Dim cno, cekNomor, nTahun As String
        cno = ""
        ntahun = Year(Now)
        cmd = New OdbcCommand("Select * from tbimportir  order by no_importir desc", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            cno = "000"
            na = 1
        Else

            cekNomor = rd.Item(0)
            na = Val(Microsoft.VisualBasic.Right(cekNomor, 4)) + 1
            If na >= 1 And na < 10 Then cno = "000"
            If na >= 10 And na < 100 Then cno = "00"
            If na >= 100 And na < 1000 Then cno = "0"
        End If
        ''  cno = Trim(Microsoft.VisualBasic.Str(na))
        txtNo_Importir.Text = ntahun + cno + Trim(Microsoft.VisualBasic.Str(na))
    End Sub
End Class