How to Create Microsoft Access DataSource for Win Forms App Visual Basic 2005
Visual Studio 2005 provides a feature, the ‘Data Source Configuration Wizard’, for creating a DataSource for a Windows Forms 2.0 application. This article provides step-by-step instructions and a sample Visual Basic 2005 application you can use to learn how make a Microsoft Access database the data source for a Visual Basic Windows Forms application.
The ‘Data Source Configuration Wizard’ creates a DataSource component that can be used to bind controls on a Windows form to database objects such as tables, stored procedures, and views.
This article will serve as ‘step one’ for future articles that will explain how to use a DataSource in a Windows Forms application. It is a generic introduction for creating a Windows Forms DataSource using options and practices that are known to work in the majority of development scenarios. Once you understand the basics you should explore the different options you can take during some of the steps, to understand when and why you may want to deviate from the steps presented below.
Data Source Configuration Wizard
Step-By-Step Instructions for Creating a DataSource
1. Create a Visual Basic 2005 Windows Forms application solution.
2. Add a Microsoft Access database file (.mdb), for example the Microsoft Northwind.mdb file, to the solution’s ‘Bin’ folder.
3. From the Visual Studio menu select: Data -> Add New Data Source…
4. The ‘Data Source Configuration Wizard’ will open. Select ‘Database’ and then click the ‘Next>’ button.
5. The ‘Choose Your Data Source’ step of the wizard will open. Click the ‘New Connection..’ button.
6. The ‘Add Connection’ dialog will open.
Make sure the data source box reads: ‘Microsoft Access Database File (OLE DB)’. If it does not, use the ‘Change…’ button to change the data source.
Next, click the ‘Browse..’. button. Use the ‘Add Connection’ dialog that opens to navigate to, and select, the Microsoft Access database you placed in your application’s ‘Bin’ folder.
7. The path to your database should now be shown in the database file name box. Click the ‘Test Connection’ button to make sure the connection works and then click the ‘OK’ button.
8. You will be returned to the ‘Choose Your Data Source’ step of the wizard. Click the ‘Next>’ button.
9. The ‘Microsoft Visual Studio’ dialog will open. This dialog asks if you would like to copy the file (database file) to your project. Click the ‘Yes’ button.
10. The ‘Save the Connection String the Application Configuration File’ step of the wizard opens. Change the name of the connection string if desired. Click the ‘Next>’ button.
11. The ‘Choose Your Database Objects’ step of the wizard will open. Navigate the objects in your database using the tree view. Click checkbox(es) to select the database objects you desire to use in your Windows Forms application. After you have selected all the objects you desire to bind, click the ‘Finish’ button.
12. The rest of the steps in these instructions are not part of the wizard.
In this step you will set a property to control how Visual Studio uses the Microsoft Access database file each time you build your project.
View the ‘Solution Explorer’ panel in your solution. Right-click the Microsoft Access database you added to the project and select: Properties
The example below shows how to select the database properties for a database named ‘Northwind’.
13. The ‘Properties’ panel will open. In it set the ‘Copy to Output Directory’ property to: Copy if newer.