This tutorial is in addition to my previous tutorial on using the MyODBC Connector, and uses the MyStore database. I will attempt to kill two birds with one tutorial: creating reports within Visual Basic.NET and modifying queries to the MySQL DataTables.
Although the focus of this tutorial is on integrating Visual Basic.NET with MySQL, much of the material in this tutorial can be applied to any use of the ReportViewer control.
It took me awhile to figure out both, and – especially in the case of creating reports – I found other tutorials hard to follow. After fighting my way through it, I created this tutorial so I would not forget.
This tutorial assumes you have completed the MyODBC Connector Databinding tutorial and have a working version of the MyStore database.
As in my previous tutorial, I do not spend a great deal of time on esthetics. Rather, I focus on functionality (getting it to work).
I intend to create a very simple report that lists all employees by the store in which they work. First, I will create a report that shows employees for all stores, and then I will create a report that only shows the employees for the selected store.
This first step is optional in most cases. When creating the report, one could simply drag and drop fields from existing DataTables. However, I have found this approach difficult in complex reports, and have found it easier to create a single DataTable that contains all information I need in the report.
For the purpose of this tutorial, I will keep the report design to a minimum, but it is not difficult to see how very complex reports can be created with a little practice.
Here is a link for a great tutorial on some of the grouping topics discussed in this section.
Setting up the Report Viewer – at least for this simple report – is quite easy.
The final step is to create a button on Form1 that brings up Form2 with the Report Viewer.
Dim rpt As New Form2 rpt.Show()
There are several ways of filtering a report to view the records you want to see. I will use a method that creates a new query and passes parameters in order to only view information for the selected store. In order to do this, I need to select a parameter I can use to select a store.
The obvious choice of a filter is storeID, as it is the Primary Key of the store table. Therefore, we need a way of selecting the storeID.
AND (store.storeID = ?)
Although it is possible to write code to use the same Report Viewer (Form2), I will create a new Report Viewer (Form3). However, there is no need to create a new report, as Report1 contains all the data fields and bindings we need.
Me.DataTable1TableAdapter.FillByStoreID(Me.mystoreDataSet.DataTable1, storeID)
Sub New(ByVal s As Integer)
Try initializeComponent() storeID = s Catch ex As Exception MsgBox("Error: " & ex.Message) End Try
End Sub
The final step is to create a button on Form1 that brings up Form3 with the Report Viewer, and passes the storeID to it.
Dim rpt As New Form3(Me.ComboBox1.SelectedValue) rpt.Show()
This tutorial demonstrated several Visual Basic.NET features: