I have a query in access like below
select field1, column_date, sum(qty) from table
group by field1, column_date
union all
select field1, 'subtotal' as column_date, sum(qty) from table
group by field1
union all
select '', 'Grand Total' as column_date, sum(qty) from table
The above code displays output as below :
field1 | column_date | qty1 | qty2 | qty3 | qty4
xyz | 1 June 2012 | 23 | 36 | 343 | 45
xyz | 2 June 2012 | 24 | 33 | 123 | 12
xyz | 3 June 2012 | 55 | 25 | 21 | 45
**xyz | subtotal | 102 | 94 | 487 | 102**
abc | 1 June 2012 | 15 | 23 | 46 | 21
abc | 2 June 2012 | 24 | 81 | 25 | 21
abc | 7 June 2012 | 33 | 25 | 43 | 21
**abc | subtotal | 72 | 129 | 114 | 63**
etc......
But I don’t want to repeat field1 values every time. I just want to display once at the top as below.
field1 | column_date | qty1 | qty2 | qty3 | qty4
xyz | 1 June 2012 | 23 | 36 | 343 | 45
| 2 June 2012 | 24 | 33 | 123 | 12
| 3 June 2012 | 55 | 25 | 21 | 45
| subtotal | 102 | 94 | 487 | 102
abc | 1 June 2012 | 15 | 23 | 46 | 21
| 2 June 2012 | 24 | 81 | 25 | 21
| 7 June 2012 | 33 | 25 | 43 | 21
| subtotal | 72 | 129 | 114 | 63**
etc......
Is it possible either with query or with VBA code? Please help.
The OP has expressed a beginner understanding of VBA, so for the others who may use this code, please keep that in mind when reading through and notice the comments in the code.
Please consider this VBA as a possible solution. I have tested this in an access 2007 database. I created a form with a button whose on_click event, calls this subroutine.
Once this executes, you can have the buttons on_click event make a call to a VBA module which exports the data contained in “tbl_output” to an excel spread sheet.
Below is a screen shot which shows proof of it working as requested:
Here is the code that I came up which makes this possible. This is my interpretation of how the end result can be achieved: