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

Submitted by: 
Visitors have accessed this post 113 times.

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.
ps2

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.  
  2. Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dbproducts.accdb")
  3. Dim cmd As OleDbCommand
  4. Dim da As OleDbDataAdapter
  5. Dim dt As DataTable
  6. Dim sql As String
  7. Dim maxrow As Integer

Step 5

Create a method for filling data in the combobox.

  1.  
  2. Private Sub load_cbo(sql As String, cbo As ComboBox)
  3. Try
  4. con.Open()
  5. cmd = New OleDb.OleDbCommand
  6. da = New OleDb.OleDbDataAdapter
  7. dt = New DataTable
  8.  
  9. With cmd
  10. .Connection = con
  11. .CommandText = sql
  12.  
  13. End With
  14. da.SelectCommand = cmd
  15. da.Fill(dt)
  16.  
  17. cbo.DataSource = dt
  18. cbo.DisplayMember = "Product"
  19. cbo.ValueMember = "ProductID"
  20.  
  21.  
  22.  
  23. Catch ex As Exception
  24. MsgBox(ex.Message)
  25.  
  26. Finally
  27. con.Close()
  28. da.Dispose()
  29. End Try
  30. End Sub
  31.  

Step 6

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

  1.  
  2. Private Function get_maxrow(sql)
  3. Try
  4. con.Open()
  5. cmd = New OleDb.OleDbCommand
  6. da = New OleDb.OleDbDataAdapter
  7. dt = New DataTable
  8.  
  9. With cmd
  10. .Connection = con
  11. .CommandText = sql
  12.  
  13. End With
  14. da.SelectCommand = cmd
  15. da.Fill(dt)
  16.  
  17. maxrow = dt.Rows.Count
  18.  
  19. Catch ex As Exception
  20. MsgBox(ex.Message)
  21.  
  22. Finally
  23. con.Close()
  24. da.Dispose()
  25. End Try
  26. Return maxrow
  27. End Function

Step 7

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

  1.  
  2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3. sql = "Select * From tblproducts"
  4. load_cbo(sql, cboProduct)
  5. End Sub

Step 8

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

  1.  
  2. Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
  3. sql = "Select * From tblproducts Where ProductID = " & cboProduct.SelectedValue & ""
  4. maxrow = get_maxrow(sql)
  5. If maxrow > 0 Then
  6. With dt.Rows(0)
  7. txtProductID.Text = .Item("ProductID")
  8. txtProductName.Text = .Item("Product")
  9. txtDescription.Text = .Item("Description")
  10. txtPrice.Text = .Item("Price")
  11. txtCategory.Text = .Item("Category")
  12. End With
  13. End If
  14. 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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.