Dim i As Integer
Dim name As String
Dim value As String
For i = 0 To 4name = Trim$(NameText(i).Text)value = Trim$(ValueText(i).Text)If name <> "" ThenPrimitives.Add value, name  在增加前我想加个判断,如已有,则不加入,求教各位该如何做,谢谢!!!End IfNext i

解决方案 »

  1.   

    private function PrimitivesExist(col as collection,str as string) as boolean
    dim idx as integer
    for idx = 1 to col.count
    if col(idx) = str then
    PrimitivesExist = true
    exit for
    next
    end function
      

  2.   

    collection没有比对之类的功能函数,只能遍历,或者变通判断,比如:
    Dim Primitives As New Collection
    On Error Resume Next
    Dim i As Integer
    Dim name As String
    Dim value As String
    For i = 0 To 4
    name = Trim$(NameText(i).Text)
    value = Trim$(ValueText(i).Text)
    If name <> "" Then Primitives.Add value, name
    err.clear
    Next i
      

  3.   


    Private Sub Command1_Click()
        Dim Primitives As New Collection
        Dim i As Integer
        Dim name As String
        Dim value As String
        For i = 0 To 3
            name = Trim$(NameText(i).Text)
            value = Trim$(ValueText(i).Text)
            If name <> "" Then
               If CLNkeyTF(name, Primitives) Then _
                  Primitives.Add value, name
            End If
         Next i
         
    End SubPrivate Function CLNkeyTF(ByVal key As String, cln As Collection) As Boolean
        On Error GoTo flgTrue
        cln.Item key
        CLNkeyTF = False
        Exit Function
    flgTrue: CLNkeyTF = True
    End Function