soce code untuk koneksi ke data base
Imports System.Data
Imports System.Data.OleDb
Module Module1
Public Conn As OleDbConnection
Public da As OleDbDataAdapter
Public ds As DataSet
Public cmd As OleDbCommand
Public rd As OleDbDataReader
Public str As String
Public Sub Koneksi()
str = "provider=microsoft.jet.oledb.4.0;data source=db1.mdb"
Conn = New OleDbConnection(str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
End Module
Imports System.Data.OleDb
Module Module1
Public Conn As OleDbConnection
Public da As OleDbDataAdapter
Public ds As DataSet
Public cmd As OleDbCommand
Public rd As OleDbDataReader
Public str As String
Public Sub Koneksi()
str = "provider=microsoft.jet.oledb.4.0;data source=db1.mdb"
Conn = New OleDbConnection(str)
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub
End Module
soce code untuk form
Imports System.Data
Imports System.Data.OleDb
Public Class F_Buku
#Region "TampilanDataGrive"
Sub AturkolomDGV()
Me.DGV.DefaultCellStyle.ForeColor = Color.Black
Me.DGV.RowsDefaultCellStyle.BackColor = Color.AliceBlue
Me.DGV.GridColor = Color.Blue
Me.DGV.BorderStyle = BorderStyle.Fixed3D
Me.DGV.RowHeadersBorderStyle = BorderStyle.Fixed3D
DGV.Columns(0).Width = 100
DGV.Columns(1).Width = 300
DGV.Columns(2).Width = 280
DGV.Columns(3).Width = 60
DGV.Columns(4).Width = 60
End Sub
#End Region
#Region "olah"
Sub matikan()
txtjudul.Enabled = False
txtpengarang.Enabled = False
txtjumlah.Enabled = False
txtthn.Enabled = False
End Sub
Sub hidupkan()
txtjudul.Enabled = True
txtpengarang.Enabled = True
txtjumlah.Enabled = True
txtthn.Enabled = True
End Sub
Sub Kosongkan()
txtnomer.Text = ""
txtjudul.Text = ""
txtpengarang.Text = ""
txtjumlah.Text = ""
txtthn.Text = ""
txtnomer.Focus()
End Sub
Sub DataBaru()
txtjudul.Text = ""
txtpengarang.Text = ""
txtjumlah.Text = ""
txtthn.Text = ""
txtjudul.Focus()
End Sub
Sub navigasi()
BtnSave.Enabled = False
BtnDelete.Enabled = False
Btnganti.Visible = True
Btnsave2.Visible = False
End Sub
Sub Tampilkan()
da = New OleDbDataAdapter("Select nobk
as [Nomer Buku], judul as [Judul], Pengarang as [Pengarang], thn as [Tahun],
stok as [Jumlah] from tbbuku ", conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "tbbuku")
DGV.DataSource = (ds.Tables("tbbuku"))
AturkolomDGV()
DGV.ReadOnly = True
End Sub
Sub ambil()
cmd = New OleDbCommand("select * from tbbuku where
nobk='" & txtnomer.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If rd.HasRows = True Then
txtjudul.Text = rd.GetString(1)
txtpengarang.Text = rd.GetValue(2)
txtjumlah.Text = rd.GetValue(3)
txtthn.Text = rd.GetValue(4)
txtjudul.Focus()
Else
Call DataBaru()
txtnomer.Focus()
End If
End Sub
Sub cari()
If txtnomer.Text = "" Then
MsgBox("Isi Kata
Kunci Pada nomer buku", MsgBoxStyle.Information, "Searching")
txtnomer.Focus()
BtnDelete.Enabled = False
Btnganti.Visible = False
BtnSave.Visible = True
Exit Sub
End If
Try
da = New OleDbDataAdapter("Select nobk
as [Nomer Buku], judul as [Judul], Pengarang as [Pengarang], thn as [Tahun],
stok as [Jumlah] from tbbuku where nobk= '" & txtnomer.Text & "'", conn)
Dim Barang As New DataTable
Barang.Clear()
da.Fill(Barang)
AturkolomDGV()
BtnDelete.Enabled = True
Btnganti.Visible = True
BtnSave.Visible = False
If Barang.Rows.Count = Nothing Then
MsgBox("Data Tidak
Ditemukan, Masukkan kode barang yang tepat !!!", MsgBoxStyle.Information, "Peringatan")
txtnomer.Clear()
txtnomer.Focus()
BtnDelete.Enabled = False
Btnganti.Visible = False
BtnSave.Visible = True
Exit Sub
End If
DGV.DataSource = Barang
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Peringatan")
End Try
End Sub
#End Region
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Me.Dispose()
F_cari.Close()
F_search.Close()
End Sub
Private Sub F_Buku_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
koneksi()
Tampilkan()
navigasi()
matikan()
End Sub
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
matikan()
If txtnomer.Text = "" Or txtthn.Text = "" Or txtpengarang.Text
= "" Or txtjumlah.Text = "" Or txtthn.Text = "" Then
MsgBox("Data Belum Lengkap
!",
MsgBoxStyle.Information, "Peringatan")
Exit Sub
Else
cmd = New OleDbCommand("Select * from
tbbuku where nobk='" & txtnomer.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
If Not rd.HasRows Then
Dim sqltambah As String = "Insert into tbbuku(nobk, judul,
Pengarang, thn, stok) values " & _
"('" &
txtnomer.Text & "','" & txtjudul.Text & "','" &
txtpengarang.Text & "','" & txtjumlah.Text & "','" & txtthn.Text
& "')"
cmd = New OleDbCommand(sqltambah, conn)
cmd.Connection = conn
cmd.ExecuteNonQuery()
Kosongkan()
Tampilkan()
Else
MsgBox("Kode [" &
txtnomer.Text & "] tersebut sudah ada, ganti kode yang lain !!!", MsgBoxStyle.Information, "Peringatan")
Kosongkan()
Tampilkan()
End If
End If
BtnAdd.Enabled = True
txtnomer.Enabled = True
BtnSave.Enabled = False
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnSave.Visible = True
BtnSave.Enabled = True
Btnsave2.Visible = False
hidupkan()
txtnomer.Focus()
End Sub
Private Sub BTnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTnCancel.Click
Kosongkan()
Tampilkan()
navigasi()
hidupkan()
Btnganti.Visible = False
Btnsave2.Visible = False
BtnSave.Visible = True
txtnomer.Enabled = True
End Sub
Private Sub BtnCari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCari.Click
cari()
ambil()
Btnganti.Visible = True
End Sub
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
If MessageBox.Show("Apakah anda yakin akan akam
menghapus data tersebut ?", "Delete", MessageBoxButtons.YesNo) =
Windows.Forms.DialogResult.Yes Then
cmd = New OleDbCommand("Delete * from
tbbuku where nobk='" & txtnomer.Text & "'", conn)
cmd.ExecuteNonQuery()
Kosongkan()
Tampilkan()
Else
Kosongkan()
BtnDelete.Enabled = False
End If
End Sub
Private Sub Btnganti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnganti.Click
hidupkan()
txtnomer.Enabled = False
Btnganti.Visible = False
BtnSave.Visible = False
Btnsave2.Visible = True
Btnsave2.Enabled = True
BtnAdd.Enabled = False
BtnDelete.Enabled = False
txtjudul.Focus()
End Sub
Private Sub Btnsave2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnsave2.Click
hidupkan()
cmd = New OleDbCommand("Select * from tbbuku where
nobk='" & txtnomer.Text & "'", conn)
rd = cmd.ExecuteReader
rd.Read()
Dim sqledit As String = "Update tbbuku set " & _
"judul='" &
txtjudul.Text & "', " & _
"Pengarang='" &
txtpengarang.Text & "', " & _
"thn='" &
txtjumlah.Text & "', " & _
"stok='" & txtthn.Text
& "'
"
& _
"where nobk='" &
txtnomer.Text & "'"
cmd = New OleDbCommand(sqledit, conn)
cmd.ExecuteNonQuery()
Kosongkan()
Tampilkan()
matikan()
BtnDelete.Enabled = False
BtnAdd.Enabled = True
txtnomer.Enabled = True
Btnsave2.Enabled = False
End Sub
End Class
NB:disini Button save dan Button cancel ad dua button yaitu BtnSave sebagai save untuk mennbah data dlm data base.sedangkan Btnsave2 sebagai save saat update data pd data base.
No comments:
Post a Comment