Anda di halaman 1dari 7

RUMUS DASAR PEMROGRAMAN DATABASE

BY : KAML ERWANSYAH, M.KOM.


1. Koneksi VB – MS. Access 2007/2010/2013 Menggunakan Module

Imports System.Data.OleDb ‘COPY DIBAGIAN PALING ATAS

Module Module1

'Conn dan TGD boleh diganti dengan nama yang lain


Public Conn As OleDbConnection
Public TGD As String

Public Sub Koneksi()


TGD = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
Application.StartupPath & "\DbPegawai.accdb"

Conn = New OleDbConnection(TGD)


If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
End Sub

End Module

2. Menampilkan/Mengambil Data dari Database ke Visual Basic (List View)

Try
Koneksi() 'memanggil prosedur koneksi di module
Using KoneksikeTabel As New OleDbConnection(Module1.TGD)
Using MasukData As New OleDbCommand("Select * From TblPegawai", KoneksikeTabel)
KoneksikeTabel.Open()
Using TampilData As OleDbDataReader = MasukData.ExecuteReader
Dim z As Integer = 0
LV.Items.Clear()
While TampilData.Read
LV.Items.Add(TampilData("NIK"))
LV.Items(z).SubItems.Add(TampilData("Nama"))
LV.Items(z).SubItems.Add(TampilData("Alamat"))
LV.Items(z).SubItems.Add(TampilData("JK"))
LV.Items(z).SubItems.Add(TampilData("Nope"))
z = z + 1
End While
End Using
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message())
End Try
3. Menampilkan/Mengambil Data dari Database ke Visual Basic (TextBox)

Try
Koneksi() 'memanggil prosedur koneksi di module
Using KoneksikeTabel As New OleDbConnection(Module1.TGD)
Using MasukData As New OleDbCommand("Select * From TblPegawai", KoneksikeTabel)
KoneksikeTabel.Open()
Using TampilData As OleDbDataReader = MasukData.ExecuteReader
IF TampilData.Read
TxtNIK.Text = TampilData("NIK")
TxtNama.Text = TampilData("Nama")
TxtAlamat.Text = TampilData("Alamat")
End IF
End Using
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message())
End Try

4. Menampilkan/Mengambil Data dari Database ke Visual Basic (TextBox Dengan Data Tertentu)

Try
Koneksi() 'memanggil prosedur koneksi di module
Using KoneksikeTabel As New OleDbConnection(Module1.TGD)
Using MasukData As New OleDbCommand("Select * From TblPegawai Where NIK = ‘” & TxtNIK.Text & “’", KoneksikeTabel)
KoneksikeTabel.Open()
Using TampilData As OleDbDataReader = MasukData.ExecuteReader
IF TampilData.Read
TxtNIK.Text = TampilData("NIK")
TxtNama.Text = TampilData("Nama")
TxtAlamat.Text = TampilData("Alamat")
End IF
End Using
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message())
End Try
5. Rumus Menyimpan Data Ke Dalam Database

Using SimpanData As New OleDbConnection(Module1.TGD)


SimpanData.Open()
Using MasukData As New OleDbCommand("Insert into tblpegawai values('" & TxtNIK.Text & _
"','" & TxtNama.Text & _
"','" & TxtAlamat.Text & _
"','" & CbJK.Text & _
"','" & TxtHP.Text & _
"','" & DtTanggal.Text & _
"','" & CbAgama.Text & "')", SimpanData)
MasukData.ExecuteNonQuery()
End Using
End Using

6. Rumus Merubah Data di Database

Using KoneksiUbah As New OleDbConnection(Module1.TGD)


KoneksiUbah.Open()
Using UbahData As New OleDbCommand("Update tblpegawai set Nik ='" & TxtNIK.Text & _
"',Nama = '" & TxtNama.Text & _
"',Alamat = '" & TxtAlamat.Text & _
"',Jk = '" & CbJK.Text & _
"',Nope = '" & TxtHP.Text & _
"',[tgl lahir]='" & DtTanggal.Text & _
"',Agama = '" & CbAgama.Text & _
"' where Nik = '" & TxtNIK.Text & "'", KoneksiUbah)
UbahData.ExecuteReader()
End Using
End Using

7. Rumus Menghapus Data di Database

Using KoneksiHapus As New OleDbConnection(Module1.TGD)


KoneksiHapus.Open()
Using UbahData As New OleDbCommand("Delete from tblpegawai where nik='" & TxtNIK.Text & "'", KoneksiHapus)
UbahData.ExecuteReader()
End Using
End Using
8. Memindahkan Isi Listview Ke Textbox

