VB查询历史数据某一时间段内的最大值

现在要通过一个查询按钮查询一段时间内历史归档数据的最大值,并将最大值输出到画面中显示,我用VB脚本实现,脚本程序如下所示:
Sub OnClick(Byval Item)                                                    

Dim sPro,sDsn,sSer,sCon,sSql

Dim conn,oRs ,oCom,oList, oItem

Dim V,Max,tem

Dim m,n,s,nRec

Dim BiginTime,EndTime

Set BiginTime=HMIRuntime.tags("BeginTime")

Set EndTime =HMIRuntime.tags("EndTime")

sPro="Provider=WinCCOLEDBProvider.1;"

sDsn=" Catalog=CC_ART_10_04_30_23_55_43R;"

sSer ="Data Source=wangkou.\WinCC"

sCon= sPro+ sDsn+ sSer

sSql = "TAG:R,'DIANDU\1#KWH','2010-05-02 08:38:00.000','2010-05-02 08:42:00.000'"

Set conn=CreateObject("adodb.connection")

conn.connectionstring=sCon

conn.Cursorlocation=3

conn.open

Set oRs=CreateObject("adodb.recordset")

Set oCom=CreateObject("ADODB.Command")

oCom.CommandType=1

Set oCom.ActiveConnection=conn

oCom.CommandText=sSql

Set oRs=oCom.execute

n= oRs.RecordCount

If (n>0) Then

oRs.movefirst

n=0

Max= oRs.Fields(2).Value

Do While Not oRs.EOF

n=n+1

V= oRs.Fields(2).Value

If V>Max Then Max=V

oRs.MoveNext

Loop

oRs.Close

If(n>1) Then

Set tem=HMIRuntime.tags("tag0").read

    max.write tem

Else

HMIRuntime.Trace"selection return nofields" &vbNewLine

End If

End If

conn. Close
Set oRs=Nothing
Set oCom=Nothing
Set conn =Nothing

End Sub
现在的问题是在画面显示中没有数据显示。
说明:
查询的时间为:2010-05-02  16:38:00.000至2010-05-02 16:42:00.000'"
tag0为wincc画面中要查看的变量。

归档名称:diandu 归档数据为:1#kwh
有哪位大侠知道以上脚本问题出在哪儿呀?

最佳答案

1) 语句中有3个if,但只有两个end if
2) max.write tem应该是 tem.write max
3) 你可以加上msgbox看脚本执行到哪里停了,再逐个排查。
Set oRs=oCom.execute以下的语句改成:
n= oRs.RecordCount
max=0
If (n>0) Then
oRs.movefirst

Do While Not oRs.EOF

If oRs.Fields(2).Value>Max Then 
   Max=oRs.Fields(2).Value
end if
oRs.MoveNext
Loop
Else
HMIRuntime.Trace"selection return nofields" &vbNewLine
end if
oRs.Close
'dim tem
Set tem=HMIRuntime.tags("tag0").read
    tem.write max
oRs.Close
set tem=nothing
Set oRs=Nothing
Set oCom=Nothing
Set conn =Nothing
End Sub

提问者对于答案的评价:
非常谢谢你的帮忙,不过主要问题不是出在这儿,而是n没有数据输出,始终输出为-1,后来增加了一条指令,就可以了。

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc269406.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2019年6月11日
下一篇 2019年6月11日

相关推荐