Make your own free website on Tripod.com

VB6 Something New: The DataReport Designer and the DataEnvironment


The following step-by-step example, demonstrates how to use a parameter based query to create a data report at runtime. For the purposes of this demonstration we use the sample database, Biblio.mdb. We set the parameter value in advance, to the PubID "79", i.e. OSBORNE MCGRAW-HILL, and also the value of the rptLabel in advance to "OSBORNE MCGRAW-HILL". So, in the following example you build a parmeterized query with the DataEnvironment. Next, you create a simple DataReport that is based on your query and bound to the DataEnvironment.

A few important considerations: The DataEnvironment is a cool new tool thatís been incorporated into Visual Basic 6.0. It makes connecting to a database, and manipulating it very, very easy. Itís a great way to do your initial development, and it makes creating reports a relatively simple task. It will speed up your development cycle when you need to create that demonstration program by tomorrow. But, if you want a robust front end, save yourself the overhead and use straight ADO code instead. Itís a tool, not a way of life. Never use a tool to do what code can do better. Or, as a friend of mine says, "It may be called Visual Basic, but it isnít really basic anymore."

Use the DataEnvironment to connect to the Access sample database (Biblio.mdb) that is included with Visual Basic by following these steps:

1. Create a new DataProject in Visual Basic.

2. Set the DataEnvironment1's connection object (Connection1) to use the Microsoft.Jet.OLEDB.3.51 OLE DB provider.

3. Browse to the Biblio.mdb database on your machine, test the connection, and then save it.

4. Add a command (Command1) to Connection1 based on the following query:

SELECT * FROM Titles WHERE PubID = ?

5. Right-click and drag the command onto the DataReport1. For simplicity, delete the label controls, then add a rptLabel (Label1) in Section2, the Page Header. (Note: I do not recommend that you right-click and drag a command from the DataEnvironment on a regular basis; however, for the purposes of this example itís acceptable.)

6. Add a textbox (Text1) to Form1 from the general toolbox.

7. Add a CommandButton (Command1) to Form1 from the general toolbox.

8. Copy, then paste the following code in the General Declarations section of Form1:

Option Explicit
Private Sub Command1_Click()
'The If block closes the recordset if it was previously open before
'running the parameterized query.
     With DataEnvironment1
         If .rsCommand1.State = adStateOpen Then

             .rsCommand1.Close
         End If

' This passes in the value entered into the TextBox.
' In this case, I'm setting the value to "79" in the Form_Load event.
         .Command1 CInt(Text1.Text)

' This If block checks to determine if any records are returned
' by the parameter. Then it shows a report if records are returned.
' Or displays a Message Box if no records are returned.
         If .rsCommand1.RecordCount > 0 Then
             Set DataReport1.DataSource = DataEnvironment1
             DataReport1.Show
         Else
             MsgBox "No Titles found"
         End If
     End With
End Sub

Private Sub Form_Load()
'This sets the initial values of the text in the controls.
     Text1.Text = "79"
     DataReport1.Sections("Section2").Controls("Label1").Caption = "OSBORNE MCGRAW-HILL"
     Command1.Caption = "Show Report"
End Sub

9. Step through the above example (F8). Click on the "Show Report" button. Try entering other values in the text box, ex. "156" is "MACMILLAN" and "188" is "MACMILLAN COLL DIV".


Back to top
Home