我安书上的代码写到按钮的事件里面,用listview显示数据,代码如下:
Dim sPro
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim conn
Dim oRs
Dim oCom
Dim oItem
Dim m,n,s
'#为ADODB创建connection string
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_yt080313_08_06_14_08_59_34R;"
sSer = "Data Source=server2\WinCC"
sCon = sPro + sDsn + sSer
'#在sSql定义命令文本(相对时间)
'sSql="select * from "
sSql = "TAG:R,'monishuju/ws1 ','2008-07-29 15:00:00.000','2008-07-29 15:14:00.000'"
'sSql = "TAG:R,1,'0000-00-00 00:10:00.000','0000-00-00 00:00:00.000'"
MsgBox "Open with:" & vbCr & sCon & vbCr & sSql & vbCr
'#建立连接
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = sCon
conn.CursorLocation = 3
conn.Open
MsgBox "aaaaaaaaaa"
'#使用命令文本进行查询
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandType = 1
Set oCom.ActiveConnection = conn
oCom.CommandText = sSql
MsgBox "bbbbbbbbbbbbb"
'#填充记录集
MsgBox "cccfffffff"
Set oRs = oCom.Execute
m = oRs.Fields.Count
MsgBox "ccccccccccccccc"
'm=oRs.RecordCount
'#用记录集填充标准listview对象
ListView1.ColumnHeaders.Clear
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(1).Name), 240
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(2).Name), 140
ListView1.ColumnHeaders.Add , , CStr(oRs.Fields(3).Name), 140
If (m > 0) Then
oRs.MoveFirst
n = 0
MsgBox "dddddddddddddddd"
Do While Not oRs.EOF
n = n + 1
s = Left(CStr(oRs.Fields(1).Value), 23)
Set oItem = ListView1.ListItems.Add()
oItem.Text = Left(CStr(oRs.Fields(1).Value), 23)
oItem.SubItems(1) = FormatNumber(oRs.Fields(2).Value, 4)
oItem.SubItems(2) = Hex(oRs.Fields(3).Value)
If (n > 100) Then Exit Do
oRs.MoveNext
Loop
oRs.Close
Else
End If
Set oRs = Nothing
conn.Close
Set conn = Nothing
但是当代码执行到:Set oRs = oCom.Execute 时就不再执行了,请高手指点一下这是怎么回事呀,我看过以前的帖子但是还是没有找到解决办法
最佳答案
sSql = "TAG:R,'monishuju/ws1 ','2008-07-29 15:00:00.000','2008-07-29 15:14:00.000'"
应该是:
sSql = "TAG:R,'monishuju\ws1 ','2008-07-29 15:00:00.000','2008-07-29 15:14:00.000'"
归档名与归档变量之间用反斜杠“\”隔开。
提问者对于答案的评价:
谢谢,解决了,不过帮助里的代码和书上的代码有问题,改了之后才调试成功,大家注意呀,呵呵
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc276666.html