打算用Treeview来读取SQL数据库里的两个表,但无论如何都会出现这个错误imagelist 必须初始化才能使用可是我的窗体里边根本就没有添加任何IMAGELIST控件,怎么会有这个提示?treeview的style我设置的是textonly,后来实在搞不定,我干脆就添加了个imagelist,结果还是出现这个问题... 谁来救救我,已经费了几个小时的时间了下面是我所用的代码Private Sub Form_Load()
Dim Rst As ADODB.Recordset
Dim CRst As ADODB.Recordset
Dim SQL As String
Dim I As Integer
Dim J As Integer
Dim Pid As Integer
Dim Nod As Node
tvw.Nodes.Add , , , "所有类别", 1
Pid = tvw.Nodes(1).Index
'Set tvw.ImageList = ilt  (我后来添加的,刚开始没有)
Set Rst = New ADODB.Recordset
Rst.CursorLocation = adUseClient
SQL = "select * from yaopin_types where len(id)=2 order by id desc"
Rst.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
If Rst.EOF Then Exit SubFor I = 1 To Rst.RecordCount
  Set Nod = tvw.Nodes.Add
  Nod.Text = Rst.Fields("name") & "(" & Rst.Fields("id") & ")"
  Nod.Key = "n" & Rst.Fields("id").Value
  'Nod.Image = 1   (后来加上的)
  Set Nod.Parent = tvw.Nodes(1)
  
  Set CRst = New ADODB.Recordset
  CRst.CursorLocation = adUseClient
  SQL = "select * from yaopin_types where id like '" & Rst.Fields("id") & "%' and len(id)=4 order by id"
  CRst.Open SQL, Conn, adOpenStatic, adLockReadOnly, adCmdText
  If CRst.EOF Then GoTo move1
  
  For J = 1 To CRst.RecordCount
    tvw.Nodes.Add Nod.Index, tvwChild, "s" & CRst.Fields("id"), CRst.Fields("name") & "(" & CRst.Fields("id") & ")", 1
    CRst.MoveNext
  Next
move1:
  CRst.Close
  Set CRst = Nothing
  
  Rst.MoveNext
Next
Rst.CloseSet Rst = NothingEnd Sub
我不需要用IMAGELIST控件,请大家帮忙,谢谢!!

解决方案 »

  1.   

    For J = 1 To CRst.RecordCount
        tvw.Nodes.Add Nod.Index, tvwChild, "s" & CRst.Fields("id"), CRst.Fields("name") & "(" & CRst.Fields("id") & ")", 1
        CRst.MoveNext
    Next
    ----------------------------------------
    For J = 1 To CRst.RecordCount
        tvw.Nodes.Add Nod.Index, tvwChild, "s" & CRst.Fields("id"), CRst.Fields("name") & "(" & CRst.Fields("id") & ")"
        CRst.MoveNext
    Next把最后那个参数去掉
      

  2.   

    tvw.Nodes.Add Nod.Index, tvwChild, "s" & CRst.Fields("id"), CRst.Fields("name") & "(" & CRst.Fields("id") & ")", 1最后一个 ,1 去掉
      

  3.   

    谢谢各位,问题解决了就是太难看了这样的china--------------------------------------------------------------------(1)
      |____chengdu__________________________________________________________________(1)是代码哪里出毛病了吗?怎么Text这么长?
      

  4.   

    估计是记录集里值包含了多个空格,用Trim函数把去掉空格试试:tvw.Nodes.Add Nod.Index, tvwChild, "s" & CRst.Fields("id"), Trim(CRst.Fields("name") _& "(" & CRst.Fields("id") & ")"
      

  5.   

    如果你添加了imagelist  然后在属性里面设置他的插入图片就可以了
    就不难看了