TxtNIK.Text = LV.SelectedItems(0).Text.ToString
TxtNama.Text = LV.SelectedItems(0).SubItems(1).Text.ToString
TxtAlamat.Text = LV.SelectedItems(0).SubItems(2).Text.ToString
CbJK.Text = LV.SelectedItems(0).SubItems(3).Text.ToString
TxtHP.Text = LV.SelectedItems(0).SubItems(4).Text.ToString

9. Import Data dari Excel

Dim xlApp As Excel.Application


Dim xlWorkbook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim xlRange As Excel.Range

Dim xlCol As Integer


Dim xlRow As Integer

Dim strDestination As String


Dim Data(0 To 100) As String

Try
With OpenFileDialog1
.Filter = "Excel Office|*.xls;*.xlsx"
.FileName = ""
.ShowDialog()
strDestination = .FileName

End With

With LV
.View = View.Details
.FullRowSelect = True
.GridLines = True
.Items.Clear()

If strDestination <> "" Then

xlApp = New Excel.Application

xlWorkbook = xlApp.Workbooks.Open(strDestination)
xlWorkSheet = xlWorkbook.Worksheets("Sheet1") ' Nama Sheet di Excel yang datanya akan dimasukkan
xlRange = xlWorkSheet.UsedRange
If xlRange.Columns.Count > 0 Then
If xlRange.Rows.Count > 0 Then

For xlRow = 1 To xlRange.Rows.Count - 1

For xlCol = 1 To xlRange.Columns.Count

Data(xlCol) = xlRange.Cells(xlRow + 1, xlCol).text

If xlCol = 1 Then
.Items.Add(Data(xlCol).ToString)
Else
.Items(xlRow - 1).SubItems.Add(Data(xlCol).ToString)
End If

Next

Next
xlWorkbook.Close()
xlApp.Quit()

End If
End If

MsgBox("Proses Tampil Data Selesai")

Else
MessageBox.Show("Silahkan Masukkan Data Yang Benar", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End With

Catch ex As Exception
MsgBox(ex.Message)

End Try

10. Memasukkan Data Ke ListView

With LV
.Items.Add(KodeBarang.Text)
With .Items(.Items.Count - 1).SubItems
.Add(NamaBarang.Text)
.Add(Satuan.Text)
.Add(JumlahBeli.Text)
.Add(Format(Val(HargaBeli.Text.Replace(",", "")), "##,##"))
.Add(Format(Val(JumlahBeli.Text) * Val(Val(HargaBeli.Text.Replace(",", ""))), "##,##"))
End With
End With
11. Memberi Warna Berbeda Untuk Isi ListView

For Each item As ListViewItem In lv.Items


If item.Index Mod 2 = 0 Then
item.BackColor = Color.Plum
Else
item.BackColor = Color.LightGray
End If
Next

12. Menyesuaikan Ukuran Kolom ListView Dengan Lebar Data

LV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)

13. Memberi Warna Gradiasi

Dim d As New System.Drawing.Drawing2D.LinearGradientBrush(Me.ClientRectangle, Color.Cyan, Color.PaleGreen,drawing2D.LinearGradientMode.Vertical)


e.Graphics.FillRectangle(d, Me.ClientRectangle)

14. Menyimpan Data Ke Database Dari Listview

For Each item As ListViewItem In ListView2.Items

Using SimpanData As New OleDbConnection(Module1.TGD)


SimpanData.Open()
Using MasukData As New OleDbCommand("Insert into tbltransaksiapriori values( '" & item.Text & "','" & item.SubItems(1).Text
& "','" & item.SubItems(2).Text & "' )", SimpanData)
MasukData.ExecuteNonQuery()
End Using
End Using

Next

15. Membuat Laporan di Crystal Report

DICOPY DIBAGIAN ATAS PUBLIC CLASS


---------------------------------
Imports System.Data.OleDb
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
DI COPY DIBAGIAN BAWAH FORM LAPORAN
-----------------------------------------------------------------------------
Dim ObjReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo

Dim StrReportPath As String = Application.StartupPath & "\LapPenyerahan.rpt"

CrystalReportViewer1.ReportSource = Nothing
ObjReport.Load(StrReportPath)

ConInfo.ConnectionInfo.UserID = "Admin"
ConInfo.ConnectionInfo.Password = ""
ConInfo.ConnectionInfo.IntegratedSecurity = True

For IntCounter = 0 To ObjReport.Database.Tables.Count - 1


ObjReport.Database.Tables(IntCounter).ApplyLogOnInfo(ConInfo)
Next

objReport.RecordSelectionFormula = ""
CrystalReportViewer1.ReportSource = Nothing
CrystalReportViewer1.ReportSource = ObjReport
CrystalReportViewer1.Show()
CrystalReportViewer1.RefreshReport()