Visual Basic: Fill a listbox with list of Access database tables

Platform:Visual Basic
Task:Fill a listbox with list of Access database tables
Discussion:
The code below will fill a listbox named lstTables with all the tables in an access database (DB)
Example:
    Private Sub ListTables(ByVal DB As String)
        Dim userTables As DataTable = Nothing
        Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
        ' c:\test\test.mdb
        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DB
        ' We only want user tables, not system tables
        Dim restrictions() As String = New String(3) {}
        restrictions(3) = "Table"
        connection.Open()
        ' Get list of user tables
        userTables = connection.GetSchema("Tables", restrictions)
        connection.Close()
        ' Add list of table names to listBox
        Dim i As Integer
        For i = 0 To userTables.Rows.Count - 1 Step i + 1
            lstTables.Items.Add(userTables.Rows(i)(2).ToString())
        Next
    End Sub
Back to Index