Introduction - Connecting To The Database
Introduction - Connecting To The Database
Beware of bugs in the above code; I have only proved it correct, not tried it.
Create a connection to Access database using Visual Basic.
To display data from a database in bound control on a form, you need to define a connection, a data
adapter and a data set.
Create the following database:
Save the database with the extension name (?? .mdb)
Example: Sun.mdb
Create the following table named Customer.
Open a connection to Access Database
A connection establishes a link to a data source which is a specific file or server.
In Server Explorer, right click Data Connection.
P a g e | 2
Beware of bugs in the above code; I have only proved it correct, not tried it.
Click on Provider tab Choose
And click NEXT button.
Next choose Connection tab and click the ellipses button ()
Get the database from your pen drive or the location where you saved the database.
Click on the button Test Connection to check if it has succeeded.
Then click OK. You will see the connection on the Server Explorer.
P a g e | 3
Beware of bugs in the above code; I have only proved it correct, not tried it.
Retrieve data from the Access database using OleDbDataAdapter.
It performs all the work of passing data back and forth between a data source and a program.
Data source can be a
Database
Text file
Object
On the toolbox, click DATA TAB.
Drag the OleDbAdapter to Form1.
Once Data Adapter Wizard Opens, click Next three times.
In Generate SQL statements panel, type the following:
SELECT *
FROM customer
Then click the FINISH button and click dont include password.
P a g e | 4
Beware of bugs in the above code; I have only proved it correct, not tried it.
Datasets
It is a temporary set of data stored in the memory of the computer.
To define a dataset
Next, follow the diagram as shown below:
Right click the OleDbDataAdapter1 and choose Generate Dataset.
When the following screen appears, Then click OK.
Fill Method
To fill the dataset at run time, you must execute the FILL method of the data adapter and this is
added to the FORM_LOAD event.
DataAdapterName.Fill(DataSetName)
Next, double click on Form1 and type the following code.
P a g e | 5
Beware of bugs in the above code; I have only proved it correct, not tried it.
Private Sub Form1_Load() Handles MyBase.Load
OleDbDataAdapter1.Fill(DataSet11)
End Sub
Binding data to Controls.
To bind dataset to controls, you set a few properties of the controls.
If you are binding to a grid, set the grids
DataSource property: to the name of the dataset and
Datamember property: to the name of the table.
Display record that are retrieved from Access Database
Add datagrid to Form1 Choose Toolbox
Right click datagrid1 and click Properties.
In the dialog box, change the properties as follows.
Click the Execute button to run the program.
P a g e | 6
Beware of bugs in the above code; I have only proved it correct, not tried it.
Modify Grid
Switch back to form
Right Click on grid and choose AutoFormat
You will be presented with a set of options as shown below.
Display a Data Pre-View
Right click on DataAdapterName and choose Preview Data
P a g e | 7
Beware of bugs in the above code; I have only proved it correct, not tried it.
Click on Fill Dataset to preview table results.
Binding Data
Simple Data Binding connects one control to one data element.
Creating a simple screen as shown below:
Begin a new project and create a connection to your database.
In the server connection, expand the Customer table and choose only the required fields
such as ID, FirstName and LastName (use SHIFT button to make selection)
P a g e | 8
Beware of bugs in the above code; I have only proved it correct, not tried it.
Drag the selected fields to the form to create a connection and data adapter components and
choose Dont include password.
Select the data adapter and generate the dataset.
Bind the data fields.
For the Combobox, set the properties as shown below.
Then select the Customer First Name label, expand the Data Binding property and set the properties
as below.
Select Text, expand the table Customer and choose Customer First Name.
Repeat the same steps for Customer Last Name.
Write Code.
Double click the form and type
OleDbDataAdapter1.Fill(DataSet12)
Save and run the program.
Problems you may encounter:
If data values in labels do not change to match selection in combo box, check the combo box
bindings.
P a g e | 9
Beware of bugs in the above code; I have only proved it correct, not tried it.
You can modify the SQL statements in an adapter by right clicking the adapter and choosing
Configure Data Adapter, click next twice and choose SQL statements. Modify statements as
required.