VB6


Creating Group Reports in VB6 using Data Report
By Richard Clark
16-Jan-10
Views: 36641

It is often required in database applications to have a report which shows data in a hierarchal fashion. It is a little tricky in Visual Basic to create such reports. This tutorial will help you create such a report. Before you continue reading this tutorial. Download a zip file containing the sample code.
 
Group Reports in VB6 using Data Report (Page 1 of 1)

For this tutorial i have used a Microsoft Access database with two tables 'Categories' and 'Products' with parent-child relationship between them. The fields in each table are shown below

Categories
Creating Group Reports in VB6 using Data Report

Products
Creating Group Reports in VB6 using Data Report

The two tables should have a parent-child relationship. The relation between the two tables i have created is shown below

Creating Group Reports in VB6 using Data Report

This is all we need from Microsoft Access. Now start a new data project in Visual Basic. You should have a form and data report added to your project. Open the report right click in the white area and select 'Insert Group Header/Footer'

Creating Group Reports in VB6 using Data Report

Add fields from the parent table in the group header section and fields from the child table in the data section of the report. Set the [DataMember] property of products field to "Products". Leave the [DataMember] property of parent fields to blank. The report should look similar to the one shown below.

Creating Group Reports in VB6 using Data Report

Open the form code window and add the following code

Dim cn As New ADODB.Connection
Private Sub Command1_Click()
    Dim rsTemp As New ADODB.Recordset
 
    rsTemp.Open "SHAPE {Select Categories.CategoryId,Categories.CategoryName from Categories} as Categories APPEND ({Select * from Products} Relate CategoryId to CategoryId) as Products", cn
    Set rptProducts.DataSource = rsTemp
    rptProducts.WindowState = vbMaximized
    rptProducts.Show
 
End Sub
Private Sub Form_Load()
    cn.ConnectionString = "Provider=MSDataShape.1;Data Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\db.mdb"
    cn.Open
End Sub
 

What the above code does?
First of all you need to add "Provider=MSDataShape.1;" to the connection string otherwise the Shape command in the query will not work. The Shape command is the crux of creating group reports in visual basic. It shapes up hierarchal data and formats it to be used by the data report. Thats all in this tutorial. If you have any problems please discuss below and also download the files included with this tutorial

Download Sample Code

This tutorial is about creating buttons which look similar to Facebook buttons. . Read More Facebook Button CSS

Comments
alex espinola
[19-Feb-2010]
#1

Hi, try your codes on how to group datareports in VB6. unfortunately i got these: runtime error '-2147217900(80040e14)'; invalid SQL statement; expected 'DELETE,INSERT,PROCEDURE,SELECT, or UPDATE'. I not actually a programmer but I have a little bit knowledge on VB6 reporting. My main problem is how to group the report.just like your example here. Its easy to groupby using crystal report but I prefer to use datareport in VB6. Can I ask you assistance on this matter.thank you very much.

Nadeem Kazmi
[21-Feb-2010]
#2

Dear Richard It is nice sample for group based data report. I am also facing some issue related to data report like.. #How to put serial no in the detail section. # How to give subtotal and total of the group numeric field if any exiting there. #how can we apply some user defined function for decoding the Flag, we maining in DBLevel like I- In patient , O-Out Patient etc.. Hope will get response asap. Thanx & regards Nadeem

mr_boombastic
[30-Sep-2011]
#3

...that's good man! !

Nabeel
[18-Oct-2011]
#4

Sir.

Please help me. I want to create a global report in visual basic. I mean. I have many tables and i want to get one day report of all tables with one query in the one report....please help me urgently

anyone can help me im waiting...

nabeelsubhani@yahoo.com

 

mohd swaleh
[15-Feb-2012]
#5

i am also facing this type of problem, but there is some difference in my problem, i want to show data of two table in detail section, while both table has repeating data. plz help me, it is very urgent

mosab abdo
[17-Jun-2012]
#6

Thank you very much , you have taught me the basic of group header

since 2010

now i am profisional 15/6/2012

Nelson
[28-Feb-2013]
#7

I am using MySQL as my database so i have a different data provider. is it possible to have a group on data report using mysql data provider?

if so, what would be the query look like?

thanks..

 

dons.harry@yahoo.com

vijayraj34
[04-Mar-2013]
#8

Hi i have little knowledge in VB.

I am using ms access 2010 as my database. i want to generate report containg last inserted record from database (id, name and address).

it should be generated when i press finish button in VB form. i dono how to do all that. am using Visual Basic 2008.

Please help asap.

vijayraj34@gmail.com

 

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Are you human ?

Enter the code shown above