Get Access Table Fields or Columns - Visual Basic .NET

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

This tutorial describes how to return all columns or fields in an access table and list them in a listbox in Visual Basic .NET

I have the following Microsoft Access table:

read access columns in visual basic .net

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

Imports System.Data.OleDb

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

    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection

The following is the button click event that will return the columns code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        dataFile = "C:\Users\Jimmy\Desktop\test.accdb"
        connString = provider & dataFile
        myConnection.ConnectionString = connString
        myConnection.Open()
        Try
            Dim Rest() As String = {Nothing, Nothing, "items", Nothing}
            Dim columns As String = "Columns"
            Dim dt As DataTable = myConnection.GetSchema(columns, Rest)
            For Each row As DataRow In dt.Rows
                ListBox1.Items.Add(row.Item("COLUMN_NAME").ToString)
            Next

            myConnection.Close()
        Catch ex As Exception
        End Try
    End Sub

Result

return access fields in visual basic


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.