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

How to Retrieve Data in the TextBox Based on ComboBox in VB.Net and MS Access Database

BackPimple

Champion
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
48
Likes
40
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Now, in this tutorial, I’m going to teach you how to retrieve data in the textbox based on combobox in VB.Net and MS Access Database. This method has the ability to display all the data in a certain textbox by selecting the data in the combobox. If you this function the output will be displayed in all 5 textboxes.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application.
Step 2

Do the form just like shown below.
2019-07-08_2.png

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB

to access OLeDB

libraries.
  1. imports System.Data.OleDb;

Step 4

Create a connection between the access database and vb.net. After that, declare all the classes that are needed.

  1. Dim

    con As

    OleDbConnection = New

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

    & Application.StartupPath & "\dbproducts.accdb"

    )
  2. Dim

    cmd As

    OleDbCommand
  3. Dim

    da As

    OleDbDataAdapter
  4. Dim

    dt As

    DataTable
  5. Dim

    sql As

    String
  6. Dim

    maxrow As

    Integer

Step 5

Create a method for filling data in the combobox.

  1. Private

    Sub

    load_cbo(sql As

    String

    , cbo As

    ComboBox)
  2. Try
  3. con.Open

    ()
  4. cmd = New

    OleDb.OleDbCommand
  5. da = New

    OleDb.OleDbDataAdapter
  6. dt = New

    DataTable

  7. With

    cmd
  8. .Connection = con
  9. .CommandText = sql

  10. End

    With
  11. da.SelectCommand = cmd
  12. da.Fill(dt)

  13. cbo.DataSource = dt
  14. cbo.DisplayMember = "Product"
  15. cbo.ValueMember = "ProductID"



  16. Catch ex As

    Exception
  17. MsgBox(ex.Message)

  18. Finally
  19. con.Close

    ()
  20. da.Dispose()
  21. End

    Try
  22. End

    Sub


Step 6

Create a function for getting the total number of rows in the database.

  1. Private

    Function

    get_maxrow(sql)
  2. Try
  3. con.Open

    ()
  4. cmd = New

    OleDb.OleDbCommand
  5. da = New

    OleDb.OleDbDataAdapter
  6. dt = New

    DataTable

  7. With

    cmd
  8. .Connection = con
  9. .CommandText = sql

  10. End

    With
  11. da.SelectCommand = cmd
  12. da.Fill(dt)

  13. maxrow = dt.Rows.Count

  14. Catch ex As

    Exception
  15. MsgBox(ex.Message)

  16. Finally
  17. con.Close

    ()
  18. da.Dispose()
  19. End

    Try
  20. Return maxrow
  21. End

    Function

Step 7

Write the following code for filling the data in the combobox in the first load of the form.

  1. Private

    Sub

    Form1_Load(sender As

    Object

    , e As

    EventArgs) Handles MyBase.Load
  2. sql = "Select * From tblproducts"
  3. load_cbo(sql, cboProduct)
  4. End

    Sub

Step 8

Write the following code for retrieving the data in the textbox when the button is clicked.

  1. Private

    Sub

    btnFind_Click(sender As

    Object

    , e As

    EventArgs) Handles btnFind.Click
  2. sql = "Select * From tblproducts Where ProductID = "

    & cboProduct.SelectedValue & ""
  3. maxrow = get_maxrow(sql)
  4. If

    maxrow > 0 Then
  5. With

    dt.Rows(0)
  6. txtProductID.Text = .Item("ProductID"

    )
  7. txtProductName.Text = .Item("Product"

    )
  8. txtDescription.Text = .Item("Description"

    )
  9. txtPrice.Text = .Item("Price"

    )
  10. txtCategory.Text = .Item("Category"

    )
  11. End

    With
  12. End

    If
  13. End

    Sub

Download the complete source code and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below


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

452,292

323,526

323,535

Top