iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 28
1
自我挑戰組

Access VBA 之 iT管理實做系列 第 28

Access VBA 之 iT管理實做Day28: 顯示Crystal Report(水晶報表)

Access雖然有內建報表功能,但Crystal Report的功能似乎更略勝一籌,安裝好Crystal Report後,可以由Access的表單內加入「Crystal Report Viewer Control」物件,再透過VBA程式,即可帶出指定的報表,修改查詢內容,達到要呈現的效果。相關顯示的程式碼如下:

Private Sub cmd_Open_Click()

    '來源報表檔
    strFile = Lista2.Column(2)
    '來源資料庫
    strDB = Lista2.Column(4)
    'strMDB = CurrentProject.FullName
    '來源MDB檔案
    strMDB = Config("MDBPath") & "\" & "xxxx.mdb"
    '設定密碼
    strPassword = "xxxxxx"

    '報表檔案路徑
    strReportPath = "C:\Reportes\"
    
    '報表名稱
    sReportName = strReportPath & strFile & ".rpt"
    
    Set objCRApp = CreateObject("CrystalRunTime.Application")
    Set objReport = objCRApp.OpenReport(sReportName)
    Set reportDb = objReport.Database
    Set reportTables = reportDb.Tables
    Set reportTable = reportTables.Item(1)

    'reportTable.ConnectBufferString = strConnectBufferString
    '資料庫來源指到MDB檔案
    reportTable.ConnectionProperties("Database Name") = strMDB
    'MDB密碼
    reportTable.ConnectionProperties("Database Password") = strPassword
    
    '變更SQL語句,可進行篩選內容調整
    strSQL = objReport.SQLQueryString
    strSQL = Replace(strSQL, 尋找字串, 替換字串)
    objReport.SQLQueryString = strSQL
    Debug.Print strSQL
    '來源
    CRViewer90.ReportSource = objReport
    'CR報表樣貌
    CRViewer90.EnableExportButton = True
    CRViewer90.EnableGroupTree = True
    CRViewer90.EnableRefreshButton = True
    CRViewer90.EnableZoomControl = True
    CRViewer90.EnableHelpButton = True
    '進行檢視
    CRViewer90.ViewReport
    
    '看是否顯示群組樹狀圖來決定顯示寬度
    If CRViewer90.EnableGroupTree = True Then
        CRViewer90.Width = 15390
    Else
        CRViewer90.Width = 12764
    End If
    
    'CRViewer90.Height = 10000
    
End Sub

筆者拿來跟樹狀圖配合,替現有的水晶報表製作了產生報表用的操作界面,不過這是公司內部使用,僅呈現給各位參考,報表的呼叫,可以參考程式碼的部份,產生出來的畫面如下:
https://ithelp.ithome.com.tw/upload/images/20181112/20007221sLrRgbGL94.png

另外筆者也製作了一個簡要的水晶報表來顯示工作紀錄資料,用來展示水晶報表的載入方式:
https://ithelp.ithome.com.tw/upload/images/20181112/20007221hziCx7FnTO.png


上一篇
Access VBA 之 iT管理實做Day27: 顯示與編輯PDF檔案
下一篇
Access VBA 之 iT管理實做Day29: XML資料的收集
系列文
Access VBA 之 iT管理實做30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言