Smiley 018
Smiley 018
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
When we use the ADO Data Environment Designer to make our connection, we don't need a Data Control, so let's pretend that we've created a new project, placed the DataGrid in our toolbox, and placed it on our form. Now we need to find and startup the Data Environment Designer. To do that, select Project-Add Data Designer from the Visual Basic Menu Bar
If you don't see Add Data Environment as an option, select Project-Components from the Visual Basic Menu Bar, click on the Designers Tab, and locate and select the Data Environment there
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Once you select Add Data Environment from the Project menu, you should see this window
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Right-click on the Connection object in the Data Environment Window and select Properties
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Look familiar? It should, it's the same wizard we saw when we created the connection using the Data Control. I'll repeat those same steps here (but in the interest of space I won't show you). Now we have a connection, but no recordset. Using the Data Environment, we must create a recordset via a Command Object. To create the Command Object, right-click on the Connection Object and select Add Command from the menu
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
We'll use a SQL Statement to create the underlying recordset (if you are not familiar with SQL, click on the SQL Builder button)
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
and after clicking on the Apply Button, we now have a properly defined Data Environment
At this point, to 'bind' our DataGrid to the Data Environment Object that we've
http://www.johnsmiley.com/cis18.notfree/Smiley018/Smiley018.htm (7 of 12)3/28/2004 11:57:32 AM
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
just created, all we need to do open up the Properties Window for the Data Grid and specify the DataEnvironment as our Data Source and the Command Object in the DataMember property of the Data Grid
Now if we run the program, well find that the DataGrid is now bound to our DataEnvironment
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
At this point, you may be thinking that the DataEnvironment really hasnt bought you all that muchafter all, we can achieve the same functionality with the Data Control. Thats true, but heres something the Data Control can never do for us. Suppose, instead of using a Data Grid to display the records in our Recordset, wed like to display them in Textboxes? Its pretty easy to display data in Textboxes using a Data Controlyou just place labels and Textboxes on a form, and individually set the DataSource and DataField Properties of each Textbox to point to the Data Control. The DataEnvironment makes that even easier. For now, lets delete the Data Grid from this form, and then click and drag the Command Object from the DataEnvironment Designer to our form---watch what happens!
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
As you can see, Visual Basic automatically creates Label and Textbox controls for each field in the Recordset that is produced by the Command Object. This can be a tremendous time saver, especially if the Recordset contains a lot of fields. Lets take some time to make the form a bit tidier
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Not bad for the little work we had to do to get thisbut theres a big problem theres no way to move from record to record, as we have when we use a Data Control. Lets place a command button on the form, and place this code in its Click Event Procedurethis will enable us to move to the next record in the Recordset Private Sub Command1_Click() DataEnvironment1.rsCommand1.MoveNext End Sub Now if we run the program, and click on the Command Button, well easily be able to move to the next record in the Recordset
Use the ADO Data Environment Designer to create an ADO Connection in Visual Basic 6
Summary
I hope you enjoyed this article on using the Data Environment.