Do not use on any
shared computer
August 30, 2008 03:27am pdt
 
[x]
Attachment Details

Create Report from VBA Code

Tags: vba, report, create
I'm at the point where I'd pay someone good money out of my own pocket to make this work.  -_-  I have a crosstab query that has different numbers of columns depending on one field in the query, called "Sequence".  Depending on the value of Sequence (an integer), there could be anywhere from 3 to 15 columns.

I'm trying to create a report from VBA code using DAO to find out how many columns there will be, and then adding the code to add the extra fields in the Detail section of the module behind the Report as well as name the columns (they will be different in each report depending on the "Sequence").  There are a few "common" text fields that the report will have regardless of the value of sequence.  Problem is, I get the error "You must be in Design view to create or modify controls."  So I tried hard coding the control creation into a module, limiting myself to 20 columns.  The code is starting to get pretty darn ugly, in my opinion, and I still think using this method I'll be unable to accomplish my goal.

I've been given other "solutions" to this problem which basically entails having columns that are hidden depending on the results of the query.  This is fine when you run a report that is using, say, names of months or something that is already set.  The wrinkle here is that the names of the columns change also; they are NOT the same from Sequence to Sequence, so having a standard report and then programmatically hiding columns will not work for me.

So I decided to come here and ask the question, how do I programmatically add controls to a report based on the information from a query in the setup I've described above?  I'd post the code I have so far but there's a lot of it and I don't think it would help.  If you know what I'm doing then I'm guessing you probably won't need to see it.

Here's the code behind the module when the Sequence needs three columns:

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Set lblLabel1 = CreateReportControl(strReportName, acLabel, acDetail, , , 60, 60, 5100, 600)
lblLabel1.Name = "lblLabel1_Label"

Set ctlText1 = CreateReportControl(strReportName, acTextBox, acDetail, , "", Left:=(intDataX), Top:=intDataY)
ctlText1.Name = "ctlText1"

Set lblLabel2 = CreateReportControl(strReportName, acLabel, acDetail, , , 60, 60, 5100, 600)
lblLabel2.Name = "lblLabel2_Label"

Set ctlText2 = CreateReportControl(strReportName, acTextBox, acDetail, , "", Left:=(intDataX), Top:=intDataY)
ctlText2.Name = "ctlText2"

Set lblLabel3 = CreateReportControl(strReportName, acLabel, acDetail, , , 60, 60, 5100, 600)
lblLabel3.Name = "lblLabel3_Label"

Set ctlText3 = CreateReportControl(strReportName, acTextBox, acDetail, , "", Left:=(intDataX), Top:=intDataY)
ctlText3.Name = "ctlText3"
End Sub

That's where I get that error about being in Design View.

I would happily give all my points to anyone that can help me arrive at a solution to this.  I left out the references to the values of the Text fields; that will be my next "thingy" to tackle, passing the information in the recordset from one module to the other.  Of course, ignore the positional elements in the code as well, they change as I work on formatting.

To get the points I expect to be given enough information to be able to create a report that will have a variable number of columns.  What would really be extra-nifty is if the report would be able to work based on grouping by "Sequence", such that based on the query behind the report when the Sequence number changed, the report would be able to switch the number of columns.
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Microsoft
Question Asked By: MattGreer
Solution Provided By: heer2351
Participating Experts: 2
Solution Grade: A
Views: 138
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by heer2351

Rank: Genius

Accepted Solution by heer2351:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Assisted Solution by joekendall

Rank: Guru

Assisted Solution by joekendall:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by joekendall

Rank: Guru

Expert Comment by joekendall:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by MattGreer
Author Comment by MattGreer:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Administrative Comment by jadedata

Rank: Genius

Administrative Comment by jadedata:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Administrative Comment by GhostMod
Administrative Comment by GhostMod:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34