Kali ini saya mau berbagi cara membuat CRUD Form di Visual Basic 2015 menggunakan database Microsoft Access. Karena Ms Acces itu mudah di gunakkan untuk yang baru mulai belajar VB.Net, jadi belajarlah dari yang mudah ya. Dan langsung saja Ikuti langkah-langkahnya.
Pertama Buatlah Form Seperti berikut:
Lalu buatlah Module dan masukkan script berikut:
Imports System.Data.OleDb
Module koneksi
Public conn As OleDbConnection
Public cmd As OleDbCommand
Public ds As DataSet
Public dr As OleDbDataReader
Public da As OleDbDataAdapter
Public lokasidata As String
Public Sub konek()
lokasidata = "provider=microsoft.jet.oledb.4.0;data
source=db_biodata.mdb"
conn = New OleDbConnection(lokasidata)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
End Module
Lalu di Form nya masukkan script berikut:
Public Class Form1
Public newData As Boolean
Private Sub
Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
newData = False
ShowData()
LoadDataToTextbox()
End Sub
Private Sub
ShowData()
konek()
da = New OleDb.OleDbDataAdapter("SELECT * FROM
tbl_biodata", conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "tbl_biodata")
MetroGrid1.DataSource = ds.Tables("tbl_biodata")
da.Dispose()
ds.Dispose()
conn.Close()
End Sub
Private Sub
LoadDataToTextbox()
Dim i As Integer
i = MetroGrid1.CurrentRow.Index
MetroTextBox1.Text = MetroGrid1.Item(0,
i).Value
MetroTextBox2.Text = MetroGrid1.Item(1,
i).Value
MetroTextBox3.Text = MetroGrid1.Item(2,
i).Value
MetroTextBox4.Text = MetroGrid1.Item(3,
i).Value
MetroTextBox5.Text = MetroGrid1.Item(4,
i).Value
End Sub
Private Sub
MetroButton1_Click(sender As Object, e As EventArgs) Handles
MetroButton1.Click
newData = True
ClearData()
End Sub
Private Sub
MetroButton2_Click(sender As Object, e As EventArgs) Handles
MetroButton2.Click
Dim save As String
If newData Then
save = "INSERT INTO tbl_biodata (idsiswa,nama,nisn,kelas,alamat)
VALUES('" & MetroTextBox1.Text & "','" & MetroTextBox2.Text
& "','" &
MetroTextBox3.Text & "','" & MetroTextBox4.Text & "','" & MetroTextBox5.Text & "')"
Else
save = "UPDATE tbl_biodata SET nama='" & MetroTextBox2.Text & "',nisn='" & MetroTextBox3.Text & "',kelas='" & MetroTextBox4.Text & "',alamat='" & MetroTextBox5.Text & "'
WHERE idsiswa=" & MetroTextBox1.Text & " "
End If
doSQL(save)
ShowData()
End Sub
Private Sub doSQL(ByVal sql As String)
konek()
Dim objcmd As New OleDb.OleDbCommand
Try
objcmd.Connection = conn
objcmd.CommandType = CommandType.Text
objcmd.CommandText = sql
objcmd.ExecuteNonQuery()
MsgBox("Data Tersimpan !!!",
vbInformation)
objcmd.Dispose()
Catch ex As Exception
MsgBox("Data Tidak Tersimpan" &
ex.Message)
End Try
End Sub
Private Sub
MetroGrid1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles
MetroGrid1.CellContentClick
newData = False
LoadDataToTextbox()
End Sub
Private Sub
ClearData()
MetroTextBox1.Text = ""
MetroTextBox2.Text = ""
MetroTextBox3.Text = ""
MetroTextBox4.Text = ""
MetroTextBox5.Text = ""
MetroTextBox1.Focus()
End Sub
Private Sub
MetroButton3_Click(sender As Object, e As EventArgs) Handles MetroButton3.Click
Dim delete As String
Dim vMessage As String
vMessage = MsgBox("Apakah Anda yakin ingin menghapus biodata Siswa ini?", vbExclamation + vbYesNo, "Perhatian")
If vMessage = vbNo Then Exit Sub
delete = "DELETE
FROM tbl_biodata WHERE idsiswa=" &
MetroTextBox1.Text & ""
doSQL(delete)
ClearData()
ShowData()
End Sub
Private Sub
MetroButton4_Click(sender As Object, e As EventArgs) Handles
MetroButton4.Click
Me.Close()
End Sub
End Class
Silahkan sesuaikan dengan textbox, datagrid, dan button nya sesuai dengan yang anda gunakan. Selesai deh, silahkan coba di run, kalo ada masalah silahkan komen ya.
0 Komentar