背景:WinCC 7.3,写一个数据查询的VBs,想通过ADO接口OLEDB连接到变量归档数据库,按起始日期和终止日期查询数据。脚本写在查询按钮上触发。
问题:执行到查询Set objRecord1=objCommand1.Execute时报错,一步步调试,发现在程序中段,Set objCon=Createobject("ADODB.Connection")这句总是连不上,用Msgbox objCon.state发送连接状态,返回0没有连接上,大神们给看一看是什么原因(语句或设置)。
变量:Flow1,Flow2,BeginTime,EndTime
归档:变量归档FlowArch,归档变量名称Flow01,Flow01
控件:MSHGrid控件,名称Grid
VBs如下:
Sub OnClick(ByVal Item)
Dim LocalBeginTime,LocalEndTime,UTCBeginTime,UTCEndTime
Dim DSN
Dim DSName,sPro,sDSName,sDS,sCon,objCon
On Error Resume Next
Set LocalBeginTime=HMIRuntime.Tags("BeginTime")
Set LocalEndTime=HMIRuntime.Tags("EndTime")
LocalBeginTime.read
LocalEndTime.read
UTCBeginTime=Dateadd("h",-8,LocalBeginTime.Value)
UTCEndTime=Dateadd("h",-8,LocalEndTime.Value)
UTCBeginTime = Year(UTCBeginTime) & "-" & Month(UTCBeginTime) & "-" & Day(UTCBeginTime) & " " & Hour(UTCBeginTime) & ":" & Minute(UTCBeginTime) & ":" & Second(UTCBeginTime)
UTCEndTime = Year(UTCEndTime) & "-" & Month(UTCEndTime) & "-" & Day(UTCEndTime) & " " & Hour(UTCEndTime) & ":" & Minute(UTCEndTime) & ":" & Second(UTCEndTime)
HMIRuntime.Trace "UTC Begin Time: " & UTCBeginTime & vbCrLf
HMIRuntime.Trace "UTC end Time: " & UTCEndTime & vbCrLf
Set DSName=HMIRuntime.Tags("@DatasourceNameRT")
DSName.Read
sPro="Provider=WinCCOLEDBProvider.1;"
sDSName="Catalog="&DSName.Value&";"
sDS="Data Source=.\WinCC"
sCon=sPro&sDSName&sDS
HMIRuntime.Trace sCon&vbCRLF
Set objCon=Createobject("ADODB.Connection")
Msgbox "开始测试连接状态,1=Success,0=Fail"
Msgbox objCon.state
objCon.ConnectionString=sCon
objCon.CursorLocation=3 '客户端游标方式
objCon.Open sCon
Dim objSQL1,objSQL2
objSQL1="Tag:R,('FlowArch\Flow01'),'"&UTCBeginTime&"','"&UTCEndTime&"',"&"'order by timestamp ACS','TimeStep=1,1'"
objSQL2="Tag:R,('FlowArch\Flow02'),'"&UTCBeginTime&"','"&UTCEndTime&"',"&"'order by timestamp ACS','TimeStep=1,1'"
Dim objRecord1,objCommand1,objRecord2,objCommand2
Set objRecord1=Createobject("ADODB.Recordset")
Set objCommand1=Createobject("ADODB.Command")
objCommand1.CommandType=1
Set objCommand1.ActiveConnection=objCon
objCommand1.CommandText=objSQL1
Set objRecord1=objCommand1.Execute
Set objRecord2=Createobject("ADODB.Recordset")
Set objCommand2=Createobject("ADODB.Command")
objCommand2.CommandType=1
Set objCommand2.ActiveConnection=objSQL1
objCommand2.CommandText=objSQL2
Set objRecord2=objCommand1.Execute
End Sub
最佳答案
WINCC连接归档数据库官方有脚本的
参考:
在WinCC中如何使用VBS读取报警记录数据到EXCEL
https://support.industry.siemens.com/cs/cn/zh/view/77938393
在WinCC中如何使用VBS读取变量归档数据到EXCEL
https://support.industry.siemens.com/cs/cn/zh/view/77940055
通过VBS脚本在ListView控件中显示WinCC的变量记录
https://support.industry.siemens.com/cs/cn/zh/view/74932765
更多的相关资料参考:
西门子官方资料大全分享之WINCC
http://www.ad.siemens.com.cn/club/bbs/post.aspx?a_id=1378253&b_id=5
或者参考:
http://mp.weixin.qq.com/mp/homepage?__biz=MzIwNTUyNTIwNg==&hid=4&sn=e1f84810a1c0f90d2a8829be52f126c2&scene=18#wechat_redirect
提问者对于答案的评价:
最后测试成功,还是很感谢大家
原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc363021.html