This tutorial is all about How to Get MS Access Table Fields or Columns in VB.Net. In this Tutorial you will learn How to Get MS Access Table Fields or Columns in VB.Net. So let’s get Started:
- First is that you have created a MS Access Database file and it should have been put in to the Bin Folder of your Visual Basic Project.
- Open the Visual Basic, Select File on the menu, then click New and create a new project.
- Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK
- After that, design your form like this just like what I’ve shown you below.
Add a Listbox and a Button from the Toolbox.
- Add this following Codes before the Public Class Form1 Line.
[vbnet]
Imports System.Data.OleDb
[/vbnet] - Next, Add this following Declarations below the Public Class Form1 Line.
[vbnet]
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection
[/vbnet] - Finally, add this code to the Button.
[vbnet]
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\Clive\Documents\Visual Studio 2008\clive projects\TableRecordsinCombobox\TableRecordsinCombobox\bin\Debug\Datalist.accdb”
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Try
Dim Rest() As String = {Nothing, Nothing, “tblname”, 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)
NextmyConnection.Close()
Catch ex As Exception
End Try
End Sub
[/vbnet]
Click F5 to run the program.
If you have any comments or suggestions about on How to Get MS Access Table Fields or Columns in VB.Net, please feel free to contact our webpage.