要在画面中获得归档数据库中的数据,一共有四个I/O域,1号用于输入开始查询的时间,2号用于输入查询结束的时间,3号用于输入归档周期,这三个I/O域的输入格式都要参考wincc6.0专用归档命令所要求格式,4号用于输出查询结果。
我编写的代码如下,编译没什么错误,但就是到了用MsgBox测试的那一句,就是没有结果,也不像在上面的其他地方有对话框弹出,能不能请高手看一下,多谢!!!!如果要改的话,要改哪一点呢???总觉得那个RecordSetObj.Fields().Value这里有点问题,因为不知道要用哪个字段!!!!!
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim starttime
Dim endtime
Dim periodtime
Dim ConnectionObj
Dim Connectionstring
Dim CommandStr_TimeStart
Dim CommandStr_TimeEnd
Dim CommandObj
Dim RecordSetObj
Dim resultt
Dim temp1
Dim temp2
Set resultt=HMIRuntime.Tags("Result")
starttime=HMIRuntime.Tags("TimeStart").Read
endtime=HMIRuntime.Tags("TimeEnd").Read
periodtime=HMIRuntime.Tags("TimePeriod").Read
'a=ScreenItems("shurushuchuyu1").OutputValue
'b=ScreenItems("shurushuchuyu2").OutputValue
'c=ScreenItems("shurushuchuyu3").OutputValue
Connectionstring="Provider=WinCCOLEDBProvider.1;Catalog=CC_sdsd_09_01_05_09_38_35R;Data
Source=.\WinCC"
CommandStr_TimeStart="Tag:R,'ProcessValueArchive\NewTag',"
CommandStr_TimeStart=CommandStr_TimeStart+"'"+starttime+"'"+","+"'"+starttime+"'"'+"+"+"'"+p
eriodtime+"'"
CommandStr_TimeEnd="Tag:R,'ProcessValueArchive\NewTag',"
CommandStr_TimeEnd=CommandStr_TimeEnd+"'"+endtime+"'"+","+"'"+endtime+"'"'+"+"+"'"+periodtim
e+"'"
Set ConnectionObj=CreateObject("ADODB.Connection")
ConnectionObj.ConnectionString=Connectionstring
ConnectionObj.CursorLocation=3
ConnectionObj.Open
Set RecordSetObj=CreateObject("ADODB.Recordset")
Set CommandObj=CreateObject("ADODB.Command")
CommandObj.ActiveConnection=ConnectionObj
CommandObj.CommandType=1
CommandObj.CommandText=CommandStr_TimeStart
Set RecordSetObj=CommandObj.Execute
temp1=RecordSetObj.Fields(1).Value
MsgBox temp1
CommandObj.CommandText=CommandStr_TimeEnd
Set RecordSetObj=CommandObj.Execute
temp2=RecordSetObj.Fields(1).Value
resultt.Write temp2-temp1
MsgBox resultt
RecordSetObj.Close
Set RecordSetObj=Nothing
Set CommandObj=Nothing
ConnectionObj.Close
Set ConnectionObj=Nothing
End Sub
问题补充:
下面的是正确答案,对此答案的解释见下面的网址:
http://www.ad.siemens.com.cn/club/bbs/post.asp?b_id=5&a_id=516891&s_id=&num=4#anch
Dim starttime
Dim endtime
Dim ConnectionObj
Dim Connectionstring
Dim CommandStr_TimeStart
Dim CommandStr_TimeEnd
Dim CommandObj
Dim RecordSetObj
Dim resultt
Dim temp1
Dim temp2
starttime=HMIRuntime.Tags("TimeStart").Read
endtime=HMIRuntime.Tags("TimeEnd").Read
HMIRuntime.Tags("Result").Write 0
Connectionstring="Provider=WinCCOLEDBProvider.1;Catalog=CC_cc_09_01_07_13_16_32R;Data Source=.\WinCC"
CommandStr_TimeStart="Tag:R,'cc\NewTag',"
CommandStr_TimeStart=CommandStr_TimeStart+"'"+starttime+"'"+","+"'"+endtime+"'"
Set ConnectionObj=CreateObject("ADODB.Connection")
ConnectionObj.ConnectionString=Connectionstring
ConnectionObj.CursorLocation=3
ConnectionObj.Open
Set RecordSetObj=CreateObject("ADODB.Recordset")
Set CommandObj=CreateObject("ADODB.Command")
Set CommandObj.ActiveConnection=ConnectionObj
CommandObj.CommandType=1
CommandObj.CommandText=CommandStr_TimeStart
Set RecordSetObj=CommandObj.Execute
RecordSetObj.MoveFirst
temp1=RecordSetObj.Fields(2).Value
RecordSetObj.MoveLast
temp2=RecordSetObj.Fields(2).Value
resultt=temp2-temp1
HMIRuntime.Tags("Result").Write resultt
RecordSetObj.Close
Set RecordSetObj=Nothing
Set CommandObj=Nothing
ConnectionObj.Close
Set ConnectionObj=Nothing
最佳答案
CommandStr_TimeStart和CommandStr_TimeEnd是多少呢?
必须的符合WinCCOLEDBProvider的查询语法才行啊。
提问者对于答案的评价:
正确答案见上,感谢关注!!!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc275050.html