Changes

Jump to: navigation, search

Accessing Databases Using Visual Basic

232 bytes added, 19:32, 9 August 2007
no edit summary
Begin by launching Visual Studio and creating a new Windows Application project called "VBdatabase". Once the new project has been created the first task is to connect to the Northwind database. Click on the ''View'' menu and select ''Server Explorer''. The Server Explorer panel will appear on the left hand side of the main Visual Studio area:
[[Image:vb_server_explorer.jpg| Visual Studio Server Explorer]]
This panel allows you to browse servers on your network and locate databases. Click on the ''Connect to Database'' button (highlighted above). The ''Add Connection'' dialog will appear as follows:
[[Image:vb_connect_to_database.jpg| Visual Studio Connect to Database]]
Change the ''Data source'' field to ''Microsoft Access Database'' if necessary. Click on the ''Browse'' button and locate the ''Northwind.mdb'' database. Once you have selected the Northwind database click on the ''Test Connection'' button to verify the database is accessible. Assuming the connection is successful, click on the ''OK'' button to apply the connection.
Once the database connection is established, the next step is to select the data source to be used in the application. To achieve this, select the ''Data->Add New Data Source'' menu option. The Data Source Configuration Wizard will appear as follows:
[[Image:vb_data_source_selection.jpg| Visual Studio Select Data Source]]
Since we are selecting data from a database, make sure that ''Database'' is highlighted in this dialog and click ''Next''. Ensure that the Northwind database is listed in the following screen and press ''Next'' to proceed.
The next step in our tutorial is to add some data fields to the our application form. The first task is to display the Data Sources Panel by selecting the ''Data->Show Data Sources'' menu option. The data source panel will subsequently appear listing all the data tables and columns that are available to us. Click on the '+' sign next to the ''Customer'' table to unfold the list of customer fields:
[[Image:vb_data_sources.jpg| Visual Studio Data Sources Panel]]
We are now ready to add some data columns to our form. Drag and drop the ''CustomerID'', ''CompanyName'', ''Address'' and ''Country'' fields onto the form so that they appear as follows:
[[Image:vb_database_fields.jpg| Visual Basic Database Example]]
Once the data fields are added you will notice that Visual Basic adds a toolbar to the top of the form to provide navigation and the ability to add and remove records from the database. Press ''F5'' to build and run the application. When the application starts the fields will populated with data from the database. Use the toolbar to move through the records:
[[Image:vb_database_application_running.jpg| Visual Basic Database Example]]
You have created your first database driven Visual Basic application.
In the Query Builder dialog, scroll down the table until you reach the ''Country'' column. Scroll horizontally until you reach the ''Filter'' field and enter ''France'' as the filter criteria:
[[Image:vb_database_query_builder.jpg| Visual Studio Query Builder]]
Click on OK. Visual Studio will generate the appropriate SQL statement to perform this query.

Navigation menu