Note that there are some explanatory texts on larger screens.

plurals
  1. POImprovement of VBA code in Access to do something like a pivot table
    primarykey
    data
    text
    <p>I have a table of data in MS Access 2007. There are 6 fields per record, thousands of records. I want to make a sort of pivot table like object. That is, if any two rows happens to be the same in the first 4 fields, then they will end up grouped together into one row. The column headers in this pivot table will be the values from the 5th field, and the value in the pivot table will be the 6th field, a dollar amount. Think of the 5th field as letters A, B, C, D, E, F, G. So, the table I start with might have a row with A in the 5th field and $3.48 in the 6th field. Another row may match in the first 4 fields, have B in the 5th field and $8.59 in the 6th field. Another may match in the first 4 fields, have E in the 5th field and $45.20 in the 6th field. I want all these rows to be turned into one row (in a new table) that starts with the first 4 fields where they match, then lists $3.48, $8.59, $0.00, $0.00, $45.20, $0.00, $0.00, corresponding to column headers A, B, C, D, E, F, G (since no records contained C, D, F, G, their corresponding values are $0.00), and then ends with one more field that totals up the money in that row.</p> <p>Currently, I have some VBA code that does this, written by someone else a few years ago. It is extremely slow and I am hoping for a better way. I asked a previous question (but not very clearly so I was advised to create a new question), where I was asking if there was a better way to do this in VBA. My question asked about reading and writing large amounts of data all at once in Access through VBA, which I know is a good practice in Excel. That is, I was hoping to take my original table and just assign the entire thing to an array all at once (as in Excel, instead of cell by cell), then work with that array in VBA and create some new array and then write that entire array all at once to a new table (instead of record by record, field by field). From the answers in that question, it seems like that is not really a possibility in Access, but my best bet might be to use some sort of query. I tried the Query Wizard and found the Cross Tab query which is close to what I describe above. But, there appears to be a max of 3 fields used in the Row Heading, whereas here I have 4. And, instead of putting $0.00 when a value is not specified (like C, D, F, G in my example above), it just leaves a blank.</p> <p><strong>Update (in response to Remou's comment to give sample data):</strong> Here is some sample data.</p> <pre><code>ID a b c d e f 7 1 2 3 5 A 5 8 1 2 3 5 B 10 9 1 2 3 5 C 15 10 1 2 3 5 D 20 11 1 2 3 5 E 25 12 1 2 4 4 A 16 13 1 2 4 4 B 26 14 1 3 3 7 D 11 15 1 3 3 7 B 11 </code></pre> <p>The result should be:</p> <pre><code>a b c d an bn cn dn en Total 1 2 3 5 5 10 15 20 25 75 1 2 4 4 16 26 0 0 0 42 1 3 3 7 0 11 0 11 0 22 </code></pre> <p>But, when I copy and paste the SQL given by Remou, the only output I get is</p> <pre><code>a b c d an bn cn dn en 1 2 3 5 5 10 15 20 25 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload