设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主: 真主
打印 上一主题 下一主题

[报表] Access免费的完美报表的讲述帖(正式开讲)

[复制链接]

点击这里给我发消息

21#
 楼主| 发表于 2013-11-1 15:34:52 | 只看该作者

其实VB中需要写的代码也就一点点,我最初来想发布一个成品就行了,毕竟授人鱼比授人以渔要简单的多,但考虑到大家的应用,所以还是授渔吧,所以需要的朋友可以耐心看看
回复 支持 1 反对 0

使用道具 举报

点击这里给我发消息

22#
 楼主| 发表于 2013-11-1 18:59:45 | 只看该作者
本帖最后由 真主 于 2013-11-1 19:00 编辑

上次讲了VB调用报表的基本方法,可他无法实现动态调用,接下来我们要完成几个功能:
1、控件大小与窗体一样
2、在当前目录下建一个Reports文件夹,将报表文件统一放在里面,
3、在当前目录下建一个tool文件夹,将动态调用的报表名与报表数据源路径存入在tool文件夹中的print.txt文件夹中(后续在access打印时,动态向print.txt文件中写入,从而实现动态应用)
4、在窗体上新增两个Label控件,分别为Label1与Label2,另加一个框:Shape1
VB的最终代码如下:
  1. Dim WithEvents Report As grproLibCtl.GridppReport

  2. Private Sub Form_Load()

  3. Me.GRPrintViewer1.Width = Me.Width - Shape1.Width
  4. Me.GRPrintViewer1.Height = Me.Height - Shape1.Height

  5. Label1.Caption = ""
  6. Va_FullName1 = App.Path & "\tools\Print.txt"
  7. '====创建文件
  8. If Dir(Va_FullName1, vbDirectory) = "" Then
  9.     Set fs = CreateObject("Scripting.FileSystemObject")
  10.     Set D = fs.CreateTextFile(Va_FullName1, True)
  11. End If
  12. '====信息读取
  13. Dim Va_i1 As Integer

  14. '读取文本文件信息,获取Server信息
  15. '----------------------------------------------------
  16. Va_lngHandle1 = FreeFile(0)                   '获得文件的句柄
  17.    
  18. Open Va_FullName1 For Input As Va_lngHandle1     'For后面的参数表示以何种方式打开文件,Input是读取,Output是覆盖写入,Append是追加写入
  19. Do While Not EOF(Va_lngHandle1)              '循环直到文件尾
  20.     Line Input #Va_lngHandle1, strLine       '每次读取一行存放在strLine变量中
  21.     If Va_i1 = 0 Then Va_A1 = strLine
  22.     If Va_i1 = 1 Then Va_A2 = strLine
  23.     Va_i1 = Va_i1 + 1
  24. Loop                '显示得到的全部分内容
  25. Close Va_lngHandle1     '关闭文件
  26. '====Server信息读取
  27. Label1.Caption = Va_A1
  28. Label2.Caption = Va_A2

  29. '改为全路径
  30. If Mid(App.Path, Len(App.Path), 1) = "" Then
  31.     Label2.Caption = App.Path & "Reports" & Label2.Caption
  32. '    Label1.Caption = App.Path & Label1.Caption
  33. Else
  34.     Label2.Caption = App.Path & "\Reports" & Label2.Caption
  35. '    Label1.Caption = App.Path & "" & Label1.Caption
  36. End If


  37. '==========================================================
  38.     '创建报表对象
  39.     Set Report = New grproLibCtl.GridppReport

  40.     '载入报表模板文件,必须保证 Grid++Report 的安装目录在‘C:\Grid++Report 4.5’下,
  41.     '关于动态设置报表路径与数据绑定参数请参考其它例子程序
  42. '    Report.LoadFromFile ("C:\Grid++Report 5.0\Samples\Reports\1a.简单表格.grf")
  43. '    Report.DetailGrid.Recordset.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=C:\Grid++Report 5.0\Samples\Data\Northwind.mdb"
  44.     Report.LoadFromFile (Label2.Caption)
  45.     Report.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Label1.Caption & ";password="   '设定参数集合的数据源
  46.     Report.DetailGrid.Recordset.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Label1.Caption & ";password="  '设定明细网格的数据源
  47.     '设置报表查询显示器控件的关联报表对象
  48.     GRPrintViewer1.Report = Report

  49.     '启动报表运行
  50.     GRPrintViewer1.Start
  51.    
  52. End Sub

  53. Private Sub Form_Resize()
  54. Me.GRPrintViewer1.Width = Me.Width - Shape1.Width
  55. Me.GRPrintViewer1.Height = Me.Height - Shape1.Height
  56. End Sub
复制代码

点击这里给我发消息

23#
发表于 2013-11-11 11:04:36 | 只看该作者
楼主请继续,不要停
24#
发表于 2013-11-11 15:12:49 | 只看该作者
真主老师,能否给条炖好的鱼吃呀!即例子,谢谢
25#
发表于 2014-10-11 20:55:45 | 只看该作者
aaa
回复

使用道具 举报

26#
发表于 2015-1-11 14:21:57 | 只看该作者
呵呵呵呵呵呵呵呵哈哈哈

点击这里给我发消息

27#
发表于 2015-1-28 16:48:59 | 只看该作者
感谢楼主分享心得,友情再顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-7 13:42 , Processed in 0.095776 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表