IFIX报表
- 格式:doc
- 大小:964.50 KB
- 文档页数:7
报表控件应用-iFix第一部分数据存储[1] 运行iFix演示程序,添加数字量输出块(DO)DataLogTrig、DataLogEnable。
[2] 工作台开发环境中新建画面DataLog,右键点击选择编辑脚本添加程序:Fix32.Fix.DataLogEnabled.F_CV = 1[3] 菜单中选择插入〉OLE对象,对象类型列表中选择hmiReportDataLog.CtrlX插入到换面中,点击控件右键菜单中选择动画〉配置〉杂项,将控件的属性DataLogEnabled链接数据块DATALOGENABLED ,属性DataLogTrig 链接数据块 DATALOGTRIG,数据转换选择”对象”。
可将控件参数TagValue001- T agValue120链接模拟量数据块的实时值(Fix32.FIX.IFIX1_BATCH_BULKFLOW.F_CV)作为采样点,控件参数TagDesc001- T agDesc120链接模拟量数据块的描述信息(Fix32.FIX.IFIX1_BATCH_BULKFLOW.A_DESC),数据块的描述信息不能为空,不能以数字开头,不能包含运算符号。
[4] 点击控件右键菜单中选择属性,将控件属性DataLogInterval 设为1-7的整数值将控制控件的采样周期为5秒钟至1小时,设为8时停止自动采样。
画面中添加一个按钮[触发手动存储]按钮,按下时将数据块DATALOGTRIG 置1,释放时置0。
[5] 保存画面并切换至运行状态,选中[显示数据]可见设置的数据点名称和iFix传入的变量数据,按控件中[创建数据表]按钮,再按[存储数据]按钮,可在数据库中建立一个名为hmiDataLog的数据表并存储一行数据。
自动状态下控件按照选定的采样周期存储数据。
画面中按[触发手动存储]按钮触发一次数据存储,可实现由外部状态控制数据存储。
[6] 报表软件程序组中打开“数据检索”工具,选择数据表名hmiDataLog,可选字段中选择时标字段TSTAMP 和其它的数据点字段,按[数据检索]按钮可从数据库查询出已存入的历史数据。
在iFix中采用ODBC制作历史报表具体步骤如下:Step 1: 打开SCU中的Task Configuration,增加HTC.EXE和WSQLODC.EXE两个任务,并将它们设置成为后台运行状态;Step 2: 启动iFix,点击工具栏上的按钮打开Historical Assign,添加所需监控的数据点,并设置历史数据的采样速度。
Step 3: 打开任务控制,查看是否已经启动HTC。
Step 4: 在Globals\User下面添加三个字符型(vtString)变量,分别命名为strStartTime, strEndTime 和Interval。
新建一幅画面,如图如示,在画面上布置3个DATALINK分别指向strStartTime, strEndTime 和Interval,并设置成In place(即允许修改)。
strStartTimestrEndTimeIntervalStep 5:为画面上的各个按钮写代码,最好先将各按钮注册——即将鼠标移到按钮上方,按右键弹出菜单,选择Edit Script。
在VB Editor的工具菜单中的Reference(引用)中选中Microsoft Excel 9.0 Object Library和Microsoft ActiveX Data Objects,如下图所示。
然后在各按钮的处理程序中添加相应代码。
内容如下(可将下列代码直接COPY到你的VB编辑器中,覆盖因为注册产生的空代码):‘运行状态画面初始化Private Sub CFixPicture_Initialize()CommandButton1_ClickCommandButton2_Clickuser.Interval.CurrentValue = "00:00:30"End Sub‘组态状态画面初始化Private Sub CFixPicture_InitializeConfigure()user.strEndTime.CurrentV alue = "报表结束时间"user.strStartTime.CurrentValue = "报表开始时间"End Sub'设当前时间为报表开始时间Private Sub CommandButton1_Click()Dim curTime As StringcurTime = NowDim curmonth, curday, curhour, curminute, cursecond As Stringcurmonth = IIf(Month(curTime) < 10, "0" & Month(curTime), Month(curTime)) curday = IIf(Day(curTime) < 10, "0" & Day(curTime), Day(curTime))curhour = IIf(Hour(curTime) < 10, "0" & Hour(curTime), Hour(curTime))curminute = IIf(Minute(curTime) < 10, "0" & Minute(curTime), Minute(curTime)) cursecond = IIf(Second(curTime) < 10, "0" & Second(curTime), Second(curTime)) user.strStartTime.CurrentValue = Year(curTime) & "-" & curmonth & "-" & curday _& " " & curhour & ":" & curminute & ":" & cursecond End Sub'设当前时间为报表结束时间Private Sub CommandButton2_Click()Dim curTime As StringcurTime = NowDim curmonth, curday, curhour, curminute, cursecond As Stringcurmonth = IIf(Month(curTime) < 10, "0" & Month(curTime), Month(curTime))curday = IIf(Day(curTime) < 10, "0" & Day(curTime), Day(curTime))curhour = IIf(Hour(curTime) < 10, "0" & Hour(curTime), Hour(curTime))curminute = IIf(Minute(curTime) < 10, "0" & Minute(curTime), Minute(curTime)) cursecond = IIf(Second(curTime) < 10, "0" & Second(curTime), Second(curTime)) user.strEndTime.CurrentV alue = Year(curTime) & "-" & curmonth & "-" & curday & " " _& curhour & ":" & curminute & ":" & cursecondEnd Sub'打印此前10分钟历史报表,只是设置时间值,打印仍要调用打印程序Private Sub CommandButton3_Click()Dim curTime As StringcurTime = NowDim curmonth, curday, curhour, curminute, cursecond As Stringcurmonth = IIf(Month(curTime) < 10, "0" & Month(curTime), Month(curTime))curday = IIf(Day(curTime) < 10, "0" & Day(curTime), Day(curTime))curhour = IIf(Hour(curTime) < 10, "0" & Hour(curTime), Hour(curTime))curminute = IIf(Minute(DateAdd("n", -10, curTime)) < 10, "0" & _Minute(DateAdd("n", -10, curTime)), Minute(DateAdd("n", -10, curTime))) cursecond = IIf(Second(curTime) < 10, "0" & Second(curTime), Second(curTime)) user.strStartTime.CurrentValue = Year(curTime) & "-" & curmonth & "-" & curday & _" " & curhour & ":" & curminute & ":" & cursecond curminute = IIf(Minute(curTime) < 10, "0" & Minute(curTime), Minute(curTime)) user.strEndTime.CurrentV alue = Year(curTime) & "-" & curmonth & "-" & curday & _" " & curhour & ":" & curminute & ":" & cursecond End Sub'打印当天报表,只是设置时间值,打印仍要调用打印程序Private Sub CommandButton4_Click()Dim curTime As StringcurTime = NowDim curmonth, curday, curhour, curminute, cursecond As Stringcurmonth = IIf(Month(curTime) < 10, "0" & Month(curTime), Month(curTime))curday = IIf(Day(curTime) < 10, "0" & Day(curTime), Day(curTime))user.strStartTime.CurrentValue = Year(curTime) & "-" & curmonth & "-" & curday _& " " & "00:00:00"user.strEndTime.CurrentV alue = Year(curTime) & "-" & curmonth & "-" & curday _& " " & "23:59:59"End Sub'打印当月报表,只是设置时间值,打印仍要调用打印程序Private Sub CommandButton5_Click()Dim curTime As StringcurTime = NowDim curmonth, BeginofMonth, EndofMonth As Stringcurmonth = IIf(Month(curTime) < 10, "0" & Month(curTime), Month(curTime)) BeginofMonth = Year(curTime) & "-" & curmonth & "-01" & " " & "00:00:00"user.strStartTime.CurrentValue = BeginofMonthEndofMonth = Day(DateAdd("s", -1, DateAdd("m", 1, CDate(BeginofMonth))))user.strEndTime.CurrentV alue = Year(curTime) & "-" & curmonth & "-" _& EndofMonth & " " & "23:59:59"End Sub‘根据输入条件,保存/打印报表到文件Private Sub CommandButton6_Click()'运行EXCEL,打开报表模板文件Dim msexcel As Excel.ApplicationSet msexcel = CreateObject("Excel.Application")With msexcel.Visible = True ‘如为FALSE,则不显示EXCEL。
Private Sub CFixPicture_Initialize()dtpTime.Value = NowEnd SubPrivate Sub CFixPicture_KeyDown(ByVal KeyCode As Long, ByVal Shift As Long, ContinueProcessing As Boolean)End SubPrivate Sub CommandButton1_Click()Dim strDate As StringDim strMonth As StringstrDate = Format(dtpTime, "yyyy-MM-dd")strMonth = Format(dtpTime, "yyyy-MM")'日报If Me.optMonth.Value = False ThenIf Me.optDay.Value = True ThenWebReport.Navigate "D:\REPORT\日报表\日报表" & strDate & ".htm" '显示ElseWebReport.Navigate "D:\REPORT\日报表\空白报表" & strDate & ".htm" '显示End IfEnd If'月报If Me.optDay.Value = False ThenIf Me.optMonth.Value = True ThenWebReport.Navigate "D:\REPORT\月报表\月报表" & strMonth & ".htm" '显示ElseWebReport.Navigate "D:\REPORT\月报表\空白报表" & strMonth & ".htm" '显示End IfEnd IfEnd SubPrivate Sub CommandButton2_Click()On Error Resume Next'日报页面设置If Me.optDay.Value = True ThenPrintSet "0.5", "0.5", "0.5", "0.5"End If'月报页面设置If Me.optMonth.Value = True ThenPrintSet "0.5", "0.5", "0.5", "0.5"End IfMe.WebReport.ExecWB 6, OLECMDEXECOPT_PROMPTUSEREnd SubPrivate Sub optDay_Click()Me.optDay.Value = True ' 日报Me.optMonth.Value = False '月报Me.dtpTime.CustomFormat = "yyyy-MM-dd" '修改时间格式dtpTime.Value = NowEnd SubPrivate Sub optMonth_Click()Me.optDay.Value = False ' 日报Me.optMonth.Value = True '月报Me.dtpTime.CustomFormat = "yyyy-MM" '修改时间格式dtpTime.Value = NowEnd Sub打印设置Private Sub PrintSet(sBottom As String, strTop As String, sLeft As String, sMargin_right As String)Dim hkey_root, hkey_path, hkey_key As StringDim RegWsh As Objecthkey_root = "HKEY_CURRENT_USER"hkey_path = "\Software\Microsoft\Internet Explorer\PageSetup"'//设置网页打印的页眉页脚为空Set RegWsh = CreateObject("WScript.Shell")hkey_key = "\header" '页眉RegWsh.RegWrite hkey_root + hkey_path + hkey_key, ""hkey_key = "\footer" '页脚RegWsh.RegWrite hkey_root + hkey_path + hkey_key, ""hkey_key = "\margin_bottom" '下页边距RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sBottom 'hkey_key = "\margin_top" '上页边距RegWsh.RegWrite hkey_root + hkey_path + hkey_key, strTop 'hkey_key = "\margin_left" ' 左页边距RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sLeft 'hkey_key = "\margin_right" ' 右页边距RegWsh.RegWrite hkey_root + hkey_path + hkey_key, sMargin_right ''打印End Sub其中日报和月报为控件报表时间为控件WebReport控件新建调度ReportPrivate Sub report_OnTimeOut(ByVal lTimerId As Long)On Error Resume NextDim strDateTime As StringDim strDate As StringDim strHourMinute As StringDim iRow As IntegerDim strTemplateName As StringstrDateTime = Format(Now(), "yyyy-MM-dd hh:mm:ss")strDate = Left(strDateTime, 10)strHourMinute = Right(Left(strDateTime, 13), 2) & ":00"iRow = Right(Left(strDateTime, 13), 2)Dim isCreate As BooleanSet fs = CreateObject("Scripting.FileSystemObject")isCreate = fs.FileExists("D:\REPORT\日报表\日报表" & strDate & ".htm") Set fs = NothingIf isCreate ThenstrTemplateName = ("D:\REPORT\日报表\日报表" & strDate & ".htm") ElsestrTemplateName = "D:\REPORT\日报表" & ".htm"End IfSet msExcel = CreateObject("Excel.Application")With msExcel.visible = True.Workbooks.Open strTemplateName, , False.ActiveWorkbook.ActiveSheet.Select.DisplayAlerts = False.Wait (Now() + 0.00002)End With'写日期msExcel.Worksheets(1).Cells(1, 8).Value = strDate'写数据msExcel.Worksheets(1).Cells(iRow + 6, 2).Value = readvalue("DAYJSLJ1")msExcel.Worksheets(1).Cells(iRow + 6, 4).Value = readvalue("DAYJSLJ2")msExcel.Worksheets(1).Cells(iRow + 6, 6).Value = readvalue("DAYJSLJ3")msExcel.Worksheets(1).Cells(iRow + 6, 8).Value = readvalue("DAYJSLJ4")msExcel.Worksheets(1).Cells(iRow + 6, 10).Value = readvalue("DAYJSLJ1") + readvalue("DAYJSLJ2") + readvalue("DAYJSLJ3") + readvalue("DAYJSLJ4")'保存报表文件Dim strSaveName As StringstrSaveName = "D:\REPORT\日报表\日报表" & strDate & ".htm"msExcel.ActiveWorkbook.SaveAs ("D:\REPORT\日报表\日报表" & strDate & ".htm") msExcel.QuitEnd SubReport1Private Sub report1_OnTimeOut(ByVal lTimerId As Long)On Error Resume NextDim strDateTime As StringDim strMonth As StringDim strHourMinute As StringDim iRow As IntegerDim strTemplateName As StringstrDateTime = Format(Now(), "yyyy-MM-dd hh:mm:ss")strMonth = Left(strDateTime, 7)strHourMinute = Right(Left(strDateTime, 13), 2) & ":00"iRow = Right(Left(strDateTime, 10), 2)Dim isCreate As BooleanSet fs = CreateObject("Scripting.FileSystemObject")isCreate = fs.FileExists("D:\REPORT\月报表\月报表" & strMonth & ".htm")Set fs = NothingIf isCreate ThenstrTemplateName = ("D:\REPORT\月报表\月报表" & strMonth & ".htm")ElsestrTemplateName = "D:\REPORT\月报表" & ".htm"End IfSet msExcel = CreateObject("Excel.Application")With msExcel.visible = False.Workbooks.Open strTemplateName, , False.ActiveWorkbook.ActiveSheet.Select.DisplayAlerts = False.Wait (Now() + 0.00002)End With'写日期msExcel.Worksheets(1).Cells(1, 8).Value = strMonth'写数据msExcel.Worksheets(1).Cells(iRow + 2, 2).Value = readvalue("DAYJSLJ1")msExcel.Worksheets(1).Cells(iRow + 2, 4).Value = readvalue("DAYJSLJ2")msExcel.Worksheets(1).Cells(iRow + 2, 6).Value = readvalue("DAYJSLJ3")msExcel.Worksheets(1).Cells(iRow + 2, 8).Value = readvalue("DAYJSLJ4")msExcel.Worksheets(1).Cells(iRow + 2, 10).Value = readvalue("DAYJSLJ4") + readvalue("DAYJSLJ1") + readvalue("DAYJSLJ2") + readvalue("DAYJSLJ3")Dim strtemp As Integerstrtemp = 3msExcel.Worksheets(1).Cells(34, 2).Value = 0msExcel.Worksheets(1).Cells(34, 4).Value = 0msExcel.Worksheets(1).Cells(34, 6).Value = 0msExcel.Worksheets(1).Cells(34, 8).Value = 0msExcel.Worksheets(1).Cells(34, 10).Value = 0Do While strtemp <= 33msExcel.Worksheets(1).Cells(34, 2).Value = msExcel.Worksheets(1).Cells(34, 2).Value + msExcel.Worksheets(1).Cells(strtemp, 2).ValuemsExcel.Worksheets(1).Cells(34, 4).Value = msExcel.Worksheets(1).Cells(34, 4).Value + msExcel.Worksheets(1).Cells(strtemp, 4).ValuemsExcel.Worksheets(1).Cells(34, 6).Value = msExcel.Worksheets(1).Cells(34, 6).Value + msExcel.Worksheets(1).Cells(strtemp, 6).ValuemsExcel.Worksheets(1).Cells(34, 8).Value = msExcel.Worksheets(1).Cells(34, 8).Value + msExcel.Worksheets(1).Cells(strtemp, 8).ValuemsExcel.Worksheets(1).Cells(34, 10).Value = msExcel.Worksheets(1).Cells(34, 10).Value + msExcel.Worksheets(1).Cells(strtemp, 10).Valuestrtemp = strtemp + 1Loop'保存报表文件Dim strSaveName As StringstrSaveName = "D:\REPORT\月报表\月报表" & strMonth & ".htm"msExcel.ActiveWorkbook.SaveAs ("D:\REPORT\月报表\月报表" & strMonth & ".htm") msExcel.QuitEnd SubResetPrivate Sub RESET_OnTimeOut(ByVal lTimerId As Long) Dim PauseTimePauseTime = 1Start = TimerCloseDigitalPoint "Fix32.FIX.RESET_DAYJSLJ.F_CV" Do While Timer < Start + PauseTimeDoEventsLoopOpenDigitalPoint "Fix32.FIX.RESET_DAYJSLJ.F_CV" End Sub。
有关iFIX软件报表生成的说明 iFIX软件可以通过内置的VBA借助EXCEL,ACCESS,Crystal Roport等软件生成报表。
本文通过 VBA+ADO+EXCEL 做一个具体的介绍。
1.V BA(Visual Basic for Application)VBA是iFIX完全内置的一个强有力的编程工具,可以使用户快速方便的生成自动的操作任务和过程自动化的解决方案。
IFIX的VBA工具包括以下特性:可操作所有列出的iFIX对象的属性,方法和事件支持多种数据源,包括iFIX过程数据库,任何OPC服务器,任何对象的属性和SQL数据库ODBC支持ActiveX控件支持VBA脚本生成向导及IFIX命令可以帮助你为常用任务自动生成程序代码第三方控件的安全容器下面的程序代码就是在iFIX中建立一个新的EXCEL应用程序并打开报表模版。
Dim msexcel As Excel.ApplicationSet msexcel = CreateObject("Excel.Application")With msexcel.Visible = True.Workbooks.Open "e:\histdemo\FixReports.xls", ,False.ActiveWorkbook.ActiveSheet.Select.DisplayAlerts = False.Wait (Now() + 0.00002)End With2.A DO (ActiveX Data Object)ADO 是微软推出的最新的数据库访问方式。
它综合了微软早期推出的DAO及RDO的特性并取代了DAO及RDO。
ADO可以访问更多类型的数据库,并且可以通过ODBC(Open DataBase Connectivity)访问数据,但它的结构是建立在OLE DB Providers上。
IFIX软件安装完毕以后,在控制面板的ODBC数据源中会增加两个系统DSN:FIX Dynamics Historical Data iFIX历史数据源FIX Dynamics Real Time Data iFIX实时数据源ADO可以通过访问这两个数据源来直接操作iFIX历史数据库和实时数据库。
第一部分数据存储[1] 运行iFix演示程序,添加数字量输出块(DO)DataLogTrig、DataLogEnable。
[2] 工作台开发环境中新建画面DataLog,右键点击选择编辑脚本添加程序:Fix32.Fix.DataLogEnabled.F_CV = 1[3] 菜单中选择插入〉OLE对象,对象类型列表中选择hmiReportDataLog.CtrlX插入到换面中,点击控件右键菜单中选择动画〉配置〉杂项,将控件的属性DataLogEnabled链接数据块 DATALOGENABLED ,属性DataLogTrig 链接数据块 DATALOGTRIG,数据转换选择”对象”。
可将控件参数TagValue001- TagValue120链接模拟量数据块的实时值(Fix32.FIX.IFIX1_BATCH_BULKFLOW.F_CV)作为采样点,控件参数TagDesc001- TagDesc120链接模拟量数据块的描述信息(Fix32.FIX.IFIX1_BATCH_BULKFLOW.A_DESC),数据块的描述信息不能为空,不能以数字开头,不能包含运算符号。
[4] 点击控件右键菜单中选择属性,将控件属性DataLogInterval设为1-7的整数值将控制控件的采样周期为5秒钟至1小时,设为8时停止自动采样。
画面中添加一个按钮[触发手动存储]按钮,按下时将数据块DATALOGTRIG 置1,释放时置0。
[5] 保存画面并切换至运行状态,选中[显示数据]可见设置的数据点名称和iFix传入的变量数据,按控件中[创建数据表]按钮,再按[存储数据]按钮,可在数据库中建立一个名为hmiDataLog的数据表并存储一行数据。
自动状态下控件按照选定的采样周期存储数据。
画面中按[触发手动存储]按钮触发一次数据存储,可实现由外部状态控制数据存储。
[6] 报表软件程序组中打开“数据检索”工具,选择数据表名hmiDataLog,可选字段中选择时标字段TSTAMP 和其它的数据点字段,按[数据检索]按钮可从数据库查询出已存入的历史数据。
Ifix报表的实现本文档主要介绍Ifix 通过调度将报表数据按照一定要求存储到Access数据库中,并根据要求从其中提取数据,结合EXCELL的制表功能实现报表。
数据存入1.数据库的建立和数据关联方法:(1)直接在Microsoft office中建立Access文件。
(2)在控制面板里ODBC数据源管理器里关联数据库时创建Access数据库。
1.1数据库的建立(1)打开新建的Access文件,点击。
(2)创建一个,名字为ReportData的表。
在表名处右键鼠标点击Ifix时间调度触发调度中的VB脚本Access数据库报表显示创建表1.2数据库的关联用关联定义画面如下图。
在其中选择点击选择Microsoft Access Driver[*.mdb],再点击完成按钮,进入下边的窗口,在其中的数据源名(N)DSN (数据源名)名,不使用数据名。
2.时间调度的建立2.1在ifix里新建基于时间的调度,在Schedules新建一个调度文件,在Time Based Entries中创建一个调度,具体设置如下图:注意:iFIX 不支持使用与本地节点名相同名称的调度。
因此,不能创建与本地节点名相同的调度。
触发信息设置为continuous,间隔为1个小时,启动时间为第2分钟。
点击Vb Editor 将以下脚本写入Private Sub RW2REPORT_OnTimeOut(ByVal lTimerId As Long)Dim cn As ADODB.Connection '定义一个ADO方式的数据库连接Dim res As ADODB.Recordset '定义一个ADO方式的数据库记录集Dim StrSQL As StringSet cn = New ADODB.Connection '定义cn为新的ADO数据库连接Set res = New ADODB.Recordset '定义res为新的ADO数据库连接集cn.ConnectionString = "DSN=ReportSource;UID=;PWD=;" '定义cn的连接数据源为ReportSource 即ODBC中建立的ACCESS的数据源名cn.OpenStrSQL = "select * from ReportData Where 日期=#" & Date & "#" '使用SQL语句查找ReportData表中日期为Date的数据res.Open StrSQL, cn, adOpenKeyset, adLockOptimisticres.AddNew '添加一个新的记录res.Fields(0) = Date '在0列加入日期res.Fields(1) = Hour(Time) '在1列加入时间res.Fields(2) = Fix32.RW2.RW_Y0GBN11AP001_ZS.f_cv '在2列加入标签1res.Fields(3) = Fix32.RW2.RW_Y0GBN11AP002_ZS.f_cv '在3列加入标签2res.Update '保存记录Update(当Edit或AddNew方法完成后保存记录集) res.Close '关闭记录集cn.CloseSet res = NothingSet cn = NothingEnd Sub在vb2.2添加到FixBackgroundServer 任务打开SCU,点击如图所示:在文件名内查找FixBackgroundServer.exe文件添加后保存SCU文件。
IFIX报表
IFIX报表常规的做法是采用IFIX内嵌的VBA语言,借助于OFFICE自带的ACCESS 和EXCEL 来做的。
例如我们要在IFIX画面上通过点击一按钮来察看某一天的数据,假如这有三个模拟量如tag1,tag2,tag3,数据间隔一小时归一次档,它主要分为四步:
一、创建一个ACCESS数据库如ReportDataBase.mdb,用来存放报表所需的数据。
然
后就在这个数据库中建立一个表如ReporData,这样IFIX 就能以一定的时间间隔将数据写入此数据库刚才创建的表中。
ReportData的结构如下:
字段名类型
日期日期/时间
tag1 数值
tag2 数值
tag3 数值
二、创建ODBC数据源,用于IFIX与上步已建立的数据库和表进行联系。
方法:控制面
板->管理工具->ODBC数据源,打开ODBC数据源管理器,建立一个“系统DSN”,点击“添加”,双击“Microsoft Access Drivers (*.mdb)”,出现“ODBC Microsoft Access 安装”对话框。
数据源名:MyReport 注:此名是任意的字符组合,但不能与数据库重名。
数据库:ReportDataBase.mdb,点击‘选择‘按钮选择第一步创建的Access数据库。
点击“完成”,退出控制面板
三、在IFIX 中创建一个“基于时间的调度”,如图
名称:任意合法的字符组合
触发信息:连续
间隔:1小时
详细代码如下:
点击“VB编辑器“进入VB编辑器
在“工具”菜单-〉引用“Microsoft ActiveX Data Objects x.x Library”
Private Sub FixTimer3_OnTimeOut(ByVal lTimerId As Long)
Dim cn As ADODB.Connection
Dim res As ADODB.Recordset
Dim StrSQL As String
Set cn = New ADODB.Connection
Set res = New ADODB.Recordset
cn.ConnectionString = "DSN= MyReport;UID=;PWD=;"
//MyReport是数据源名称//
cn.Open
StrSQL = "select * from ReportData where 日期=#" & Date & "#"
//ReportData是建立数据库中的表名//
res.Open StrSQL, cn, adOpenKeyset, adLockOptimistic
res.AddNew
res.Fields(0) = Date
res.Fields(1) = Fix32.Fix.tag1.f_cv
res.Fields(2) = Fix32.Fix.tag2.f_cv
res.Fields(3) = Fix32.Fix.tag3.f_cv
res.Update
res.Close
Set res = Nothing
Set cn = Nothing
End Sub
另外,如果定义了主键,如上图,选择了,则res.Fields(0)不可用。
四、在IFIX画面上插入一个按钮。
按钮名称:Cmdreport
Private Sub Cmdreport_Click()
UserForm2.show
End Sub
再在”VB编辑器环境“下,插入一用户窗体。
并且在工具-〉附加工具中选择日历控件11.0
在“工具”菜单-〉引用“Microsoft ActiveX Data Objects 2.7 Library”
“Microsoft Excel 9.0 Library”
“取消”按钮的代码
Private Sub Cmdcancel_Click()
Unload Me
End Sub
“确定”按钮的代码如下:
Private Sub CmdOK_Click()
'On Error Resume Next
Dim cn As ADODB.Connection
Dim res As ADODB.Recordset
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim strFileName As String
Dim StrSQL As String
Dim i As Integer
Dim row As Integer
strFileName= "c:\report.xls" ‘预先设计的报表显示模板文件
StrSQL = "select * from ReportData where 日期=#" & Calendar1.Value& "#" If Dir(strFileName) = "" Then '判断文件是否存在,不存在则退出
MsgBox "报表模版文件不存在"
Exit Sub
End If
Set cn = New ADODB.Connection
cn.ConnectionString = " DSN=MyReport;UID=;PWD=;"
cn.Open
Set res = New ADODB.Recordset
res.Open StrSQL, cn, adOpenKeyset, adLockOptimistic
If res.RecordCount <= 0 Then
MsgBox "你要查询的数据不存在,可能已被删除", vbInformation + vbOKOnly, "系统提示"
res.Close
Set res = Nothing
cn.Close
Set cn = Nothing
Exit Sub
Else
res.MoveFirst
Set xlBook = GetObject(strFileName)
Set xlsheet = xlBook.Worksheets(1)
xlBook.Application.Visible = True
xlsheet.Cells(2, "n") = CDate(res.Fields(0))
i=0
While i < res.RecordCount
Row=i+4
xlsheet.Cells(row, "a") = res.Fields(1)
xlsheet.Cells(row, "b") = res.Fields(2)
xlsheet.Cells(row, "c") = res.Fields(3)
i = i + 1
res.MoveNext
Wend
xlsheet.PrintPreview True
xlBook.Application.DisplayAlerts = False
xlBook.Application.Quit
End If
End Sub。