拖了好久,终于有机会写上这篇博客了——折腾我许久的VB报表问题。实际上,在十一假期之前基本上都做完了,后来因为自考的关系,才一直没有时间来完善(自己给自己的借口!)那时候作出的报表,只能返回周结账单中所有记录,而不能返回指定日期的记录。上次头脑风暴的时候,偶然间听说了一个属性,这才完完整整的解决这个问题:返回任意指定日期的结账记录!
第一步:下载Grid Report ,然后安装。(这是下载地址:Grid Report 下载地址)
第二步:添加引用和部件(部件 :和 引用:
)
第三步:在VB窗体上添加GRDisplayViewer控件
第四步:编写代码:第一次犯错时的代码:(其中错误是不能解决指定日期的记录检索不出来)
Option Explicit
Dim Report As grproLibCtl.GridppReport
Dim Reportheader As IGRReportHeader
Dim StaticBox As IGRStaticBox
Private Sub Rshow()
Dim strSQL As String
Dim strMsg As String
strSQL = "select * from checkday_info where date>='" & Format$(cmbBeginDate.Text, "yyyy-mm-dd") & "' and date<='" & Format$(cmbEndDate.Text, "yyyy-mm-dd") & "'"
Set Report = New grproLibCtl.GridppReport
Report.LoadFromFile (App.Path & "\checkWeek.grf") '加载模版
Report.DetailGrid.Recordset.ConnectionString = "filedsn=charge.dsn;UID=sa;PWD=123456" '数据源
Report.DetailGrid.Recordset.QuerySQL = strSQL '通过SELECT查询创建记录集
'=================报表中自定义控件================
Set Reportheader = Report.InsertReportHeader
'插入一个静态文本框,显示需要显示的文字
Set StaticBox = Reportheader.Controls.Add(grctStaticBox).AsStaticBox
StaticBox.Text = cmbBeginDate.Text & " 至 " & cmbEndDate.Text
StaticBox.Width = 500
'============================
GRDisplayViewer1.Report = Report
GRDisplayViewer1.Start
End Sub
Private Sub cmdcmdPrintRefresh_Click()
StaticBox.Text = cmbBeginDate.Text & " 至 " & cmbEndDate.Text
GRDisplayViewer1.Refresh '刷新
End Sub
Private Sub cmdPrint_Click()
Report.[Print] (True) '打印
End Sub
Private Sub cmdViewPrint_Click()
Report.PrintPreview (True) '打印预览
End Sub
Private Sub cmbBeginDate_DropDown()
MonthView1.Visible = True
End Sub
Private Sub cmbEndDate_DropDown()
MonthView2.Visible = True
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
cmbBeginDate.Text = MonthView1.Year & "-" & MonthView1.Month & "-" & MonthView1.Day
MonthView1.Visible = False
End Sub
Private Sub MonthView2_DateClick(ByVal DateClicked As Date)
cmbEndDate.Text = MonthView2.Year & "-" & MonthView2.Month & "-" & MonthView2.Day
MonthView2.Visible = False
End Sub
Private Sub Form_Load()
MonthView1.Visible = False
MonthView2.Visible = False
' cmbBeginDate.Text = Format$("2012-08-08", "YYYY-MM-DD")
' cmbEndDate.Text = Format$(Date, "YYYY-MM-DD")
Me.Width = 15000
Me.Height = 13000
Call Rshow
End Sub
第二次,是在听同学说了一个stop 属性的时候,自己重新试了一下,结果相对满意,能够实现返回指定日期的源码:
Option Explicit
Dim Report As grproLibCtl.GridppReport
Dim Reportheader As IGRReportHeader
Dim StaticBox As IGRStaticBox
Private Sub Rshow()
Dim strSQL As String
Dim strMsg As String
strSQL = "select * from checkday_info where date>='" & Format$(cmbBeginDate.Text, "yyyy-mm-dd") & "' and date<='" & Format$(cmbEndDate.Text, "yyyy-mm-dd") & "'"
Set Report = New grproLibCtl.GridppReport
Report.LoadFromFile (App.Path & "\checkWeek.grf") '加载模版
Report.DetailGrid.Recordset.ConnectionString = "filedsn=charge.dsn;UID=sa;PWD=123456" '数据源
Report.DetailGrid.Recordset.QuerySQL = strSQL '通过SELECT查询创建记录集
'=================报表中自定义控件================
Set Reportheader = Report.InsertReportHeader
'插入一个静态文本框,显示需要显示的文字
Set StaticBox = Reportheader.Controls.Add(grctStaticBox).AsStaticBox
StaticBox.Text = cmbBeginDate.Text & " 至 " & cmbEndDate.Text
StaticBox.Width = 500
'============================
GRDisplayViewer1.Report = Report
GRDisplayViewer1.Start
End Sub
Private Sub cmdcmdPrintRefresh_Click()
GRDisplayViewer1.Stop
StaticBox.Text = cmbBeginDate.Text & " 至 " & cmbEndDate.Text
Call Rshow
End Sub
Private Sub cmdPrint_Click()
Report.[Print] (True) '打印
End Sub
Private Sub cmdViewPrint_Click()
Report.PrintPreview (True) '打印预览
End Sub
Private Sub cmbBeginDate_DropDown()
MonthView1.Visible = True
End Sub
Private Sub cmbEndDate_DropDown()
MonthView2.Visible = True
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
cmbBeginDate.Text = MonthView1.Year & "-" & MonthView1.Month & "-" & MonthView1.Day
MonthView1.Visible = False
End Sub
Private Sub MonthView2_DateClick(ByVal DateClicked As Date)
cmbEndDate.Text = MonthView2.Year & "-" & MonthView2.Month & "-" & MonthView2.Day
MonthView2.Visible = False
End Sub
Private Sub Form_Load()
MonthView1.Visible = False
MonthView2.Visible = False
cmbBeginDate.Text = Format$("2012-08-08", "YYYY-MM-DD")
cmbEndDate.Text = Format$(Date, "YYYY-MM-DD")
Me.Width = 15000
Me.Height = 13000
Call Rshow
End Sub
可以对比出来,那个区别是在
cmdcmdPrintRefresh_Click()事件下,先将报表给停止,然后重新调用声明的子过程。
如果不停止的话,就会报错:
总结一下:偶然听别人说的方法,自己可以试一下!还有就是,有了思路就要及时的写,不然就会导致再想写的时候,思维的短路,我写这篇博客就是又看了好久我前面的代码才写出来的!