2012-07-16 57 views
1

表1如何避免在列表框中重复值

ID Division Dept 

001 CS IT 
002 CD Admin 
003 AS Admin 

我想了重复值

试图代码加载列表框中部门

Dim rdoRs As New ADODB.Recordset 
Dim record As Variant 
Dim Div As Variant 
Dim usr, MySQL As String 
usr = "CD,AS," 
record = Split(usr, ",") 
For Each Div In record 
MySQL = "Select Distinct dept from table1 Where division = '" & div & "'" 
    rdoRs.Open MySQL, conn1 
    If rdoRs.RecordCount > 0 Then 
     Do While Not rdoRs.EOF 
     listbox1.AddItem rdoRs!dept 
      rdoRs.MoveNext 
      Loop 
    End If 
    rdoRs.Close 
Next 

输出

Listbox1 

Admin 'Loaded for CD Division 
Admin 'Loaded for AS Division 

上面的代码是w奥可福罚款,但它是加载2倍管理部门。在列表框中。由于For Loop正在加载CD的部门管理员,并且它正在加载AS部门的部门管理员。

我不想在列表框中显示重复的值。

预期输出

Listbox1 

Admin 'Loaded for both CD and AS Division 

如何在VB6做到这一点。

需要VB6代码帮助。

回答

1

编写一个函数来检查它是否已经在列表...

Public Function FindInList(theList as ListBox, theString as String) 
    Dim i as Integer 
    theString = LCase(Trim(theString)) 

    For i = 0 to theList.ListCount - 1 
     If LCase(Trim(theList.List(i))) = theString then 
      FindInList = i 
      Exit Function 
     End If 
    Next i 

    FindInList = -1 
End Function 

,然后当你想要的东西添加到列表中,只是做...

If FindInList(List1, StringToAdd) = -1 Then List1.AddItem(StringToAdd) 
+0

,它不应该提供重复值对不起,代码中有一个错误,它将LCase()的d字符串与未知情况的字符串进行比较。这会导致代码无法工作。现在修复。 – TimFoolery 2012-07-16 05:30:46

0

您可以在查询做到这一点

变化

MySQL = "Select dept from table1 Where division = '" & div & "'" 

喜欢的东西

MySQL = "Select DISTINCT dept from table1 Where division = '" & div & "'" 
+0

下站在我的问题,请,我不SQL查询有问题,我也知道不同的值,FOR循环重复给予值... – JetJack 2012-07-16 05:00:35

+0

如果您正在使用DISTINCT – 2012-07-16 05:18:47