Storing Data and Filling the ComboBox with Two Display Members
Submitted by janobe on Wednesday, January 8, 2014 - 07:19.
In this tutorial, I will show you how to store and fill data in the combobox with two display members using Visual Basic 2008 and Ms Access Database. Displaying two members in the ComboBox is very easy, all you need is a concatenation function in a query.
Concatenation is a function that joins two or more fields in the table of the Ms Access Database.
The concatenation in the MS Access Database is, you have to put an operator (&) to concat the two or more fields.
And in the MySQL Database, you have to type the word “concat” and put an open and close parenthesis in two or more fields.
You will find out what I’m talking about in the following steps below.
To start with:
Open the Visual Basic 2008, create a project and in the Form. You need to add four Labels, two TextBoxes, to write your records to save it on the two fields of the table. Then, add a button and a ComboBox. In the ComboBox, it is where you display the two fields in the table of Ms Access Database.
Double click the Form and do this code for the connection of MS Access Database to Visual Basic 2008 above the
Description : OleDb is a namespace of a class. Application in the application.startpath is providing the static method to manage the application. Startpath in the application.startpath is getting the executive file.
In the
And this code is for storing the data in the database.
The database of this file is in the bin. You can download the complete Source Code.
Form1_Load
.
- 'connection of MS Access Database to Visual Basic 2008
- Dim con As OleDb.OleDbConnection = New _
- OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
- & Application.StartupPath & "\test.accdb")
- 'declaring the classes
- 'OleDbDataAdapter represents a set of data command and
- ' the database that are use to update and fill the data source
- Dim da As OleDb.OleDbDataAdapter
- Dim ds As DataSet 'represent a cache in memory of data
Form1_Load
, do this code for filling the data in the ComboBox that has two display members.
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'filling the data of a combobox in the first load
- 'openning the connection
- con.Open()
- 'in this area, it is where the command happens for filling
- 'and updating data in the database
- 'and also the concatenation of a query.
- da = New OleDb.OleDbDataAdapter("SELECT ID,(fname & ' ' & lname) as [Fullname] from member ", con)
- ds = New DataSet
- 'refreshes the row into the dataset
- 'to match those data in the table
- da.Fill(ds, "test")
- 'defining what are the attributes of a combo box
- With ComboBox1
- 'set the data source to this combo box
- .DataSource = ds.Tables(0)
- 'set the fields of the table to display in a list control
- .DisplayMember = "Fullname"
- 'set the actual value of a combo box
- .ValueMember = "ID"
- End With
- 'closing the connection
- End Sub
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- 'for data storing
- 'openning connection
- con.Open()
- da = New OleDb.OleDbDataAdapter("INSERT INTO member (fname,lname) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "')", con)
- ds = New DataSet
- da.Fill(ds)
- 'closing the connection
- 'calling the first load to refresh the record in the combo box
- Call Form1_Load(sender, e)
- End Sub
Add new comment
- 268 views