NAMA : RAHMAT HIDAYAT
NIM : 1815025091
KELAS : ILKOM B 2018
MATKUL PRAKTIKUM : REKAYASA PERANGKAT LUNAK B2
Source Code
Imports System.Data.Odbc
Public Class Form1
Dim Conn As OdbcConnection
Dim Cmd As OdbcCommand
Dim Ds As DataSet
Dim Da As OdbcDataAdapter
Dim Rd As OdbcDataReader
Dim MyDB As String
*Koneksi Data
Sub Koneksi()
MyDB = "Driver={MySQL ODBC 3.51 Driver};Database=db_distro;Server=Localhost;uid=root"
Conn = New OdbcConnection(MyDB)
If Conn.State = ConnectionState.Closed Then Conn.Open()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Call KondisiAwal()
End Sub
*Sub Tampil Data
Sub KondisiAwal()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
Button1.Text = "Tambah Data"
Button2.Text = "Ubah Data"
Button3.Text = "Hapus Data"
Button4.Text = "Cari"
Button5.Text = "Exit"
Call Koneksi()
Da = New OdbcDataAdapter("Select * From tb_barang", Conn)
Ds = New DataSet
Da.Fill(Ds, "tb_barang")
DataGridView1.DataSource = Ds.Tables("tb_barang")
End Sub
*Button 1
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or
TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Then
MsgBox("Pastikan semua field terisi ")
Else
Call Koneksi()
Dim TambahData As String = "Insert into tb_barang values ('" & TextBox1.Text & "','" &
TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" &
TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "')"
Cmd = New OdbcCommand(TambahData, Conn)
Cmd.ExecuteNonQuery()
MsgBox("Tambah Data Berhasil")
Call KondisiAwal()
End If
End Sub
*Button 2
Private Sub Button2_Click(ByVal sender As Object, e As EventArgs) Handles Button2.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or
TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Then
MsgBox("Pastikan semua field terisi ")
Else
Call Koneksi()
Dim UbahData As String = "Update tb_barang set Nama='" & TextBox2.Text & "',TTL='" &
TextBox3.Text & "',Tempat Lahir ='" & TextBox4.Text & "',No Telepon='" & TextBox5.Text & "' ,Barang='"
& TextBox6.Text & "' ,Harga Barang='" & TextBox7.Text & "',Alamat= '" & TextBox8.Text & "' where ID = '"
& TextBox1.Text & "'"
Cmd = New OdbcCommand(UbahData, Conn)
Cmd.ExecuteNonQuery()
MsgBox("Ubah Data Berhasil")
Call KondisiAwal()
End If
End Sub
*Button 3
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or
TextBox5.Text = "" Or TextBox6.Text = "" Or TextBox7.Text = "" Or TextBox8.Text = "" Then
MsgBox("Pastikan Data yang Akan Dihapus Terisi ")
Else
Call Koneksi()
Dim HapusData As String = "Delete from tb_barang where ID='" & TextBox1.Text & "'"
Cmd = New OdbcCommand(HapusData, Conn)
Cmd.ExecuteNonQuery()
MsgBox("Hapus Data Berhasil")
Call KondisiAwal()
End If
End Sub
*Button 4
Private Sub Button4_Click(ByVal sender As Object, e As EventArgs) Handles Button4.Click
Call Koneksi()
Cmd = New OdbcCommand("Select * from tb_barang where nama Like '%" & TextBox9.Text & "%'",
Conn)
Rd = Cmd.ExecuteReader
Rd.Read()
If Rd.HasRows Then
Call Koneksi()
Da = New OdbcDataAdapter("Select * from tb_barang where nama Like '%" & TextBox9.Text & "%'",
Conn)
Ds = New DataSet
Da.Fill(Ds, "Data Ketemu")
DataGridView1.DataSource = Ds.Tables("Data Ketemu")
DataGridView1.ReadOnly = True
End If
End Sub
*TextBox 1
Private Sub TextBox1_KeyPress(ByVal sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
Call Koneksi()
Cmd = New OdbcCommand("Select * From tb_barang where ID ='" & TextBox1.Text & "'", Conn)
Rd = Cmd.ExecuteReader
Rd.Read()
If Rd.HasRows Then
TextBox2.Text = Rd.Item("Nama")
TextBox3.Text = Rd.Item("TTL")
TextBox4.Text = Rd.Item("Tempat Lahir")
TextBox5.Text = Rd.Item("No Telepon")
TextBox6.Text = Rd.Item("Barang")
TextBox7.Text = Rd.Item("Harga Barang")
TextBox8.Text = Rd.Item("Alamat")
Else
MsgBox("Data Tidak Ada")
End If
End If
End Sub