You have learned how to build a database in Visual Basic 6 in previous chapters, however you have not learned how to display the saved data in a report.
Reports are important and useful in many respects because they provide useful and meaningful information concerning a set of data. In this chapter, we will show you how to create a report in Visual Basic 6.
In previous versions of Visual Basic 6, there is no primary reporting . Previous versions of Visual basic 6 uses Crystal Reports tool, a software from Seagate. Fortunately, Microsoft has integrated a good report writer into Visual Basic 6, so you no longer need to use Crystal Report.
Now, you need to connect to the database by selecting a database file from your hard disk. For demonstration purpose, we will use the database BIBLIO.MDB that comes with Visual Basic, as shown in Figure 40.4. The path to this database file is C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB. This path varies from computers to computers, depending on where you install the file. After selecting the file, you need to test the connection by clicking the Test Connection button at the right bottom of the Data Link Properties dialog. If the connection is successful, a message that says 'Test Connection Succeeded' will appear. Click the OK button on the message box to return to the data environment. Now you can rename connection1 to any name you like by right-clicking it. For example, you can change it to MyConnection. You may also change the name of DataEnvironment1 to MyDataEnvironment using the Properties window.
Step 3: Retrieving Information from the Database
In order to use the database in your report, you need to create query to retrieve the information from the database. Here , we will use SQL command to create the query. First of all, right click on MyConnection to add a command to the data environment. The default command is Command1, you can rename it as MyCommand, as shown in Figure 40.5.
Figure 40.5: MyCommand
In order to use SQL command, right-click MyCommand and you can see its properties dialog. At the General tab, select SQL statement and key in the following SQL statement:
SELECT Au_ID, Author
FROM Authors ORDER BY Author
This command is to select all the fields from the Authors table in the Biblio.Mdb database. The command ORDER BY Author is to arrange the list in ascending order according to the Authors' Names.
Now, you need to customize a few properties of your data report so that it can connect to the database. The first property to set is the DataSource, set it to MyDataEnvironment.
Next, you need to set the DataMember property to MyCommand,as shown in Figure 40.6
Figure 40.6: Properties of
To add data to your report, you need to drag the fields from MyCommand in MyDataEnvironment into MyDataReport, as shown in Figure 40.7.Visual Basic 6 will automatically draw a RptTextBox, along with a RptLabel control for each field on the report. You can customize the look of the labels as well as the TextBoxes from the properties window of MyDataReport.
The Final step is to set MydataReport as the Startup form from the Project menu, then run the program. You will see your report as shown in Figure 40.8. You can print out your report.
Figure 40.8: The Final Report.
Congratulation! You have finish reading all the 39 lessons, and now you can consider yourself a VB programmer. You should consider buying the TEXTBOOK
for this tutorial for easy referencing in the future. Buy this book by clicking the picture below:
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: email@example.com