机房收费系统——报表(2)

时间:2022-12-20 15:20:30


    二、代码部分

跟之前写的excel相比,我认为报表还是有点同样的。

大体思路是这种:

  1. 先定义。

    1. (报表:Dim Withevents 对象变量 As 类名

    这样的使用方法是载入该类的事件。Withevents之后就能在代码编辑窗看到该类的事件了(假设有的话)。

    由于类不像控件,双击控件就能够进入事件编辑。

    使用WithEvents 变量时,应该注意以下这些限制条件:   

    1. WithEvents变量不能是派生对象变量。

      也就是说,不能把它声明为As  Object—当声明该变量时必须指定类名。    

    1. 不能把 WithEvents 变量声明为As  New

      必须明白地创建事件源对象,并将它赋给WithEvents变量。)

  1. 实例化报表。打开报表位置。
  1. 链接数据源。(因为报表里面已经连接了数据源,所以这句话我凝视后程序依旧能够正常的执行。在excel是写地址)
  1. 查询表并把表里面的内容显示出来

 以下看一下代码:

<span style="white-space:pre"><span style="color:#006600;"></span></span><span style="color:#006600;">'定义</span>
<pre name="code" class="plain" style="font-size: 19px;"> Dim WithEvents report As grproLibCtl.GridppReport <span style="font-family: 华文楷体; font-size: 14pt;">  </span><span style="font-family: 华文楷体; font-size: 14pt;"></span>


<span style="color:#006600;"> <span style="white-space:pre"></span>'载入一个新报表</span>
Set report = New grproLibCtl.GridppReport
<span style="color:#006600;">'载入报表模版文件</span>
report.LoadFromFile (App.Path & "\日报表.grf")
'设置数据连接串
<span style="color:#006600;"> 'report.DetailGrid.Recordset.ConnectionString = ConnectString
'链接数据源,因为在报表里面已经连接了。

</span>
txtsql = "select * from checkday_Info where date ='" & Date & "'"
report.DetailGrid.Recordset.QuerySQL = txtsql '运行
<span style="color:#006600;">'显示报表中的内容</span>
grddayBill.report = report
grddayBill.Start

<span style="color:#006600;">'报表模板上的4个日期或时间赋值</span>
report.ParameterByName("Date1").Value = Format(Date, "yyyy-MM-dd")
report.ParameterByName("Datetime").Value = Now
report.ParameterByName("username").Value = UserName


三、总结

所以非常多新的东西仅仅要理清好原理和思路,也没什么太难的。

还是特别感谢杨琛师哥的这两篇博客,巨具体。

机房收费系统之结账与报表(三)VB与报表代码《一学就会傻瓜版》

机房收费系统之结账与报表(二)周报表制作《一学就会傻瓜版》