简体   繁体   中英

Access/VBA - Creating an new table with grouped records

I've been thinking about my problem and researched here and the on the internet. I don't seem to get a step further...

I have a database with different tables. One of them is like a client table. Every client has to send in forms (up to 3) each months, this is in a second table.

It looks somewhat like this:

**tblReportedFiles**
ID Month Form
1 201803 1
2 201803 1
1 201803 2
2 201804 2
3 201804 3
1 201804 1

My goal is to create code that will create a new table with the following dataset:

    clientID - ReportingMonth - Form 1 - Form 2 - Form 3
        1         201803         true     true     false
        2         201803         true     false    false
        3         201803         false    false    false
        1         201804         true     false    false
        2         201804         false    true     false
        3         201804         false    false    true

I just cant to get anything to work. I playe around with SQL and VBA but nothing goes into the right direction.

Do you have any ideas?

You can use a crosstab (pivot) query to achieve this. That's more flexible than the solution suggested by Jaime, since this will auto-create columns as new forms are added to the table.

TRANSFORM CBool(Count([Form]))
SELECT ID, [Month]
FROM tblReportedFiles
GROUP BY ID, [Month]
PIVOT "Form " & [Form]

We're both transforming Form to the count cast to a boolean, and pivoting by Form . That means if the specific Form value occurs for the grouping variables, the count will be 1, and that's cast to a boolean True , and if it doesn't, the count will be 0, which casts to a boolean False .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM