Imports System.Data
Imports System.Data.OleDb
Namespace AccessData
Public Class DatabaseConnection
// untuk acces 2003
Dim conect As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppPath + "namadatabase.mdb;")
//untuk access 2007
Dim conect As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppPath + "namadatabase.mdb;")
Public Function open() As OleDbConnection
conect.Open()
Return conect
End Function
Public Function close() As OleDbConnection
conect.Close()
Return conect
End Function
End Class
End Namespace
tambahkan kode berikut pada form didalam sebuah region untuk memanggil koneksi.
#Region "Deklarasi"
Dim MyConnection As New AccessData.DatabaseConnection
#End Region
---------------------------------------------------------------------------------------
contoh pada form kerja:
#Region "Deklarasi"
Dim MyConnection As New AccessData.DatabaseConnection ( berasal dari modul)
Dim mPosition As Integer
Dim objConnection As OleDbConnection
Dim objCommand As OleDbCommand
Dim objDataAdapter As OleDbDataAdapter
Dim objdatareader As OleDbDataReader
Dim strSQL As String
Dim objDataSet As New DataSet
Dim objDataTable As New DataTable
Dim mProcess As String
Protected MyConnectionBarang As New AccessData.AccessBarang -( berasal dari class)
#End Region
-----------------------------------------------------------------------------------
dipanggil dan di eksekusi pada saat suatu tombol di klik:
ElseIf btnsave.Text = "Save Edit" Then
Try
Dim mYes_No As String = MsgBox("Anda yakin data mau diupdate ?", vbYesNo)
If mYes_No = vbYes Then
MyConnectionBarang.AccessBarangUpdate(UCase(Trim(txtKode_Brg.Text)), StrConv(Trim(txtDeskripsi.Text), VbStrConv.ProperCase), _
Trim(txtPhoto.Text), Trim(txtStok.Text), Trim(txtHrg_Beli.Text), Trim(txtHrg_Jual.Text), Trim(txtMinimum.Text))
NonAktif_Inputan()
btnsave.Text = "Save (Ctrl+S)"
btnsave.Enabled = False
btnadd.Focus()
Displaydata()
clear()
Else
NonAktif_Inputan()
clear()
btnadd.Focus()
End If
Catch When Err.Number <> 0
MsgBox("Tidak dapat melakukan proses update" _
& vbCrLf & Err.Description)
MyConnection.close()
End Try
End If
Imports System.Data.OleDb
Namespace AccessData
Public Class DatabaseConnection
// untuk acces 2003
Dim conect As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppPath + "namadatabase.mdb;")
//untuk access 2007
Dim conect As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppPath + "namadatabase.mdb;")
Public Function open() As OleDbConnection
conect.Open()
Return conect
End Function
Public Function close() As OleDbConnection
conect.Close()
Return conect
End Function
End Class
End Namespace
tambahkan kode berikut pada form didalam sebuah region untuk memanggil koneksi.
#Region "Deklarasi"
Dim MyConnection As New AccessData.DatabaseConnection
#End Region
---------------------------------------------------------------------------------------
contoh pada form kerja:
#Region "Deklarasi"
Dim MyConnection As New AccessData.DatabaseConnection ( berasal dari modul)
Dim mPosition As Integer
Dim objConnection As OleDbConnection
Dim objCommand As OleDbCommand
Dim objDataAdapter As OleDbDataAdapter
Dim objdatareader As OleDbDataReader
Dim strSQL As String
Dim objDataSet As New DataSet
Dim objDataTable As New DataTable
Dim mProcess As String
Protected MyConnectionBarang As New AccessData.AccessBarang -( berasal dari class)
#End Region
-----------------------------------------------------------------------------------
dipanggil dan di eksekusi pada saat suatu tombol di klik:
ElseIf btnsave.Text = "Save Edit" Then
Try
Dim mYes_No As String = MsgBox("Anda yakin data mau diupdate ?", vbYesNo)
If mYes_No = vbYes Then
MyConnectionBarang.AccessBarangUpdate(UCase(Trim(txtKode_Brg.Text)), StrConv(Trim(txtDeskripsi.Text), VbStrConv.ProperCase), _
Trim(txtPhoto.Text), Trim(txtStok.Text), Trim(txtHrg_Beli.Text), Trim(txtHrg_Jual.Text), Trim(txtMinimum.Text))
NonAktif_Inputan()
btnsave.Text = "Save (Ctrl+S)"
btnsave.Enabled = False
btnadd.Focus()
Displaydata()
clear()
Else
NonAktif_Inputan()
clear()
btnadd.Focus()
End If
Catch When Err.Number <> 0
MsgBox("Tidak dapat melakukan proses update" _
& vbCrLf & Err.Description)
MyConnection.close()
End Try
End If