• Tidak ada hasil yang ditemukan

Membuat Form Pelanggan

MICROSOFT.NET FRAMEWORK

LATIHAN 6.1 Database Mahasiswa

7.5 Membuat Form Pelanggan

Gambar 7.3 Hasil Form Barang

Label2 Text Alamat

Label3 Text Telepon

Label4 Text Email

Label5 Text Jenis Kelamin

Botton1 Text &Simpan

Botton2 Text &Hapus

Botton3 Text &Batal

Botton4 Text &Tutup

DataGridView1 Name DGV

3. Masukkan kode berikut diatas Public Class Pelanggan bertujuan untuk memanggil driver dengan koneksi oleDB

Imports System.Data.OleDb

4. Dengan membuat prosedur kosongkan yang berfungsi untuk menghilangkan nilai pada textbox, Masukkan kode berikut ini :

Sub Kosongkan()

TextBox1.Text = ""

TextBox2.Text = ""

TextBox3.Text = ""

TextBox4.Text = ""

TextBox5.Text = ""

ComboBox1.Text = ""

TextBox1.Focus() End Sub

5. Tambahkan kembali prosedur DataBaru dengan kode sebagai berikut : Sub DataBaru()

TextBox2.Text = ""

TextBox3.Text = ""

TextBox4.Text = ""

TextBox5.Text = ""

ComboBox1.Text = ""

ComboBox1.Focus() End Sub

6. Pada prosedur Tampilkan berfungsi untuk mengambil data dari tabel Pelanggan dengan kode sebagai berikut :

Sub Tampilkan()

DA = New OleDbDataAdapter("Select * from Pelanggan", CONN)

DS = New DataSet DS.Clear()

DA.Fill(DS, "Pelanggan")

DGV.DataSource = (DS.Tables("Pelanggan")) DGV.ReadOnly = True

End Sub

7. Masukkan kode berikut ini pada objek form atau pelanggan prosedur load sebagai berikut :

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

Call Koneksi() Call Tampilkan()

End Sub

8. Masukkan kode berikut ini pada objek Textboxt1 prosedur KeyPress :

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress TextBox1.MaxLength = 3

If e.KeyChar = Chr(13) Then Try

CMD = New OleDbCommand("select * from Pelanggan where KodePlg='" & TextBox1.Text & "'", CONN)

RD = CMD.ExecuteReader RD.Read()

If RD.HasRows = True Then

TextBox2.Text = RD.GetString(1) TextBox3.Text = RD.GetValue(2) TextBox4.Text = RD.GetValue(3) TextBox5.Text = RD.GetValue(4) ComboBox1.Text = RD.GetString(5) TextBox2.Focus()

Else

Call DataBaru() TextBox2.Focus() End If

Catch ex As Exception

End Try End If

End Sub

Kode diatas berfungsi ketika nilai yang sama dimasukkan pada textbox1 maka akan dimunculkan data pelanggan yang sebelumnya telah terinput.

9. Masukkan kode pada objek textbox2 prosedur KeyPress sebagai berikut : Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

If e.KeyChar = Chr(13) Then TextBox3.Focus() End Sub

10. Masukkan kode pada objek textbox3 prosedur KeyPress sebagai berikut : Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress If e.KeyChar = Chr(13) Then TextBox4.Focus()

End Sub

11. Masukkan kode pada objek textbox4 prosedur KeyPress sebagai berikut Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress If e.KeyChar = Chr(13) Then TextBox5.Focus()

If Not ((e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = vbBack) Then e.Handled() = True

End Sub

12. Masukkan kode pada objek textbox5 prosedur KeyPress sebagai berikut Private Sub TextBox5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress If e.KeyChar = Chr(13) Then ComboBox1.Focus()

End Sub

13. Masukkan kode pada objek combobox1 prosedur KeyPress sebagai berikut Private Sub combobox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

If e.KeyChar = Chr(13) Then Button1.Focus() End Sub

Pada kode objek textbox2, textbox3, textbox4, textbox5 dan combobox1 prosedur KeyPress berfungsi jika nilai telah diisi pada textbox2 maka kursor akan dilanjutkan pada combobox1 dan seterusnya pada button1.

14. Masukkan kode pada button1 yang berfungsi untuk menyimpan data pelanggan ke tabel pelanggan

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = ""

Or TextBox4.Text = "" Or TextBox5.Text = "" Or ComboBox1.Text = "" Then MsgBox("Data Belum Lengkap")

Exit Sub Else

CMD = New OleDbCommand("Select * from Pelanggan where KodePlg='" & TextBox1.Text & "'", CONN)

RD = CMD.ExecuteReader RD.Read()

If Not RD.HasRows Then

Dim sqltambah As String = "Insert into

Pelanggan(KodePlg,NamaPlg,AlamatPlg,TeleponPlg,EmailPlg,JK) values " & _

"('" & TextBox1.Text & "','" & TextBox2.Text & "','" &

TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" &

ComboBox1.Text & "')"

CMD = New OleDbCommand(sqltambah, CONN) CMD.ExecuteNonQuery()

Call Kosongkan() Call Tampilkan() Else

Dim sqledit As String = "Update Pelanggan set " & _ "NamaPlg='" & TextBox2.Text & "', " & _

"AlamatPlg='" & TextBox3.Text & "', " & _ "TeleponPlg='" & TextBox4.Text & "', " & _ "EmailPlg='" & TextBox5.Text & "', " & _

"JK='" & ComboBox1.Text & "' " & _ "where KodePlg='" & TextBox1.Text & "'"

CMD = New OleDbCommand(sqledit, CONN) CMD.ExecuteNonQuery()

Call Kosongkan() Call Tampilkan() End If

End If End Sub

15. Masukkan kode pada button2 berfungsi untuk menghapus data pelanggan Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

If TextBox1.Text = "" Then

MsgBox("Isi kode Pelanggan terlebih dahulu")

TextBox1.Focus() Exit Sub

Else

If MessageBox.Show("Yakin akan dihapus..?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

CMD = New OleDbCommand("Delete * from Pelanggan where KodePlg='" & TextBox1.Text & "'", CONN)

CMD.ExecuteNonQuery() Call Kosongkan()

Call Tampilkan() Else

Call Kosongkan() End If

End If End Sub

16. Masukkan kode pada button3 berfungsi untuk menghilangkan nilai pada textbox dan combobox

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Call Kosongkan() End Sub

17. Masukkan kode pada button4 untuk menutup form pelanggan

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Me.Close() End Sub

18. Hasil dari kode program diatas, dapat dilihat pada gambar 7.5 berikut ini :

Gambar 7.5Hasil Form Pelanggan

Dokumen terkait