之前做项目的时候,查阅了大量资料,终于解决了将归档数据写入listviews控件的功能,这里将我的代码发出来以供参考。注意,里面的一些参数需要根据你的具体需要进行修改。
本人还总结了一份技术文档,由于这里不能上传上传文档,请到我的个人博客里下载吧:
http://www.ucooper.com/wincc-summary.htmlSub OnLButtonDown(ByVal Item, ByVal Flags, ByVal x, ByVal y)
Dim sPro,sDsn,sSer,sCon,sSql,oRs,conn,oCom,oItem,m, n, s,i,oList
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_tanks_09_07_14_10_38_56R;"
sSer = "Data Source=.\WinCC"
sCon = sPro + sDsn + sSer
sSql = "TAG:R,'tanks\aa','2009-07-16 00:00:00','2009-07-17 00:00:00'"
MsgBox "Open with:" & vbCr & sCon & vbCr & sSql & vbCr
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
MsgBox(n)
Set oList = ScreenItems("控件1")
oList.ListItems.Clear
olist.ColumnHeaders.clear
oList.View =3 //显示样式,3为报表样式
oList.GridLines =True //显示网格
olist.ColumnHeaders.Add, , CStr(oRs.Fields(0).Name),150
oList.ColumnHeaders.Add, , CStr(oRs.Fields(1).Name),150
oList.ColumnHeaders.Add, , CStr(oRs.Fields(2).Name),150
oList.ColumnHeaders.Add, , CStr(oRs.Fields(3).Name),150
oList.ColumnHeaders.Add, , CStr(oRs.Fields(4).Name),150
'ListView1.ColumnHeaders.Add, , CStr(oRs.Fields(3).Name),150
If(n>0) Then
oRs.MoveFirst
End If
m = 0
Do While Not oRs.EOF
m = m + 1
Set oItem = oList.ListItems.Add()
oItem.text=CStr(oRs.Fields(0).value)
' oItem.SubItems(1)="bb"
' oItem.SubItems(2)="cc"
oItem.SubItems(1) = CStr(DateAdd("h", -8,oRs.Fields(1).value))
oItem.SubItems(2) = CStr(oRs.Fields(2).value)
oItem.SubItems(3) = CStr(oRs.Fields(3).value)
oItem.SubItems(4) = CStr(oRs.Fields(4).value)
' oItem.Text = Left(CStr(oRs.Fields(1).Value), 23)
' oItem.SubItems(1) = FormatNumber(oRs.Fields(2).Value, 4)
' oItem.SubItems(2) = Hex(oRs.Fields(4).Value)
If m > 100 Then
Exit Do
Else
End If
oRs.MoveNext
Loop
MsgBox("wwwww")
oRs.Close
Set oRs = Nothing
conn.Close
Set conn = Nothing
End Sub