2012-07-26 30 views
1

我一直在爲此工作2天。基本上我有2個ListBox,我想要一個命令按鈕來比較這些值並顯示不匹配的值(那些出現在第一個列表框但不在第二個列表框中)並將它們列在第三個列表框中。我不確定這是否是最好的方法,但這裏是我的代碼。它的錯誤符合以下消息:比較2個列表框並在第3個列表框中顯示不匹配的值

Wrong number of arguments or invalid property assignment

我的列表框被命名爲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 
+0

哪些行源? – Fionnuala 2012-07-26 20:05:28

+0

CompList1和CompList2填充了表格的字段名稱。表名將始終相同,但字段將隨每次使用而改變。行源類型設置爲字段列表。 – JenPhyllisB 2012-07-26 20:07:35

回答

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

+0

謝謝你的建議。這是否意味着命令按鈕事件?當我嘗試在Private Sub CmdComparisonResults_Click()和End Sub之間運行它時,我收到錯誤消息「Compile error:Expected End Sub」 – JenPhyllisB 2012-07-26 20:18:57

+0

我想我可以更清楚地解釋這一點。我只想要在CompList1中但不在CompList2中的字段名稱。我不關心實際的字段值。 – JenPhyllisB 2012-07-26 20:23:15

+0

感謝您的耐心等待。當我運行代碼錯誤行Set tdf1 = db.TableDefs(Me.CompList1.RowSource)與錯誤消息「運行時錯誤」3265'項目找不到此集合中 – JenPhyllisB 2012-07-26 20:39:48

相關問題