Koneksi Dinamis Crystal Report Pada VB.Net 2010

Koneksi Dinamis Database .accdb Ke Crystal Report Di Vb.net 2010

Selama ini kan kalau bikin crystal report pasti pake koneksi wizard. Biasanya report aku simpan di folder Debug dan memanggilnya menggunakan Crystal Report Viewer. Code untuk memanggil report itu seperti ini:

Private Sub crView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles crView.Load

       Me.crView.ReportSource = Application.StartupPath & "\rpt_kasir.rpt"

       crView.SelectionFormula = "{v_kasir.no_fak}=('" & Trim(txtCari.Text) & "')"

End Sub

Jika aplikasi dijalankan di komputer tempat kita membuat program sih nggak masalah ya.. 

masalah justru timbul saat aplikasi mau dipasang di komputer lain. Satu-satunya cara ya nyimpen si file database (untuk kasus ini menggunakan dbms access) sesuai dengan lokasi database di komputer kita. Tapi gimana kalau pada awalnya si file database tersebut disimpan di drive D sementara di komputer tujuan hanya ada drive C ? 

Udah nyambung ke database nya,tapi pas panggil Crystal report Kok Minta Login ? Aneh ? 

Sepertinya aku harus setting koneksi nya jadi dinamis.. Dan akhirnya aku nemu beberapa caranya,pas aku bikin postingan ini saya pakai database acces 2010 yang extensinya .accdb di visual basic .NET 2010,berikut yang beberapa koding yang saya terpakan,silakan agan coba aja dech koding mana saja yang menurut agan suka.

1. Cara 1

Imports System.Data.OleDb

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

--------------------------------------------------------------------------------------------

Public Class frm_report

Private Report1 As ReportDocument

--------------------------------------------------------------------------------------------  

Private Sub crView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles crView.Load

        Dim A As CrystalDecisions.CrystalReports.Engine.Table

        Dim B As CrystalDecisions.Shared.TableLogOnInfo

        Report1 = New ReportDocument()

        Report1.Load(Application.StartupPath & "\rpt_kasir.rpt")

        Report1.RecordSelectionFormula = "{v_kasir.no_fak}=('"&Trim(txtCari.Text)&"')"

        For Each A In Report1.Database.Tables 'preports.Database.Tables 

            B = A.LogOnInfo

            With B.ConnectionInfo

                .ServerName = Application.StartupPath & "\dbResto.mdb"

                .UserID = ""

                .Password = ""

                .DatabaseName = ""

            End With

            A.ApplyLogOnInfo(B)

        Next A

        Me.crView.ReportSource = Report1

    End Sub

End Class

2. Cara Ke -2

rpt = New ReportDocument

    rpt.Load(Application.StartupPath & "\Report\attendance.rpt")

    Dim crTableLogOnInfo As TableLogOnInfo

    Dim crConnectionInfo As ConnectionInfo

    Dim crDatabase As Database

    Dim crTables As Tables

    Dim crTable As Table

     crConnectionInfo = New ConnectionInfo

    With crConnectionInfo

        .ServerName = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" & Application.StartupPath & "\attend.accdb"

        .UserID = "Admin"

        .Password ="your password"

    End With

  'Get the table information from the report

    crDatabase = rpt.Database

    crTables = crDatabase.Tables

    'Loop through all tables in the report and apply the connection

    'information for each table.

    For Each crTable In crTables

        crTableLogOnInfo = crTable.LogOnInfo

        crTableLogOnInfo.ConnectionInfo = crConnectionInfo

        crTable.ApplyLogOnInfo(crTableLogOnInfo)

    Next

    R_Report_frmPreview.Show()

3. Cara Ke -3

Cara Ketiga Ini Yang saya terapkan di Aplikasi Saya.

Saya Buat Dulu Settingan Di Module1 Nya Supaya Tinggal Panggil di Form yang lain,Seperti Ini :

Imports System.Data.OleDb

Imports CrystalDecisions.CrystalReports.Engine

Imports CrystalDecisions.Shared

Module Module1 

Public cryRpt As New ReportDocument

Public crtableLogoninfos As New TableLogOnInfos

Public crtableLogoninfo As New TableLogOnInfo

Public crConnectionInfo As New ConnectionInfo

Public CrTables As Tables

Public Sub seting_laporan()

With crConnectionInfo

.ServerName = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=" & Application.StartupPath & "\db.accdb"

.UserID = ""

.Password = ""

.DatabaseName = ""

End With

        CrTables = cryRpt.Database.Tables

        For Each CrTable In CrTables

            crtableLogoninfo = CrTable.LogOnInfo

            crtableLogoninfo.ConnectionInfo = crConnectionInfo

            CrTable.ApplyLogOnInfo(crtableLogoninfo)

        Next

    End Sub 

End Module 

Terus Di Form berikutnya saya letakan kode di balik button,seperti ini : 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Cetak.Show()

Cetak.Focus()

Cetak.CRV.SelectionFormula = "{tblglobal.tanggal}=#" & MenuMaster.panel5.Text & "#"

cryRpt.Load(Application.StartupPath & "\Laporan Bulanan Global.rpt")

Call seting_laporan()

Cetak.CRV.ReportSource = cryRpt

Cetak.CRV.RefreshReport()

End Sub

Dan Akhir nya Aplikasi Laporan pun berjalan dengan baik dan lancar,walaupun saya pindah programnya dari komputer 1 ke komputer yang lainnya,tanpa ada error sedikitpun dan pastinya pas crystal report di panggil tidak minta login lagi.. :)

Terimkasih Sudah Berkunjung,Semoga Bermanfaat.... 

 Sumber : 1,2,3

1 KOMENTAR: