Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Dynamic Data Report

Posted on 2002-04-01
Medium Priority
Last Modified: 2007-12-19
The application I am building requires some relatively simple reporting capabilities.  Primarily I need to incorporate the ability to print the contents of a Mshflexgrid when the user requests.

The application is a data mining application where a user could import virtually any file for analysis purposes. As such it is impossible to build a static Data Report.

My first thought was to utilize the Data Report in VB6, but found that it doesn't seem to allow dynamic creation of reports.

I worked with the printer object, but found it sucked particularly if the data went past the margins.  In other words it was too complex to program properly.

The only thing I think of to work here is to dump the contents of the ADO recordset/datasource into a RichTextbox then printing from that.

As a followup, is there a dialog box OCX or something that can be used to set the options of the printer object? IE margins, zoom, etc?  Seems really stupid to have an object like that and nobody built an OCX to set properties.  I already am using the common dialog control to present the typical vanilla print options, but I need to be able to scale the printout to fit a page, set margins, etc. just like every other program on the planet.
Question by:edwinson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 32

Expert Comment

by:Brendt Hess
ID: 6911003
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6911360
>> My first thought was to utilize the Data Report in VB6, but found that it doesn't seem to allow dynamic
creation of reports. <<

Actually you can and you do not even need the DataEnvironment (other than for design purposes)

Take a look at the following articles from MSDN:
HOWTO: Dynamically Populate a Data Report in Visual Basic (Q240019);en-us;Q240019
HOWTO: Dynamically Populate a Group Data Report in Visual Basic (Q289793);EN-US;q289793

On the other hand, it would only be fair to add that the DataReport is not supported in .NET


Author Comment

ID: 6911456

These two references seem OK, but there is a problem.  The text suggests you need to know how many fields there will be in the report during design time.  The code segments then simply process through each of the controls on the report and bind data fields to them at runtime.

This works fine if you already know what the data report will contain.  In my situation, a user could import any file so I don't know how many fields are going to be in the ultimate report during design time.  Therefore I cannot anticipate the number of controls that will be needed.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

Valliappan AN earned 300 total points
ID: 6912628
You can try this:
(Printer control)

(RTF Print Preview)

Hope it helps.
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6913045
Fair point.  There are workarounds, but I suspect you are right in looking elsewhere.


Author Comment

ID: 6913679
I came up with a cheesy solution but still looking.

I built a form with a richtextbox control in it.
I also built a function to create a string to contain the contents from the grid/recordset.

Code in the "preview form"
Grid1 - a reference to the grid containing the data I want to print.  

Private Sub Form_Load()
Dim rst As New ADODB.Recordset, mfield As ADODB.Field
Dim strText As String
Set rst.DataSource = Grid1.DataSource

'just in case this form opens and there is no data in the
If rst.State = 0 Then Exit Sub

'build the string headers
For Each mfield In rst.Fields
  strText = strText & vbTab & mfield.Name
Next mfield
strText = strText & vbCrLf

'now build the field values
Do Until rst.EOF
  For Each mfield In rst.Fields
     strText = strText & vbTab & mfield.Value
  Next mfield
  strText = strText & vbCrLf

 RichTextBox1.Text = strText
 Set rst = Nothing
End Sub

I then use a PrintRTF function when the print is requested:
I gotta tell you this is not exactly what I wanted to accomplish.  It leaves a heck of a lot to be desired.

a.) Printer dialog still hasn't been addressed (ie Page Margins, etc.)
b.) Building all of the stinking options to modify the text etc.!!
c.) all the other typical reporting type things.


Author Comment

ID: 7070590
Provided some good direction on where to solve the problem.

Expert Comment

by:Valliappan AN
ID: 7071821
Thanks edwinson. Did you use the RTF Preview or what?

But then, why Grade B, though it matters not much.

Glad I could help you.

Author Comment

ID: 7072592
I used a combination of things to get a rudimentary function.  

I ended up working with an HTML document object to build a table in HTML.  This way I could be a bit more flexible.

I guess the B was for providing direction on the ultimate solution.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question