展开查看
以下是引用zhangli0在2014-10-16 08:46:49的发言 >5楼:wincc画面上Excel控件的名字为“控件1"
zhangli0按着这格式写了VB,但是EXCEL控件中还是未能显示数据,能帮忙看下问题出在哪吗,代码如下
Sub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
'---------------------------------------
Dim con
Dim kongjian1
Set kongjian1 = ScreenItems("ribb")
Dim str
'Dim blnOK
Dim sql
Dim it
Dim i
Dim rst
Dim cmd
Dim obj
Dim ok
Dim worksheet
Dim qyear
Dim qmonth
Dim qday
Dim qhour
Dim qendyear
Dim qendmonth
Dim qendday
Dim qendhour
Dim reportname
reportname="日报表"
Dim t1,t2
On Error Resume Next
Set obj=hmiruntime.Screens("日报表").ScreenItems("ribb")
Set worksheet=obj.Object.sheets(1)
Set con=CreateObject("ADODB.Connection")
str="Provider=MSDASQL;DSN=yx;Uid=sa;Pwd=;"
con.ConnectionString=str
con.Open
If con.State=1 Then
Dim rili
Set rili=ScreenItems("rq1")
qyear=rili.year
qmonth=rili.month
qday=rili.day
qhour=rili.hour
if qmonth<10 then
if qday<10 then
t1=qyear & "-0" & qmonth & "-0" & qday
Else
t1=qyear & "-0" & qmonth & "-" & qday
End If
End If
If qmonth>=10 Then
If qday<10 Then
t1=qyear & "-" & qmonth & "-0" & qday
else
t1=qyear & "-" & qmonth & "-" & qday
End If
End If
Dim d1
Dim d2
d1=t1 & " 00" & ":00" & ":00"
d2=t1 & " 23" & ":59" & ":00"
sql="select * from dbo.日报表 where 时间>= '"&d1&"' and 时间<='"&d2&"' order by 时间 ASC"
Set cmd=CreateObject("ADODB.Command")
cmd.ActiveConnection=con
cmd.CommandText=sql
Set rst=CreateObject("ADODB.Recordset")
Set rst=cmd.Execute
With worksheet
.activate
i=4
If rst.RecordCount>2000 Then
ok=MsgBox("符合条件的记录超过2000条!",vbOK)
Else
Do Until rst.EOF
worksheet.cells(i,1).value=rst.fields("时间").value
i=i+1
rst.movenext
Loop
End If
End With
Set worksheet=Nothing
Set obj=Nothing
rst.close
Set rst=Nothing
Set cmd=Nothing
'MsgBox "执行成功",vbOKOnly,"提示"
con.Close
End If
Set con=Nothing
End Sub