Berikut langkah-langkahnya :
Pastikan anda sudah menginstall :
- Xampp, sebagai aplikasi untuk database
- Visual Basic 2010 (Baik versi Express maupun Ultimate)
- Conector odbc versi 3.51 (sebagai koneksi VB 2010 ke MySQL)
Buatlah database pada Xampp, dengan nama : dbbelajarvb
Buatlah Tabel dengan nama : barang
Agar lebih terlihat dengan bagus nantinya masukan data di tabel barang tersebut. sebagai contoh seperti dibawah ini :
Jika sudah membuat database dan Tabel diatas, mari kita lanjutkan ke Langkah 2.
Langkah 2 :
Buatlah Project dengan VB 2010, buatlah design form seperti gambar dibawah ini :
Form diatas terdapat : TexBox1, TexBox2, TexBox3, TexBox4, Button1, DatagridView1
Kemudian masukan Koding dibawah ini pada Form1 diatas :
Imports System.Data.Odbc
Public Class Form1
Dim Conn As OdbcConnection
Dim da As OdbcDataAdapter
Dim ds As DataSet
Dim str As String
Dim CMD As OdbcCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call KondisiAwal()
End Sub
Sub Koneksi()
str = "Driver={MySQL ODBC 3.51 Driver};database=dbbelajarvb;server=localhost;uid=root"
Conn = New OdbcConnection(str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
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 = "" Then
MsgBox("Data belum lengkap, Pastikan Kode Barang 6 Digit dan Semua form terisi")
Exit Sub
Else
Call Koneksi()
Dim simpan As String = "insert into Barang values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
CMD = New OdbcCommand(simpan, Conn)
CMD.ExecuteNonQuery()
MsgBox("Data berhasil di Input", MsgBoxStyle.Information, "Information")
Call KondisiAwal()
End If
End Sub
Sub KondisiAwal()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
Me.Text = "www.belajarvb.com"
Koneksi()
da = New OdbcDataAdapter("Select * from Barang", Conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "Barang")
DataGridView1.DataSource = (ds.Tables("Barang"))
End Sub
End Class
Silahkan jalankan Project VB 2010 anda, Masukan Kode Barang, Nama, Harga dan Jumlah.
Kemudian Klik Tombol Simpan
Salam
BelajarVB.com