Fill Microsoft Access Table Records in Combobox - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This tutorial will describe how to return all records in a Microsoft Access database table and fill it in a ComboBox in Visual Basic .NET

I have the following table:

Fill Access Records in ComboBox

Add a ComboBox to the form

Add the following code before the (Public Class Form1) line:

Imports System.Data.OleDb

Then add the following declarations below the (Public Class Form1) line

   Public myConnToAccess As OleDbConnection
    Dim mySQLCommand As OleDbCommand
    Dim mySQLStrg As String
    Dim ds As DataSet
    Dim da As OleDbDataAdapter
    Dim tables As DataTableCollection

The following is the form load event code that is used to fill the combobox.

It will return the FirstName:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        myConnToAccess = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jimmy\Desktop\test.accdb")
        ds = New DataSet
        tables = ds.Tables
        da = New OleDbDataAdapter("SELECT FirstName from Items", myConnToAccess)
        da.Fill(ds, "Items")
        Dim view1 As New DataView(tables(0))
        With ComboBox1
            .DataSource = ds.Tables("Items")
            .DisplayMember = "FirstName"
            .ValueMember = "FirstName"
            .SelectedIndex = 0
            .AutoCompleteMode = AutoCompleteMode.SuggestAppend
            .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
    End Sub

The user will be will be able to type in the combobox and see result as a dropdown.

Fill combo box in visual basic .net

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.