Rabu, 08 Juni 2011

Pemrograman Visual Basic ( VB.Net)

Aplikasi Pemesanan Barang ( Tugas UAS )




   
Public Class Form1
     Sub kosong()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
    End Sub
    Sub netral()
        Button1.Text = "INPUT"
        Button2.Text = "CARI"
        Button3.Text = "EDIT"
        Button4.Text = "HAPUS"
        Button5.Text = "EXIT"
        Button1.Enabled = True
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        TextBox1.Enabled = True
    End Sub
    Sub cekrecord()
        Call Koneksi()
        Tampil.Connection = Database
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from costumer where Kd_costumer='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil.ExecuteReader

        If Tampilkan.HasRows = True Then
            hasilcek = True
        Else
            hasilcek = False
        End If

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Simpan()
    End Sub
    Sub Simpan()
        If Button1.Text = "INPUT" Then
            TextBox1.Text = ""
            Button1.Text = "Simpan"
            Button2.Enabled = False
            Button3.Enabled = False
            Button4.Enabled = False
            Button5.Text = "Batal"
            'TextBox1.Enabled = False
            TextBox1.Focus()
        Else
            If TextBox1.Text = "" Then Exit Sub
            If TextBox2.Text = "" Then Exit Sub
            If TextBox3.Text = "" Then Exit Sub
            If TextBox4.Text = "" Then Exit Sub
            If TextBox5.Text = "" Then Exit Sub
            If TextBox6.Text = "" Then Exit Sub
            If TextBox7.Text = "" Then Exit Sub
            Call cekrecord()
            Try
                If hasilcek = "False" Then
                    Call Koneksi()

                    Tampil.Connection = Database
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Insert into costumer (Kd_costumer,Nama_costumer,Tlp,Alamat,Kota,Kode_Pos,Fax)values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "')"
                    Tampil.ExecuteNonQuery()
                    Call kosong()
                Else
                    MsgBox("Data sudah ada, silah cek data yang di input", MsgBoxStyle.Critical, "Input data")
                    TextBox1.Focus()
                End If
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Call Delete()
    End Sub
    Sub Delete()
        If Button4.Text = "DELETE" Then
            TextBox1.Text = ""
            Button4.Text = "Ok"
            Button1.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
            Button5.Text = "Batal"
            TextBox1.Focus()

        Else
            If TextBox1.Text = "" Then Exit Sub
            If TextBox2.Text = "" Then Exit Sub
            If TextBox1.Text = "" Then Exit Sub
            If TextBox2.Text = "" Then Exit Sub
            If TextBox3.Text = "" Then Exit Sub
            If TextBox4.Text = "" Then Exit Sub
            If TextBox5.Text = "" Then Exit Sub
            If TextBox6.Text = "" Then Exit Sub
            If TextBox7.Text = "" Then Exit Sub
            Try
                Call Koneksi()

                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text

                Tampil.CommandText = "Delete from costumer where Kd_costumer='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()

                Call kosong()
                TextBox1.Focus()

            Catch ex As Exception
                MsgBox(ex.ToString())

            End Try
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            If Button1.Text = "Simpan" Then
                Call cekrecord()
                If hasilcek = False Then
                    TextBox2.Focus()
                Else
                    MsgBox("Kode sudah ada, silahkan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Infromasi")
                    TextBox1.Focus()
                End If
            Else
                Call Koneksi()
                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "select * from costumer where Kd_costumer='" & Trim(TextBox1.Text) & "'"
                Tampilkan = Tampil.ExecuteReader

                If Tampilkan.HasRows = True Then
                    While Tampilkan.Read()
                        If IsDBNull(Tampilkan("Kd_costumer")) Then
                            TextBox1.Focus()
                        Else
                            TextBox2.Text = Tampilkan("Nama_costumer")
                            TextBox3.Text = Tampilkan("Tlp")
                            TextBox4.Text = Tampilkan("Alamat")
                            TextBox5.Text = Tampilkan("Kota")
                            TextBox6.Text = Tampilkan("Kode_Pos")
                            TextBox7.Text = Tampilkan("Fax")
                        End If
                    End While
                Else
                    MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah Data")
                End If
            End If
        End If
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call Edit()
    End Sub
    Sub Edit()
        If Button3.Text = "EDIT" Then
            TextBox1.Text = ""
            Button3.Text = "Simpan"
            Button1.Enabled = False
            Button2.Enabled = False
            Button4.Enabled = False
            Button5.Text = "Batal"
            TextBox1.Focus()

        Else
            If TextBox1.Text = "" Then Exit Sub
            If TextBox2.Text = "" Then Exit Sub
            Try
                Call Koneksi()

                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update costumer set Nama_costumer ='" & Trim(TextBox2.Text) & "',Tlp ='" & Trim(TextBox3.Text) & "',Alamat ='" & Trim(TextBox4.Text) & "',Kota ='" & Trim(TextBox5.Text) & "',Kode_Pos ='" & Trim(TextBox6.Text) & "',Fax ='" & Trim(TextBox7.Text) & "' where Kd_costumer ='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()


                Call kosong()
                TextBox1.Focus()

            Catch ex As Exception
                MsgBox(ex.ToString())

            End Try
        End If
    End Sub

        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If Button5.Text = "EXIT" Then
            Me.Close()
        Else
            Call kosong()
            Call netral()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


Module

Imports System.Data
Imports System.Data.Sql
Module koneksidatabase
    Public Database As New OleDb.OleDbConnection
    Public Tampil As New OleDb.OleDbCommand
    Public Tampil2 As New OleDb.OleDbCommand
    Public Tampilkan As OleDb.OleDbDataReader
    Public hasiljur, jur, hasilcek As String

    Public Sub Koneksi()
        Try
            Database.Close()
            'server'
            'Database.ConnectionString = "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;"User ID=sasa;Initial Catalog=PTJayaKarta;Data Source=192.168.10.4

            'lokal
            Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog= lia;Data Source=."
            Database.Open()
            'Tampil.Connection = Database
            MsgBox(" Koneksi Berhasil ")
        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try
     End Sub
 End Module