Friday, July 29, 2011

The Database Wizard in VB NET Express

Let's make a start on our Database project. So, once you have your VB software open, do the following:

  • Click File > New Project from the menu bar
  • Select Windows Application, and then give it the Name AddressBook. Click OK
  • Locate the Solution Explorer on the right hand side (If you can't see it, click View > Solution Explorer from the menu bar, or View > Other Windows > Solution Explorer in version 2010.) (View Image)


  • We need to select a Data Source. So click on Data Sources at the bottom of the Solution Explorer in version 2008 (View Image)


If you have VB NET 2010 then the Data Source tab is on the left, just below the Toolbox (View Image)

To Add a New Data Source, click on the link. When you do, you'll see a screen welcoming you to the Data Source Configuration Wizard, Just click Next, to get to the screen (View Image)
 


You want to connect to a Database. So select this option, and click Next. In version 2010 of VBN NET, you'll see this screen appear (you won't see it if you have version 2008) (View Image)

Select DataSet and click Next.

In both versions 2008 and 2010, you'll then see this screen (View Image)



Click the New Connection button and another dialogue box pops up (View Image)



Click the Change button, because we want to connect to an Access database. (The default is for a SQL Server database.) When you click Change, you'll see this (View Image)



Select Microsoft Access Database File, then click OK. The previous dialogue box will then look like this (View Image)



Click the Browse button and navigate to where on your computer you downloaded our Access Database called AddressBook.mdb. Click Test Connection to see if everything is OK, and you'll hopefully see this (View Image)



Click the OK button, then click the OK button on the Add Connection dialogue box as well. You will be returned to the Data Source Configuration Wizard, which should now look like this (View Image)



Click Next to move to the next step of the Wizard. You may see a message box appear, however. Click No on the message box to stop VB copying the database each time it runs. You should then see this (View Image)



Make sure there's a tick in the box for "Save the connection", and then click Next (View Image)


Here, you can select which tables and fields you want. Tick the Tables box to include them all. You can give your DataSet a name, if you prefer. Click Finish and you're done.

When you are returned to your form, you should notice the Solution Explorer has added your new Data Source (View Image)



The Data Sources area of the Solution Explorer now displays information about your database. Click the plus symbol (an arrow in VB NET 2010) next to tblContacts (View Image)



All the Fields in the Address Book database are now showing.

To add a Field to your Form, click on one in the list. Hold down your left mouse button, and drag it over to your form (View Image)



In the image above, the FName field is being dragged on the Form. Your mouse cursor will change shape.


When your Field is over the Form, let go of your left mouse button. A textbox and a label will be added. There are two other things to notice: a navigation bar appears at the top of the form, and a lot of strange objects have appeared in the object area at the bottom (View Image)



We'll explore the Objects in a later section. But notice the Navigation bar in blue. Run your programme by hitting the F5 key on your keyboard. You should see this (View Image)



Click the Navigation arrows to scroll through the database. When you've played around with the controls, stop the form from running, and return to Design View.


Drag and Drop more Fields to your form. But don't align them yet. We'll see an easy way to do this. But once you've dragged the fields to your form, it might look like this (View Image)



I'm sure you'll agree - that's a very untidy form. But there's a very easy way to align all your controls. Try this:

  • Click on a Textbox and its label with your left mouse button
  • Hold down the Ctrl key on your keyboard, and select a second Textbox and label
  • With the Ctrl key still held down, click each Textbox and label in turn
  • When all Textbox are selected, click on the Format menu at the top
  • From the Format menu select Align > Lefts. The left edges of the Textboxes will align themselves
  • From the Format menu select Vertical Spacing > Make Equal. The space between each textbox will then be the same

With your new controls added, and nicely aligned, press F5 to run your form. For the Notes Textbox, set the MultiLine property to True. Your form might then be something like this (View Image)



Click the Navigation icons to move backwards and forwards through your database.

In the next part, you'll move away from the Wizards and learn how to add your own programming code to open up and manipulate databases.

Source: homeandlearn

Visual Basic.Net Online Programming