2017-12-11 2 views
0

MS Access 2016のリストボックス(リストボックス1)は、1列 - ActualDateを持っています。リストボックスから重複をカウントして返す

この列には多数の日付が含まれており、その一部は複製されています。 ActualDateカウント - - Countがlistbox1をから選択された行の数であると

このリストボックスのための値集合ソースは、私は2列を有し、同じフォーム上の別のリストボックス(Listbox2)を移入する必要が

Set rs = CurrentDb.OpenRecordset("SELECT q.ActualDate FROM TBLQUOTESNEW q WHERE q.ActualDate >= #12/01/2017# order by q.ActualDate") 

あります日付を含む。

のでlistbox1をは次のようになります -

13/01/2017 
13/01/2017 
14/01/2017 
14/01/2017 

すべての4行を選択した場合は、Listbox2は、私はこれを達成するための最良の方法ではよく分からない

13/01/2017 2 
14/01/2017 2 

を返す必要があります。私はユニークな日付の配列を作成することができましたが、そこから私は困惑しています。

+0

最初のコンボボックスの行ソースとは何ですか?複雑なVBAソリューションではなく、単純なクエリを使用することができます。 –

+0

@ErikvonAsmuth追加されたソース – user1936588

+0

あなたは選択された行にのみ関心があります。 1つの日付が2回選択されているのに3回発生する場合は、カウントを2と表示しますか? –

答えて

1

をクエリーのグループ化に基づいて、第2のリストボックスやサブフォームを表示することは非常に容易になりますことをMoveListBoxItems Me.Listbox2, Me.Listbox1

注:

Public Sub MoveListBoxItems(lstDestination As ListBox, lstSource As ListBox) 
    Dim intListItem As Long 
    Dim lastItem As String 
    Dim itemAmount As Long 
    'Set these using the property pane, then remove them from the VBA 
    lstDestination.RowSource = "" 
    lstDestination.RowSourceType = "Value List" 
    lstDestination.ColumnCount = 2 
    For intListItem = 0 To lstSource.ListCount - 1 'iterate through the whole list 
     If lstSource.Selected(intListItem) Then 'If the item is selected 
      If lstSource.ItemData(intListItem) = lastItem Then 'If the current item is equal to the last one 
       itemAmount = itemAmount + 1 'Increment the amount by 1 
      Else 
       If itemAmount <> 0 Then 'If it isn't a non-occuring list item (first iteration 
        lstDestination.RowSource = lstDestination.RowSource & """" & lastItem & """;""" & itemAmount & """;" 
       End If 'Add the item 
       lastItem = lstSource.ItemData(intListItem) 'Last item = current item, amount = 1 
       itemAmount = 1 
      End If 
     End If 
    Next intListItem 
    If itemAmount <> 0 Then 'If it isn't a non-occuring list item 
     lstDestination.RowSource = lstDestination.RowSource & """" & lastItem & """;""" & itemAmount & """;" 
    End If 'Add the last item 
End Sub 

はこのようにそれを呼び出しますリストは順序付けされなければならず、リストに引用符を含めてはいけません。そうでなければ、引用符をエスケープする必要があります。

0

リストボックスの代わりにサブフォームを使用します。追加列「選択済み」を含む一時表に基づくサブフォームは、チェックボックスを使用してレコードを選択します。この場合、あなたは次のサブルーチンを使用することができ、一時テーブルから

関連する問題