Public Class Form1 'set up connection to MS Access database Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _ Application.StartupPath & "\datastore.accdb") 'represent the set of commands and a database for filling the dataset and update the datatsource. Dim da As New OleDb.OleDbDataAdapter 'contains basic elements of the database such as table , indexes ,keys and the relation of the tables Dim ds As New DataSet 'variable string, where i put a query oon it. Dim sql As String End Class
Form_load
and set up a code for filling the ComboBox on the first load.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try 'open the connection con.Open() 'set your query to a variable(sql) sql = "Select * From tbllist" 'set new dataset because every time you call the Form_load it execute new dataset in the database ds = New DataSet da = New OleDb.OleDbDataAdapter(sql, con) 'fill and add or refreshes rows in the dataset da.Fill(ds, "tbllist") 'set your datasource to combobox ComboBox1.DataSource = ds.Tables(0) 'gets or set the property to display in the listcontrol ComboBox1.DisplayMember = "list" Catch ex As Exception End Try 'close the connection End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try 'open the connection con.Open() 'set your query to a variable(sql) sql = "INSERT INTO tbllist (list) Values ('" & TextBox1.Text & "')" da = New OleDb.OleDbDataAdapter(sql, con) 'fill and add or refreshes rows in the dataset da.Fill(ds) Catch ex As Exception End Try 'close the connection 'call the form_load to refresh the records in the combobox. Call Form1_Load(sender, e) End Sub