Buka VB.net 2012
Persiapan:
1 form
1 Module
selanjutnya desain from seperti berikut:
Berikut adalah Source Koneksi letakkan pada module
Imports System.Data.OleDb
Module Koneksi
Public sqlQuery As String
Public sqlConnection As New OleDbConnection
Public Sub BukaKoneksi()
sqlConnection = New OleDbConnection
sqlConnection.ConnectionString = "provider=microsoft.jet.oledb.4.0;data Source=" & Application.StartupPath & "\rawatjalan.mdb; User Id=admin; password=;"
Try
sqlConnection.Open()
Catch myerror As OleDbException
MsgBox("Ada kesalahan dalam koneksi database", MsgBoxStyle.Critical, "Not Connection")
End
End Try
End Sub
End Module
Berikut adalah Source pada Form
Imports System.Data.OleDb
Public Class DataPasien
Private Sub DataPasien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbKelamin.Items.Add("Laki-laki")
cmbKelamin.Items.Add("Perempuan")
FormAwal()
TampilkanData()
End Sub
Private Sub TampilkanData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("SELECT * FROM pasien ORDER BY kode_pasien", sqlConnection)
Dim myReader As OleDbDataReader = myCommad.ExecuteReader()
ListView1.Items.Clear()
While myReader.Read
Dim NewListViewItem As New ListViewItem
NewListViewItem.Text = myReader.GetString(0)
NewListViewItem.SubItems.Add(myReader.GetString(1))
NewListViewItem.SubItems.Add(myReader(2))
NewListViewItem.SubItems.Add(myReader(3))
NewListViewItem.SubItems.Add(myReader(4))
ListView1.Items.Add(NewListViewItem)
End While
End Sub
Private Sub SimpanData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("INSERT INTO pasien(kode_pasien,nama_pasien,tanggal_lahir, kelamin,alamat)" & _
"VALUES(@kode_pasien,@nama_pasien,@tanggal_lahir, @kelamin,@alamat)", sqlConnection)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.Parameters.AddWithValue("@nama_pasien", txtNama.Text)
myCommad.Parameters.AddWithValue("@tanggal_lahir", Format(dtp.Value, "yyyy/MM/dd"))
myCommad.Parameters.AddWithValue("@kelamin", cmbKelamin.Text)
myCommad.Parameters.AddWithValue("@alamat", txtAlamat.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data Tersimpan", MsgBoxStyle.Information, "Sukses")
End Sub
Private Sub UpdateData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("UPDATE pasien SET nama_pasien=@nama_pasien,tanggal_lahir=@tanggal_lahir," & _
"kelamin=@kelamin,alamat=@alamat WHERE kode_pasien=@kode_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@nama_pasien", txtNama.Text)
myCommad.Parameters.AddWithValue("@tanggal_lahir", Format(dtp.Value, "yyyy/MM/dd"))
myCommad.Parameters.AddWithValue("@kelamin", cmbKelamin.Text)
myCommad.Parameters.AddWithValue("@alamat", txtAlamat.Text)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data TerUpdate", MsgBoxStyle.Information, "Sukses")
End Sub
Private Sub HapusText()
txtKode.Text = ""
txtNama.Text = ""
dtp.Value = Format(Now)
cmbKelamin.Text = ""
txtAlamat.Text = ""
End Sub
Private Sub FormAwal()
txtKode.Enabled = False
txtNama.Enabled = False
dtp.Enabled = False
cmbKelamin.Enabled = False
txtAlamat.Enabled = False
simpan.Text = "&Simpan"
simpan.Enabled = False
edit.Enabled = False
hapus.Enabled = False
End Sub
Private Sub Ekskusi()
txtKode.Enabled = True
txtNama.Enabled = True
dtp.Enabled = True
cmbKelamin.Enabled = True
txtAlamat.Enabled = True
End Sub
Private Sub simpan_Click(sender As Object, e As EventArgs) Handles simpan.Click
If simpan.Text = "&Simpan" Then
If txtKode.Text = "" Or txtNama.Text = "" Then
MsgBox("Data Belum Lengkap", MsgBoxStyle.Exclamation, "Peringatan")
Exit Sub
Else
SimpanData()
TampilkanData()
HapusText()
FormAwal()
End If
Else
UpdateData()
TampilkanData()
HapusText()
FormAwal()
End If
End Sub
Private Sub baru_Click(sender As Object, e As EventArgs) Handles baru.Click
simpan.Text = "&Simpan"
HapusText()
FormAwal()
Ekskusi()
txtKode.Focus()
simpan.Enabled = True
End Sub
Private Sub edit_Click(sender As Object, e As EventArgs) Handles edit.Click
simpan.Text = "U&pdate"
simpan.Enabled = True
Ekskusi()
txtKode.Enabled = False
txtNama.Focus()
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
With ListView1.SelectedItems.Item(0)
txtKode.Text = .Text
txtNama.Text = .SubItems(1).Text
dtp.Value = .SubItems(2).Text
cmbKelamin.Text = .SubItems(3).Text
txtAlamat.Text = .SubItems(4).Text
End With
End If
edit.Enabled = True
hapus.Enabled = True
End Sub
Private Sub hapus_Click(sender As Object, e As EventArgs) Handles hapus.Click
BukaKoneksi()
If MsgBox("Apakah Data akan di hapus", vbYesNo, "Verifikasi") = vbYes Then
Dim myCommad As New OleDbCommand("DELETE from pasien WHERE kode_pasien=@kode_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data Terhapus", MsgBoxStyle.Information, "Sukses")
TampilkanData()
Else
HapusText()
End If
End Sub
Private Sub txtCari_TextChanged(sender As Object, e As EventArgs) Handles txtCari.TextChanged
BukaKoneksi()
Dim myCommad As New OleDbCommand("SELECT * FROM pasien WHERE nama_pasien LIKE @nama_pasien ORDER BY nama_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@nama_pasien", "%" & txtCari.Text & "%")
Dim myReader As OleDbDataReader = myCommad.ExecuteReader()
ListView1.Items.Clear()
While myReader.Read
Dim NewListViewItem As New ListViewItem
NewListViewItem.Text = myReader.GetString(0)
NewListViewItem.SubItems.Add(myReader.GetString(1))
NewListViewItem.SubItems.Add(myReader(2))
NewListViewItem.SubItems.Add(myReader(3))
NewListViewItem.SubItems.Add(myReader(4))
ListView1.Items.Add(NewListViewItem)
End While
End Sub
Private Sub batal_Click(sender As Object, e As EventArgs) Handles batal.Click
HapusText()
FormAwal()
End Sub
Private Sub keluar_Click(sender As Object, e As EventArgs) Handles keluar.Click
Me.Close()
End Sub
End Class
Public Class DataPasien
Private Sub DataPasien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbKelamin.Items.Add("Laki-laki")
cmbKelamin.Items.Add("Perempuan")
FormAwal()
TampilkanData()
End Sub
Private Sub TampilkanData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("SELECT * FROM pasien ORDER BY kode_pasien", sqlConnection)
Dim myReader As OleDbDataReader = myCommad.ExecuteReader()
ListView1.Items.Clear()
While myReader.Read
Dim NewListViewItem As New ListViewItem
NewListViewItem.Text = myReader.GetString(0)
NewListViewItem.SubItems.Add(myReader.GetString(1))
NewListViewItem.SubItems.Add(myReader(2))
NewListViewItem.SubItems.Add(myReader(3))
NewListViewItem.SubItems.Add(myReader(4))
ListView1.Items.Add(NewListViewItem)
End While
End Sub
Private Sub SimpanData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("INSERT INTO pasien(kode_pasien,nama_pasien,tanggal_lahir, kelamin,alamat)" & _
"VALUES(@kode_pasien,@nama_pasien,@tanggal_lahir, @kelamin,@alamat)", sqlConnection)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.Parameters.AddWithValue("@nama_pasien", txtNama.Text)
myCommad.Parameters.AddWithValue("@tanggal_lahir", Format(dtp.Value, "yyyy/MM/dd"))
myCommad.Parameters.AddWithValue("@kelamin", cmbKelamin.Text)
myCommad.Parameters.AddWithValue("@alamat", txtAlamat.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data Tersimpan", MsgBoxStyle.Information, "Sukses")
End Sub
Private Sub UpdateData()
BukaKoneksi()
Dim myCommad As New OleDbCommand("UPDATE pasien SET nama_pasien=@nama_pasien,tanggal_lahir=@tanggal_lahir," & _
"kelamin=@kelamin,alamat=@alamat WHERE kode_pasien=@kode_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@nama_pasien", txtNama.Text)
myCommad.Parameters.AddWithValue("@tanggal_lahir", Format(dtp.Value, "yyyy/MM/dd"))
myCommad.Parameters.AddWithValue("@kelamin", cmbKelamin.Text)
myCommad.Parameters.AddWithValue("@alamat", txtAlamat.Text)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data TerUpdate", MsgBoxStyle.Information, "Sukses")
End Sub
Private Sub HapusText()
txtKode.Text = ""
txtNama.Text = ""
dtp.Value = Format(Now)
cmbKelamin.Text = ""
txtAlamat.Text = ""
End Sub
Private Sub FormAwal()
txtKode.Enabled = False
txtNama.Enabled = False
dtp.Enabled = False
cmbKelamin.Enabled = False
txtAlamat.Enabled = False
simpan.Text = "&Simpan"
simpan.Enabled = False
edit.Enabled = False
hapus.Enabled = False
End Sub
Private Sub Ekskusi()
txtKode.Enabled = True
txtNama.Enabled = True
dtp.Enabled = True
cmbKelamin.Enabled = True
txtAlamat.Enabled = True
End Sub
Private Sub simpan_Click(sender As Object, e As EventArgs) Handles simpan.Click
If simpan.Text = "&Simpan" Then
If txtKode.Text = "" Or txtNama.Text = "" Then
MsgBox("Data Belum Lengkap", MsgBoxStyle.Exclamation, "Peringatan")
Exit Sub
Else
SimpanData()
TampilkanData()
HapusText()
FormAwal()
End If
Else
UpdateData()
TampilkanData()
HapusText()
FormAwal()
End If
End Sub
Private Sub baru_Click(sender As Object, e As EventArgs) Handles baru.Click
simpan.Text = "&Simpan"
HapusText()
FormAwal()
Ekskusi()
txtKode.Focus()
simpan.Enabled = True
End Sub
Private Sub edit_Click(sender As Object, e As EventArgs) Handles edit.Click
simpan.Text = "U&pdate"
simpan.Enabled = True
Ekskusi()
txtKode.Enabled = False
txtNama.Focus()
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
If ListView1.SelectedItems.Count > 0 Then
With ListView1.SelectedItems.Item(0)
txtKode.Text = .Text
txtNama.Text = .SubItems(1).Text
dtp.Value = .SubItems(2).Text
cmbKelamin.Text = .SubItems(3).Text
txtAlamat.Text = .SubItems(4).Text
End With
End If
edit.Enabled = True
hapus.Enabled = True
End Sub
Private Sub hapus_Click(sender As Object, e As EventArgs) Handles hapus.Click
BukaKoneksi()
If MsgBox("Apakah Data akan di hapus", vbYesNo, "Verifikasi") = vbYes Then
Dim myCommad As New OleDbCommand("DELETE from pasien WHERE kode_pasien=@kode_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@kode_pasien", txtKode.Text)
myCommad.ExecuteNonQuery()
sqlConnection.Close()
MsgBox("Data Terhapus", MsgBoxStyle.Information, "Sukses")
TampilkanData()
Else
HapusText()
End If
End Sub
Private Sub txtCari_TextChanged(sender As Object, e As EventArgs) Handles txtCari.TextChanged
BukaKoneksi()
Dim myCommad As New OleDbCommand("SELECT * FROM pasien WHERE nama_pasien LIKE @nama_pasien ORDER BY nama_pasien", sqlConnection)
myCommad.Parameters.AddWithValue("@nama_pasien", "%" & txtCari.Text & "%")
Dim myReader As OleDbDataReader = myCommad.ExecuteReader()
ListView1.Items.Clear()
While myReader.Read
Dim NewListViewItem As New ListViewItem
NewListViewItem.Text = myReader.GetString(0)
NewListViewItem.SubItems.Add(myReader.GetString(1))
NewListViewItem.SubItems.Add(myReader(2))
NewListViewItem.SubItems.Add(myReader(3))
NewListViewItem.SubItems.Add(myReader(4))
ListView1.Items.Add(NewListViewItem)
End While
End Sub
Private Sub batal_Click(sender As Object, e As EventArgs) Handles batal.Click
HapusText()
FormAwal()
End Sub
Private Sub keluar_Click(sender As Object, e As EventArgs) Handles keluar.Click
Me.Close()
End Sub
End Class
0 komentar:
Posting Komentar