| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 364 人关注过本帖
标题:ADO输出到LISTVIEW错误
只看楼主 加入收藏
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
ADO输出到LISTVIEW错误
程序代码:
Private Sub Form_Load()
Dim con As Object
Dim rs As Object
Set con = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source='F:\订单一览表.xls';Extended Properties='Excel 8.0;HDR=Yes'"
con.Open
Sql = "select * from [Page1$]"
rs.Open Sql, con, 3, 3

Do While Not rs.EOF
Set Item = ListView1.listitems.Add()
Item.Text = rs.fields(0)
For i = 2 To rs.Recordcount - 1
Item.SubItems(1) = rs.fields(i)
Next
rs.movenext
Loop

rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
End Sub


Item.SubItems(1) = rs.fields(i) 提示无效的属性值,哪里出的问题,请各位帮忙指导下,小弟新手,谢谢。

[ 本帖最后由 xxyyx82 于 2015-7-19 20:24 编辑 ]
2015-07-19 20:22
lianyicq
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:26
帖 子:737
专家分:3488
注 册:2013-1-26
收藏
得分:20 
回复 楼主 xxyyx82
在listitem.add前应该ListView1.ColumnHeaders.Add, 把字段名称给ListView
For i = 2 To rs.Recordcount - 1'此处应该是每条记录的列数,而不记录数
  Item.SubItems(1) = rs.fields(i)'subitem后括号内应该是个变量,不是1
Next
比如
程序代码:
Private Sub Form_Load()
  Dim con As Object
  Dim rs As Recordset
  Dim itm
  Set con = CreateObject("ADODB.Connection")
  Set rs = CreateObject("ADODB.Recordset")
  con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source='e:\test.xlsx';Extended Properties='Excel 8.0;HDR=Yes'"
  con.Open

  Sql = "select * from [sheet1$]"
  rs.Open Sql, con, 3, 3
  For i = 1 To 5
    ListView1.ColumnHeaders.Add , , i, ListView1.Width / 5, 0
  Next
  Do While Not rs.EOF
    Set Item = ListView1.ListItems.Add()
    Item.Text = rs.Fields(0)
    For i = 1 To 2
      Item.SubItems(i) = rs.Fields(i)
    Next
    rs.MoveNext
  Loop

  ListView1.View = lvwReport
  ListView1.GridLines = True
  rs.Close
  Set rs = Nothing
  con.Close
  Set con = Nothing
End Sub


大开眼界
2015-07-20 09:44
快速回复:ADO输出到LISTVIEW错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041342 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved