我想用listview控件实现两个归档变量的数据查询,但是总是做不出来,希望各位高手给提点建议!
Dim sPro
Dim sDsn
Dim sSer
Dim sCon
Dim sSql
Dim oRs
Dim conn
Dim oCom
Dim oItem
Dim m, n, s
Dim oList
Set oList = ScreenItems("listview1")
oList.View =3
oList.ListItems.Clear
sPro = "Provider=WinCCOLEDBProvider.1;"
sDsn = "Catalog=CC_jjin_12_10_29_16_49_51R;"
sSer = "Data Source=PC-201210291444\WinCC"
sCon = sPro + sDsn + sSer
sSql = "TAG:R,(12;15),'2013-03-20 00:01:00.000','2013-03-20 19:00:00.000'"
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
m = oRs.Fields.Count
MsgBox "Open with:" & vbCr & m & vbCr
oList.ColumnHeaders.Clear
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
If (m > 0) Then
oRs.MoveFirst
n = 0
MsgBox "Open with:" & vbCr & m & vbCr
Do While Not ors.eof
n=n+1
s=Left(CStr(ors.Fields(1).Value),23)
Set oitem= oList.ListItems.Add()
oitem.Text=Left(CStr(ors.Fields(1).Value),23)
oitem.SubItems(1)=FormatNumber(ors.Fields(2).Value,4)
oitem.SubItems(2)=FormatNumber(ors.Fields(0).Value,4)
ors.MoveNext
Loop
ors.Close
Else
End If
Set ors=Nothing
conn.close
Set conn=Nothing
问题补充:
我想用listview控件实现归档数据的查询,单变量可以实现,但是两个变量的无法实现,希望各位提出宝贵的建议!
最佳答案
每次只查询一个,循环着来。
貌似你即便能够一次查询出来多个,也没法直接用RECORDSET直接将结果赋值到LIST控件。ADO得到的数据矩阵,并不是你我想要的布局格式。
所以还是要编程循环。
提问者对于答案的评价:
嗯,我已经实现了,就是采用循环的方式实现的!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc250968.html