2017-12-11 5 views
0

1 열 - ActualDate MS Access 2016 목록 상자 (Listbox1) 있습니다.목록 상자에서 중복 계산 및 반환

이 열에는 여러 날짜가 포함되어 있으며 그 중 일부는 복제됩니다. ActualDate 및 개수 - - 카운트가에 ListBox1에서 선택된 행의 개수 인 상태

이리스트 박스 행 원본은 I 2 열이 동일한 형태의 다른리스트 박스 (Listbox2)를 채워야

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

인 날짜가 들어 있습니다.

그래서에 ListBox1이 될 수있다 : - 4 개 행이 선택된다면

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

이 Listbox2이

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

내가 이것을 달성하기 위해 가장 좋은 방법에 확실하지 않다 반환해야합니다. 고유 한 날짜가있는 배열을 만들 수 있었지만 거기에서 난처한 상태입니다.

+0

첫 번째 콤보 상자의 행 원본은 무엇입니까? 복잡한 VBA 솔루션 대신 간단한 쿼리를 사용할 수 있습니다. –

+0

@ErikvonAsmuth 추가 된 소스 – user1936588

+0

당신은 단지 선택된 행에만 관심이 있습니다. 한 날짜가 2 번 선택되었지만 3 번 발생하면 카운트를 2로 표시 하시겠습니까? –

답변

1

다음 서브 루틴을 사용할 수 있습니다

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 

이처럼 전화 : MoveListBoxItems Me.Listbox2, Me.Listbox1

주를이 그것은 목록을 정렬해야합니다. 목록에 따옴표를 포함해서는 안됩니다 (그렇지 않으면 인용 부호를 추가해야합니다.)

0

목록 상자 대신 하위 양식을 사용합니다. 하위 열은 추가 열 "선택됨"이있는 임시 테이블을 기반으로하며 확인란을 사용하여 레코드를 선택합니다. 이 경우 임시 테이블에서 쿼리를 그룹화에 따라 두 번째 목록 상자 또는 하위 폼을 표시하는 것은 매우 쉬운 것입니다