Access에서 한 테이블의 필드를 다른 테이블의 한 필드로 결합

Access에서 한 테이블의 필드를 다른 테이블의 한 필드로 결합

사용자 ID가 있는 한 테이블의 여러 필드와 수평으로 배열된 10개의 데이터 열을 결합하여 사용자 ID에 해당하는 필드와 10개의 수직 필드에 해당하는 필드가 있는 두 개의 필드에 수직으로 표시하려고 합니다. 두 번째 테이블에서는 원래 테이블의 10개 가로 필드에 입력이 있는 횟수만큼 사용자 이름이 반복됩니다.

내 예는 기본적으로 다음과 같습니다.

MainT
`````
User ID
otherfields
Invoice1
:
Invoice10

새 테이블에 넣고 싶습니다.

InvoicesT
`````````
User ID
Invoices

MainT에서 데이터를 가져오는 방법을 알아야 합니다.

User    |   Othr Fld    |   Invoice1    |   Invoice2    |   Invoice3
--------+---------------+---------------+---------------+-------------
1       |   JF          |   Paid        |   Past Due    |  Civil Court
2       |   JN          |   Paid        |   Paid        |  Past Due
3       |   MO          |   Past Due    |   Paid        |  Paid

내가 규정한 방식으로 송장T을 작성합니다.

User    |   Invoice
--------+--------------
1       |   Paid
1       |   Past Due 
1       |   Civil Court
2       |   Paid
2       |   Past Due

등.

답변1

원하는 방식으로 데이터를 변환할 수 있는 간단한 쿼리가 하나도 없었던 기억이 납니다. 내 마음에 가장 먼저 떠오른 것은 크로스탭 쿼리였지만, 이것은 가지고 있는 데이터 구조를 사용하여 수행할 수 있는 작업이 아닙니다. 저는 귀하가 테이블 데이터를 재설계하는 데 올바른 방향으로 가고 있다고 생각하므로 VBA 솔루션을 제공하고 싶습니다.

이 솔루션이 작동하려면 먼저 InvoicesT다음 필드를 사용하여 두 번째 테이블을 만듭니다.

InvoicesT
`````````
Field Name          |   Data Type
--------------------+------------------
invoiceID           |   Autonumber 
UserID              |   Number
InvoiceCategory     |   Text

다음으로 다음 하위를 표준 모듈에 복사합니다.

Public Sub transferData()
    Dim oldTbl As DAO.Recordset, newTbl As DAO.Recordset
    Dim dbObj As DAO.Database
    Dim fCtr As Integer

    Set dbObj = CurrentDB()

    'Open the MainT table to read the information.
    Set oldTbl = dbObj.OpenRecordSet("SELECT [User ID], Invoice1, Invoice2, Invoice3, Invoice4, " & _
                                     "Invoice5, Invoice6, Invoice7, Invoice8, Invoice9, Invoice10 FROM MainT")

    'Open the InvoicesT to write new information into.
    Set newTbl = dbObj.OpenRecordSet("InvoicesT")

    'Loop through the list of all entries in MainT
    Do While Not oldTbl.EOF
        'This will loop through the fields to create one entry for each field.
        'You can change the number 10 to any number depending on the Invoice fields in the table. 
        For fCtr = 1 To 10
            'I have put an IF condition to make sure there are not many empty records for a UserID
            'If the Invoice(n) is not available for a particular user, then it does not create an entry. 
            If Len(oldTbl.Fields("Invoice" & fCtr) & vbNullString) > 0 Then
                With newTbl
                    .AddNew
                    .Fields("UserID") = oldTbl.Fields(0)
                    .Fields("InvoiceCategory") = oldTbl.Fields("Invoice" & fCtr)
                    .Update
                End With
            End If
        Next
        'Go to the next record when one user is done with
        oldTbl.MoveNext
    Loop

    'Clean up
    Set newTbl = Nothing
    Set oldTbl = Nothing
    Set dbObj = Nothing
End Sub

이제 모듈을 다음과 같은 이름으로 저장 mod_TransferTables하고 컴파일했습니다. 즉시 실행 창으로 이동합니다(CTRL + G). 그런 다음 을 입력 transferData하고 누르기 만 ENTER하면 테이블의 레코드 수에 따라 시간이 걸릴 수 있습니다 MainT. 일단 이것이 완료됩니다. 새 테이블에 모든 데이터가 있어야 합니다.

도움이 되었기를 바랍니다 !

관련 정보