Selasa, 11 Juli 2017

Script Form User (CRUD)

SCRIPT BAGIAN FORM USER :

Imports System.Data.SqlClient
Public Class Form1
    Sub kosongkan()
        TextBox1.Clear()
        TextBox2.Clear()
        ComboBox1.Text = ""
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox1.Focus()
    End Sub
    Sub databaru()
        TextBox2.Clear()
        ComboBox1.Text = ""
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox2.Focus()
    End Sub
    'Sub ketemu()
    '    On Error Resume Next
    '    TextBox2.Text = DR.Item("Nm_User")
    '    ComboBox1.Text = DR.Item(2)
    '    TextBox3.Text = DR.Item(3)
    '    TextBox2.Focus()

    'End Sub
    'Sub tampilStatus_User()
    '    CMD = New SqlCommand("select distinct Status_User from tbluser", CONN)
    '    DR = CMD.ExecuteReader
    '    ComboBox1.Items.Clear()
    '    Do While DR.Read
    '        ComboBox1.Items.Add(DR.Item("Status_User"))
    '    Loop
    'End Sub
    Sub tampilGrid()
        Call koneksi()
        DA = New SqlDataAdapter("select * from tbluser", CONN)
        DS = New DataSet
        DA.Fill(DS)
        DGV.DataSource = DS.Tables(0)
        DGV.ReadOnly = True
    End Sub
    'Sub CariKode()
    '    CMD = New SqlCommand("select* from tbluser where Kd_User='" & TextBox1.Text & "'", CONN)
    '    DR = CMD.ExecuteReader
    '    DR.Read()
    'End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Call koneksi()
        Call kosongkan()
        'Call tampilStatus_User()
        Call tampilGrid()
    End Sub

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        TextBox1.MaxLength = 5
        If e.KeyChar = Chr(13) Then
            Call koneksi()
            CMD = New SqlCommand("select * from tbluser where kd_user='" & TextBox1.Text & "'", CONN)
            DR = CMD.ExecuteReader
            DR.Read()
            If DR.HasRows Then
                TextBox2.Text = DR.Item("nm_user")
                TextBox3.Text = DR.Item("password_user")
                ComboBox1.Text = DR.Item("status_user")
                TextBox2.Focus()
            Else
                Call databaru()
            End If
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or ComboBox1.Text = "" Then
            MsgBox("data belum lengkap")
            Exit Sub
        Else

        End If
        Call koneksi()
        CMD = New SqlCommand("select * from tbluser where kd_user='" & TextBox1.Text & "'", CONN)
        DR = CMD.ExecuteReader
        DR.Read()
        If Not DR.HasRows Then
            Call koneksi()
            Dim simpan As String = "insert into tbluser values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & ComboBox1.Text & "')"
            CMD = New SqlCommand(simpan, CONN)
            CMD.ExecuteNonQuery()
        Else
            Call koneksi()
            Dim edit As String = "update tbluser set nama_user='" & TextBox2.Text & "',password_user='" & TextBox3.Text & "',status_user='" & ComboBox1.Text & "' where kd_user='" & TextBox1.Text & "'"
            CMD = New SqlCommand(edit, CONN)
            CMD.ExecuteNonQuery()
        End If
        Call kosongkan()
        Call tampilGrid()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Then
            MsgBox("kode user harus diisi dulu")
            TextBox1.Focus()
            Exit Sub
        Else
            If MessageBox.Show("hapus data ini...?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                'Call Koneksi()
                Dim hapus As String = "delete from tbluser where kode_user='" & TextBox1.Text & "'"
                CMD = New SqlCommand(hapus, CONN)
                CMD.ExecuteNonQuery()
                Call kosongkan()
                Call tampilGrid()
            Else
                Call kosongkan()
            End If
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Call kosongkan()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
        DA = New SqlDataAdapter("select * from tbluser where Nm_User like '&" & TextBox4.Text & "%'", CONN)
        DS = New DataSet
        DA.Fill(DS)
        DGV.DataSource = DS.Tables(0)
        DGV.ReadOnly = True
    End Sub

    Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs)
        On Error Resume Next
        TextBox1.Text = DGV.Rows(e.RowIndex).Cells(0).Value
        TextBox2.Text = DGV.Rows(e.RowIndex).Cells(1).Value
        TextBox3.Text = DGV.Rows(e.RowIndex).Cells(2).Value
        ComboBox1.Text = DGV.Rows(e.RowIndex).Cells(3).Value
    End Sub
End Class


SCRIPT BAGIAN MODULE :

Imports System.Data.SqlClient
Module Module1
    Public CONN As SqlConnection
    Public DA As SqlDataAdapter
    Public DS As DataSet
    Public CMD As SqlCommand
    Public DR As SqlDataReader

    Public Sub Koneksi()
        CONN = New SqlConnection("Data Source=DESKTOP-1H3V5JT; Initial Catalog=dbwayan; Integrated Security=True")
        CONN.Open()
    End Sub
End Module

Tidak ada komentar:

Posting Komentar