Crystal Report pada VB .NET

Buka kembali file Inventory.vb untuk melakukan atau menambahkan Crystal Report pada form inventory tersebut dengan database inventory yang sudah ada.
a.       Source code
Imports MySql.Data.MySqlClient

Module Koneksi
    Public cmmd As MySqlCommand 'untuk eksekusi query sql
    Public dreader As MySqlDataReader 'untuk menampung hasil query
    Public cnn As MySqlConnection 'untuk membuka koneksi ke sql

    Public connStr As String = "server=localhost;user=root;database=dbinventory;"

    Public Sub reconnect()
        cnn = New MySqlConnection(connStr)
        If cnn.State <> ConnectionState.Closed Then
            cnn.Close()
        End If
        cnn.Open()
    End Sub

    Public Function sqlSelect(ByVal sql As String) As DataTable
        ' kamus
        Dim dataAdapter As New MySqlDataAdapter
        Dim dataTable As New DataTable

        ' algoritma
        reconnect()

        ' sql command
        cmmd = New MySqlCommand(sql, cnn)

        ' data adapter
        dataAdapter.SelectCommand = cmmd
        dataAdapter.Fill(dataTable)

        ' destroy objects
        dataAdapter.Dispose()
        cmmd.Dispose()
        cnn.Close()

        Return dataTable
    End Function

    Public Function sqlQuery(ByVal sql As String) As Boolean
        ' kamus
        Dim state As Integer

        ' algoritma
        reconnect()

        ' sql command
        cmmd = New MySqlCommand(sql, cnn)
        state = cmmd.ExecuteNonQuery

        ' destroy objects
        cmmd.Dispose()
        cnn.Close()

        If state = 1 Then
            Return True
        Else
            Return False
        End If
    End Function

End Module   

Source code di form DataGridView
Public Class FormDataGridView

    Private Sub FormDataGridView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call refreshTable()
    End Sub

    Private Sub tambahBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tambahBtn.Click
        If kodeBarangTB.Text = "" Or namaBarangTB.Text = "" Or jenisBarangTB.Text = "" Or jumlahTB.Text = "" Then
            MsgBox("Lengkapi data barang!", MsgBoxStyle.Critical, "Error")
            Return
        End If

        ' kamus
        Dim sql As String

        ' algoritma
        sql = "INSERT INTO tbbarang VALUES('" & kodeBarangTB.Text & "','" & namaBarangTB.Text & "','" & jenisBarangTB.Text & "','" & jumlahTB.Text & "')"

        If sqlQuery(sql) Then
            clearData()
            refreshTable()
            MsgBox("Data berhasil ditambahkan", MsgBoxStyle.Information, "Insert Information")
        Else
            MsgBox("Data tidak berhasil ditambahkan", MsgBoxStyle.Information, "Insert Information")
        End If
    End Sub

    Private Sub ubahBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ubahBtn.Click
        If kodeBarangTB.Text = "" Then
            MsgBox("Anda belum memilih data yang akan diubah!", MsgBoxStyle.Critical, "Error")
            Return
        End If

        ' kamus
        Dim sql As String

        ' algoritma
        sql = "UPDATE tbbarang SET kodebrg='" & kodeBarangTB.Text & "', namabrg='" & namaBarangTB.Text & "', jnsbrg='" & jenisBarangTB.Text & "', jumbrg='" & jumlahTB.Text & "' WHERE kodebrg ='" & kodeBarangTB.Text & "'"

        If sqlQuery(sql) Then
            clearData()
            refreshTable()
            MsgBox("Data berhasil diperbaharui", MsgBoxStyle.Information, "Update Information")
        Else
            MsgBox("Data tidak berhasil diperbaharui", MsgBoxStyle.Information, "Update Information")
        End If
    End Sub

    Private Sub hapusBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hapusBtn.Click
        If kodeBarangTB.Text = "" Then
            MsgBox("Anda belum memilih data yang akan dihapus!", MsgBoxStyle.Critical, "Error")
            Return
        End If

        ' kamus
        Dim sql As String

        ' algoritma
        sql = "DELETE FROM tbbarang WHERE kodebrg='" & kodeBarangTB.Text & "'"

        If sqlQuery(sql) Then
            clearData()
            refreshTable()
            MsgBox("Data berhasil diperbaharui", MsgBoxStyle.Information, "Update Information")
        Else
            MsgBox("Data tidak berhasil diperbaharui", MsgBoxStyle.Information, "Update Information")
        End If
    End Sub

    Private Sub cariTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cariTB.TextChanged
        refreshTable()
    End Sub

    Private Sub DGV_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellDoubleClick
        kodeBarangTB.Text = DGV.SelectedRows(0).Cells(0).Value
        namaBarangTB.Text = DGV.SelectedRows(0).Cells(1).Value
        jenisBarangTB.Text = DGV.SelectedRows(0).Cells(2).Value
        jumlahTB.Text = DGV.SelectedRows(0).Cells(3).Value

        kodeBarangTB.ReadOnly = True
    End Sub

    Private Sub clearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearBtn.Click
        Call clearData()
    End Sub

    Private Sub refreshTable()
        DGV.DataSource = sqlSelect("SELECT * FROM tbbarang WHERE namaBrg like '%" & Trim(cariTB.Text) & "%'ORDER BY namabrg ASC")
    End Sub

    Private Sub clearData()
        kodeBarangTB.Clear()
        namaBarangTB.Clear()
        jenisBarangTB.Clear()
        jumlahTB.Clear()

        kodeBarangTB.ReadOnly = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Formreport.Show()
    End Sub
End Class

Anallisa
Dalam program ini dikenalkan dengan Cristal Report dimana hasil dari form Data Grid akan di report pada form report yang telah dibuat interfacenya. Untuk konektor database xampp menggunakan mysql connector odbc maka database tersebut akan menjadi hasil report yang ditampilkan pada form report.

Hasil Form Data Grid
Hasil Form Report

0 Response to "Crystal Report pada VB .NET"

Post a Comment