我想在ListView控件中添加我数据库中一个表的的字段,但是这个字段(字段名称:主机编号)中有很多是一样的主机编号,这样在我ListView控件添加的内容就出现很多一样的名称。请教下论坛朋友,如何做到一样的名称只在ListView中添加一次。下边是我现在添加字段的代码。
Dim itmX As ListItem '定义一个ListItem对象
Dim key As String '定义字符串变量Adodc1.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\客车列尾运行数据.mdb"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from 客车列尾运行数据"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
ListView1.Enabled = True
ListView1.ListItems.Clear
Adodc1.Recordset.MoveFirst
Do While Adodc1.Recordset.EOF = False '将操作员信息添加到ListView控件当中
key = Adodc1.Recordset.Fields("主机编号") & ""
Set itmX = ListView1.ListItems.Add(, , key, 1) 'distinct
Adodc1.Recordset.MoveNext
Loop
Else
ListView1.Enabled = False
End If
Dim itmX As ListItem '定义一个ListItem对象
Dim key As String '定义字符串变量Adodc1.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\客车列尾运行数据.mdb"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from 客车列尾运行数据"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
ListView1.Enabled = True
ListView1.ListItems.Clear
Adodc1.Recordset.MoveFirst
Do While Adodc1.Recordset.EOF = False '将操作员信息添加到ListView控件当中
key = Adodc1.Recordset.Fields("主机编号") & ""
Set itmX = ListView1.ListItems.Add(, , key, 1) 'distinct
Adodc1.Recordset.MoveNext
Loop
Else
ListView1.Enabled = False
End If
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select distinct 主机编号 from 客车列尾运行数据 "
Adodc1.Refresh
key = Adodc1.RecordSource
这样就把我VB程序搞死了。正能关闭重新启动VB了。
是指Adodc1还有它用?
再用一个Adodc2就是了,或者这里用完了,再设置一下就可以了
Adodc1.RecordSource = "select * from 客车列尾运行数据 "
Adodc1.Refresh
Adodc1.Refresh
这两句可以重复用.
红色的部分改为for i=1 to Adodc1.Recordset.RecordCount
Set itmX = ListView1.ListItems.Add(, , Adodc1.Recordset.Fields("主机编号") , 1)
next i但我想问一句,你只有一列的吗?做这个之前要把listview的标题部分加进去
Set itmX = ListView1.ListItems.Add(, , Adodc1.Recordset.Fields("主机编号") , 1)
Adodc1.Recordset.MoveNext
next i
Dim itmX As ListItem '定义一个ListItem对象
Dim key As String '定义字符串变量 Adodc1.connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\客车列尾运行数据.mdb"
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "select * from 客车列尾运行数据" 改成下边的
Adodc1.RecordSource = "select distinct 主机编号 from 客车列尾运行数据"
Adodc1.Refresh
If Adodc1.Recordset.RecordCount > 0 Then
ListView1.Enabled = True
ListView1.ListItems.Clear
Adodc1.Recordset.MoveFirst
Do While Adodc1.Recordset.EOF = False '将操作员信息添加到ListView控件当中
key = Adodc1.Recordset.Fields("主机编号") & ""
Set itmX = ListView1.ListItems.Add(, , key, 1) 'distinct
Adodc1.Recordset.MoveNext
Loop
Else
ListView1.Enabled = False
End If