1、 Dim cn As ADODB.Connection 定义一个ADO方式的数据库连接 Dim res As ADODB.Recordset 定义一个ADO方式的数据库记录集 Dim StrSQL As String Set cn = New ADODB.Connection 定义cn为新的ADO数据库连接 Set res = New ADODB.Recordset 定义res为新的ADO数据库连接集 On Error Resume Nextcn.ConnectionString = DSN=ReportSource;UID=;PWD=;定义cn的连接数据源为ReportSource 即ODB
2、C中建立的ACCESS的数据源名 cn.Open StrSQL = select * from ReportData Where 日期=# & Date & #使用SQL语句查找ReportData表中日期为Date的数据 res.Open StrSQL, cn, adOpenKeyset, adLockOptimistic res.AddNew 添加一个新的记录 res.Fields(0) = Date 在0列加入日期 res.Fields(1) = Hour(Time) 在1列加入时间res.Fields(2) = Fix32.RW2.RW_Y0GBN11AP001_ZS.f_cv 在2列
3、加入标签1res.Fields(3) = Fix32.RW2.RW_Y0GBN11AP002_ZS.f_cv 在3列加入标签2 res.Update 保存记录 Update(当Edit或AddNew方法完成后保存记录集) res.Close 关闭记录集cn.Close Set res = Nothing Set cn = NothingEnd Sub 在vb的菜单栏点击工具引用,添加一下控件:2.2添加到FixBackgroundServer 任务 打开SCU,点击configureTask 如图所示:在文件名内查找FixBackgroundServer.exe文件添加后保存SCU文件。2.3
4、在后台任务启动列表中添加调度点击settingsuser preferences进入如下图界面,添加后台启动项3同时在Report画面里,添加WEB控件:4 报表生成在IFIX画面上插入一个按钮。按钮名称:CommandButton1报表生成按钮代码如下:Private Sub CommandButton1_Click()Dim cn As ADODB.ConnectionDim res As ADODB.RecordsetDim xlApp As Excel.ApplicationDim xlBook As Excel.WorkbookDim xlsheet1 As Excel.Worksh
5、eetDim xlsheet2 As Excel.WorksheetDim strFileName As StringDim StrSQL As StringDim i As IntegerDim row As IntegerDim CreatDayReport As Boolean WebBrowser.Navigate E:空报表.htmstrFileName = ReportView.htm预先设计的报表显示模板文件,存放路径C:Documents and SettingsAdministrator桌面 实际看工程存放路径这里是测试StrSQL = select * from Repor
6、tData where 日期=# Calendar.Value &使用SQL语句查询Calendar日历上日期的数据If Dir(strFileName) = Then 判断文件是否存在,不存在则退出 MsgBox 报表模版文件不存在 Exit SubEnd IfSet cn = New ADODB.Connection 新建连接连接到ReportSource数据源cn.Open 打开连接Set res = New ADODB.Recordset 新建记录集res.Open StrSQL, cn, adOpenKeyset, adLockOptimisticIf res.RecordCount
7、 = 0 Then RecordCount 记录集中记录的数量。若无法判断记录条数则返回-1你要查询的数据不存在,可能已被删除, vbInformation + vbOKOnly, 系统提示 res.Close cn.Close Else res.MoveFirst 将记录指针移动到第一行 Set xlApp = New Excel.Application 新建Excel Set xlBook = xlApp.Workbooks.Open()Set xlBook = GetObject(strFileName) 打开模版XLS文件 Set xlsheet1 = xlBook.Worksheet
8、s(1) Set xlsheet2 = xlBook.Worksheets(2) xlBook.Worksheets.Application.Visible = False 设置第一张表为可视 xlsheet1.Range(b4, g27) = b4列g27行清空 xlsheet2.Range( xlsheet1.Cells(2, A) = CDate(res.Fields(0) 获取日期 CDate转换为幼小日期 xlsheet2.Cells(2, i = 0 While i res.RecordCount row = res.Fields(1) + 4 利用时间排序 xlsheet1.Ce
9、lls(row, b) = res.Fields(2)c) = res.Fields(3) i = i + 1 res.MoveNext Wend xlApp.DisplayAlerts = False xlBook.SaveAs 保存 以日报表.htm格式来保存报表 xlApp.DisplayAlerts = True 将日报表显示出来 xlApp.Quit Set xlsheet1 = Nothing Set xlsheet2 = Nothing Set xlBook = Nothing Set xlApp = Nothing CreatDayReport = True If CreatD
10、ayReport = True Thene: End If End Sub以上对于数据代码部分,添加的数据名称、数量要以本身项目所对数据库为基准。5保存报表在IFIX中插入一个按钮,命名为:CommandButton2保存报表按钮代码如下:Private Sub CommandButton2_Click()Dim FileName As StringOn Error GoTo errHandler Dim strReport As StringDim mydate As Datemydate = Date strReport = 日报表If Me.DayControl = True Then
11、strReport = If Me.MonthControl = True Then strReport = 月报表 Set xlApp = GetObject(, Excel.Application If Err.Number 0 Then Set xlApp = CreateObject( xlApp.ScreenUpdating = True Err.ClearSet xlApp = getobjece(, excel.applicationxlApp.ScreenUpdating = True xlApp.Visible = False dlgSaveAs.CancelError =
12、True dlgSaveAs.Flags = cdlOFNHideReadOnly dlgSaveAs.Filter = HTML 文件(*.HTM)|*.HTM|EXCEL 文件(*.xls)|*.xls dlgSaveAs.FileName = C:Documents and SettingsAdministrator桌面 Format(Calendar.Value, yyyy-MM-dd) & strReport &.xls FileName = .xlsxdlgSaveAs.Filename = strReport dlgSaveAs.FilterIndex = 2dlgSaveAs.
13、ShowSavexlBook.SaveAs Filename:=dlgSaveAs.Filename, FileFormat:=xlNormal, Password:=, WriteResPassword:, ReadOnlyRecommended:=False, CreateBackup:=False xlBook.SaveAs FileName xlApp.DisplayAlerts = True xlApp.Visible = TrueerrHandler:保存出错!End Sub6 报表日期查询功能添加打开IFIX报表画面,点击工具箱TOOLBOX,单击OLE,,选择日历控件:Cale
14、ndar Control 11.0;日历控件必须初始化,初始化代码如下:Private Sub CFixPicture_Initialize() 对日历控件初始化Calendar.Value = Date至此IFIX报表ODBC已经组态完成,若需要打印报表,则添加打印按钮:打印按钮代码如下:Private Sub cmdPrint_Click()日报页面设置 If Calendar.Value = True Then PrintSet 0.5 WebBrowser.ExecWB 6, OLECMDEXECOPT_PROMPTUSER打印页面设置代码如下:Private Sub PrintSet
15、(sBottom As String, strTop As String, sLeft As String, sMargin_right As String) Dim hkey_root, hkey_path, hkey_key As String Dim RegWsh As Object hkey_root = HKEY_CURRENT_USER hkey_path = SoftwareMicrosoftInternet ExplorerPageSetup/设置网页打印的页眉页脚为空 Set RegWsh = CreateObject(WScript.Shell hkey_key = hea
16、der页眉 RegWsh.RegWrite hkey_root + hkey_path + hkey_key, footer页脚margin_bottom下页边距 RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sBottom margin_top上页边距 RegWsh.RegWrite hkey_root + hkey_path + hkey_key, strTop margin_left 左页边距 RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sLeft margin_right 右页边距 RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sMargin_right 打印
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1