• We just launched and are currently in beta. Join us as we build and grow the community.

Storing Data and Filling the ComboBox with Two Display Members

normal223333

Funny Flash Master
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
138
Likes
129
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.

storeform.jpg


Double click the Form and do this code for the connection of MS Access Database to Visual Basic 2008 above the Form1_Load

.

  1. 'connection of MS Access Database to Visual Basic 2008
  2. Dim

    con As

    OleDb.

    OleDbConnection

    =

    New

    _
  3. OleDb.

    OleDbConnection

    (

    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="

    _
  4. &

    Application.

    StartupPath

    &

    "\test.accdb"

    )

  5. 'declaring the classes
  6. 'OleDbDataAdapter represents a set of data command and
  7. ' the database that are use to update and fill the data source
  8. Dim

    da As

    OleDb.

    OleDbDataAdapter
  9. Dim

    ds As

    DataSet 'represent a cache in memory of data

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 Form1_Load

, do this code for filling the data in the ComboBox that has two display members.

  1. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. 'filling the data of a combobox in the first load
  3. 'openning the connection
  4. con.

    Open

    (

    )
  5. 'in this area, it is where the command happens for filling
  6. 'and updating data in the database
  7. 'and also the concatenation of a query.
  8. da =

    New

    OleDb.

    OleDbDataAdapter

    (

    "SELECT ID,(fname & ' ' & lname) as [Fullname] from member "

    , con)
  9. ds =

    New

    DataSet

  10. 'refreshes the row into the dataset
  11. 'to match those data in the table
  12. da.

    Fill

    (

    ds, "test"

    )
  13. 'defining what are the attributes of a combo box
  14. With

    ComboBox1
  15. 'set the data source to this combo box
  16. .

    DataSource

    =

    ds.

    Tables

    (

    0

    )
  17. 'set the fields of the table to display in a list control
  18. .

    DisplayMember

    =

    "Fullname"
  19. 'set the actual value of a combo box
  20. .

    ValueMember

    =

    "ID"
  21. End

    With
  22. 'closing the connection
  23. con.

    Close

    (

    )
  24. End

    Sub

And this code is for storing the data in the database.

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. 'for data storing
  3. 'openning connection
  4. con.

    Open

    (

    )
  5. da =

    New

    OleDb.

    OleDbDataAdapter

    (

    "INSERT INTO member (fname,lname) VALUES ('"

    &

    TextBox1.

    Text

    &

    "','"

    &

    TextBox2.

    Text

    &

    "')"

    , con)
  6. ds =

    New

    DataSet
  7. da.

    Fill

    (

    ds)
  8. 'closing the connection
  9. con.

    Close

    (

    )
  10. 'calling the first load to refresh the record in the combo box
  11. Call

    Form1_Load(

    sender, e)
  12. End

    Sub

The database of this file is in the bin. You can download the complete Source Code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,517

323,526

Top