Selamat sore semuanya
Kali ini saya akan berbagi ilmu dengan semuanya tentang visual basic.
Disini saya akan membuat program Pengisian Data Kendaraan dengan menggunakan visual basic.
Pertama sekali kita harus mempunyai aplikasi Microsoft Visual Basic.
Setelah itu buat database melalui Microsoft Access dengan nama kendaraan.mdb dengan field sebagai berikut:
- No Polisi Kendaraan
- Pemilik
- Alamat
- Tahun
- Warna
- No Rangka
Dan akan tampil seperti di bawah ini:
Tampilan database kendaraan.mdb pada Microsoft Access
Setelah selesai dengan database, langsung aja buka Microsoft Visual Basic dan lalu pilih Create untuk membuat form baru.
|
Imports System.Data.OleDb
Public Class Form1
Public db As OleDbConnection
Public cmd As OleDbCommand
Public adp As OleDbDataAdapter
Public dr As OleDbDataReader
Public ds As New DataSet
Dim sql As String
Public Sub opendb()
Dim lokasi = "provider=microsoft.jet.oledb.4.0;data source =C:\VB2\kendaraan.mdb"
db = New OleDbConnection(lokasi)
If db.State = ConnectionState.Closed Then
db.Open()
End If
End Sub
Sub tampilrecordkendaraan()
adp = New OleDbDataAdapter("select * from kendaraan", db)
ds = New DataSet
adp.Fill(ds, "kendaraan")
DataGridView1.DataSource = ds.Tables("kendaraan")
End Sub
Sub ButtonBaru()
btnBaru.Enabled = True
btnSimpan.Enabled = False
btnEdit.Enabled = False
btnHapus.Enabled = False
btnBatal.Enabled = False
btnKeluar.Enabled = True
End Sub
Sub ButtonEdit()
btnBaru.Enabled = False
btnSimpan.Enabled = False
btnEdit.Enabled = True
btnHapus.Enabled = True
btnBatal.Enabled = True
btnKeluar.Enabled = False
End Sub
Sub ButtonSimpan()
btnBaru.Enabled = False
btnSimpan.Enabled = True
btnEdit.Enabled = False
btnHapus.Enabled = False
btnBatal.Enabled = True
btnKeluar.Enabled = False
End Sub
Sub hapustext()
NoBK.Text = ""
pemilik.Text = ""
alamat.Text = ""
tahun.Text = ""
warna.Text = ""
norangka.Text = ""
Call ButtonBaru()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call opendb()
Call hapustext()
Call tampilrecordkendaraan()
End Sub
Private Sub btnhapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhapus.Click
sql = "DELETE FROM kendaraan Where NoBK='" & NoBK.Text & "'"
cmd = New OleDbCommand(sql, db)
cmd.ExecuteNonQuery()
Call btnbaru_click(sender, e)
Call tampilrecordkendaraan()
End Sub
Private Sub btnkeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkeluar.Click
Me.Close()
End Sub
Private Sub btnbaru_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbaru.Click
Call hapustext()
NoBK.Focus()
End Sub
Private Sub NoBK_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NoBK.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
Dim xkode As String
If tombol = 13 Then
sql = "select * from kendaraan where NoBK ='" & NoBK.Text & "'"
cmd = New OleDbCommand(sql, db)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows = True Then
pemilik.Text = dr.Item(1)
alamat.Text = dr.Item(2)
tahun.Text = dr.Item(3)
warna.Text = dr.Item(4)
norangka.Text = dr.Item(5)
Call ButtonEdit()
MsgBox("Data Ditemukan !!")
Else
xkode = NoBK.Text
Call hapustext()
NoBK.Text = xkode
Call ButtonSimpan()
End If
pemilik.Focus()
End If
End Sub
Private Sub NoBK_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoBK.TextChanged
End Sub
Private Sub btnbatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbatal.Click
Call hapustext()
NoBK.Focus()
End Sub
Private Sub btnsimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsimpan.Click
sql = "insert into kendaraan (NoBK,pemilik,alamat,tahun,warna,norangka)" & _
"values ('" & NoBK.Text & "','" & _
pemilik.Text & "','" & _
alamat.Text & "','" & _
tahun.Text & "','" & _
warna.Text & "','" & _
norangka.Text & "')"
cmd = New OleDbCommand(sql, db)
cmd.ExecuteNonQuery()
Call btnbaru_Click(sender, e)
Call tampilrecordkendaraan()
End Sub
Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
sql = "update kendaraan set pemilik='" & pemilik.Text & "'," & _
"alamat='" & alamat.Text & "'," & _
"tahun='" & tahun.Text & "'," & _
"warna='" & warna.Text & "'," & _
"norangka='" & norangka.Text & "'" & _
"where NoBK='" & NoBK.Text & "'"
cmd = New OleDbCommand(sql, db)
cmd.ExecuteNonQuery()
Call btnbaru_Click(sender, e)
Call tampilrecordkendaraan()
End Sub
End Class
Dan berikut adalah tampilan hasil program yang suadah bisa di jalankan.
|
Tidak ada komentar:
Posting Komentar