Storing Data and Autocomplete ComboBox

For today, I will teach you how to create an Autocomplete ComboBox and Store data in MS Access Database. In doing the Autocomplete ComboBox there’s no need for you to dropdown the ComboBox every time you search for your list. All you have to do is type the initial letter in the ComboBox that you’re going to search and the records will automatically dropdown. And that’s how easy it is. So, let’s begin. 1. Open Visual Basic 2008 2. Create a project. 3. Create a windows Form. 4. Set up your form just like this. First Form Click the ComboBox , go to the properties, click the AutocompleteMode and select the SuggestAppend. Then click the AutoCompleteSource and select the ListItems. Second Form After setting up the properties of the ComboBox double click the Form and do this code.
  1. Public Class Form1
  2. 'set up connection to MS Access database
  3. Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
  4. Application.StartupPath & "\datastore.accdb")
  5.  
  6. 'represent the set of commands and a database for filling the dataset and update the datatsource.
  7. Dim da As New OleDb.OleDbDataAdapter
  8. 'contains basic elements of the database such as table , indexes ,keys and the relation of the tables
  9. Dim ds As New DataSet
  10. 'variable string, where i put a query oon it.
  11. Dim sql As String
  12. End Class
Go to Form_load and set up a code for filling the ComboBox on the first load.
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. Try
  3. 'open the connection
  4. con.Open()
  5. 'set your query to a variable(sql)
  6. sql = "Select * From tbllist"
  7. 'set new dataset because every time you call the Form_load it execute new dataset in the database
  8. ds = New DataSet
  9. da = New OleDb.OleDbDataAdapter(sql, con)
  10. 'fill and add or refreshes rows in the dataset
  11. da.Fill(ds, "tbllist")
  12. 'set your datasource to combobox
  13. ComboBox1.DataSource = ds.Tables(0)
  14. 'gets or set the property to display in the listcontrol
  15. ComboBox1.DisplayMember = "list"
  16.  
  17.  
  18. Catch ex As Exception
  19. MsgBox(ex.Message)
  20. End Try
  21. 'close the connection
  22. con.Close()
  23. End Sub
Go back to the Design Views, double click the Save Button and do this code for saving your data.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Try
  3. 'open the connection
  4. con.Open()
  5. 'set your query to a variable(sql)
  6. sql = "INSERT INTO tbllist (list) Values ('" & TextBox1.Text & "')"
  7.  
  8. da = New OleDb.OleDbDataAdapter(sql, con)
  9. 'fill and add or refreshes rows in the dataset
  10. da.Fill(ds)
  11.  
  12. Catch ex As Exception
  13. MsgBox(ex.Message)
  14. End Try
  15. 'close the connection
  16. con.Close()
  17. 'call the form_load to refresh the records in the combobox.
  18. Call Form1_Load(sender, e)
  19. End Sub
Complete Source Code is included. Download and run it on your computer.

Add new comment