比较2个列表框并在第3个列表框中显示不匹配的值

时间:2012-07-26 20:02:59

标签: ms-access-2003

我已经为此工作了2天。基本上我有2个ListBoxes,我想要一个命令按钮来比较值并显示不匹配的值(出现在第一个列表框中但不出现在第二个列表框中的值)并将它们列在第3个列表框中。我不确定这是否是最佳方式,但这是我的代码。它出现错误信息:

  

参数数量错误或属性分配无效

我的列表框名为CompList1,CompList2和CompList3。

Dim BoolAdd As Boolean, I As Long, j As Long
'Set initial Flag
BoolAdd = True
'If CompList2 is empty then abort operation
If CompList2.ListCount = 0 Then
MsgBox "Nothing to compare"
Exit Sub
'If CompList1 is empty then copy entire CompList2 to CompList3
ElseIf CompList1.ListCount = 0 Then
For I = 0 To CompList2.ListCount
CompList3.AddItem CompList2.Value
Next I
Else
For I = CompList2.ListCount - 1 To 0 Step -1
For j = 0 To CompList1.ListCount
If CompList2.ListCount(I) = CompList1.ListCount(j) Then
'If match found then abort
BoolAdd = False
Exit For
End If
DoEvents
Next j
'If not found then add to CompList3
If BoolAdd = True Then CompList3.AddItem CompList2.Value
DoEvents
Next I
End If

1 个答案:

答案 0 :(得分:0)

一些注意事项:

Dim tdf1 As TableDef
Dim tdf2 As TableDef
Dim db As Database

Set db = CurrentDb

Set tdf1 = db.TableDefs(Me.CompList1.RowSource)
For Each fld In tdf1.Fields
    sFields = sFields & ";" & fld.Name
Next

sFields = sFields & ";"

Set tdf2 = db.TableDefs(Me.CompList2.RowSource)
For Each fld In tdf2.Fields
    sf = ";" & fld.Name & ";"
    sFields = Replace(sFields, sf, ";")
Next

Me.CompList3.RowSource = Mid(sFields,2)

编辑:

Field List, combo properties

相关问题