[RESOLVED] Programmatically fetching table names from MS Access results in Nothing
For some reason I am getting Nothing for the table name that's pulled from an MS Access db. I just opened the file manually, and there is a Table named "log_returns", so why is this coming up as Nothing?
The code below is used to add the found table names to a ListView. Although there is a table in a .mdb I am opening, there are no tables listed in the ListView, and the row count is zero. Is there an issue with the parameters being pulled from the dt?
[CODE] Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data source =" & filename
Dim cn As New OleDbConnection(ConnString)
cn.Open()
Dim restrictions() As String = New String(3) {}
restrictions(3) = "Table"
Dim userTables As DataTable = Nothing
userTables = cn.GetSchema("Tables", restrictions)
Dim ListView1 As New ListView
For i = 0 To userTables.Rows.Count - 1
Dim lvi As New ListViewItem(userTables.Rows(i)(2).ToString())
ListView1.Items.Add(lvi)
Next
Re: MS Access Table name is Nothing, but Table does exist
There's a lot wrong here, both with the code and the question. I'll start with the main issue with the question and deal with the code issues later. You have shown us this line:
vb.net Code:
Dim selectedtable As String = Form_SelectAccessSQLTables.selectedtable
but you have not shown us how you set selectedtable inside Form_SelectAccessSQLTables. For all we know, you never set it to anything and, based on the behaviour you describe, that appears to be the case. Presumably you need to set it based on the selection when someone makes one in the ListView. You need to show us that code because that's almost certainly where the actual issue is.
Re: MS Access Table name is Nothing, but Table does exist
The question was revised, so that it only focuses on a count of zero for the number of rows in the dt.
Re: MS Access Table name is Nothing, but Table does exist
I just used pretty much your exact code and I saw the expected table information. I suggest that you just bind the returned DataTable to a DataGridView, as I did, and see what you see:
vb.net Code:
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data source =" & filename
Dim cn As New OleDbConnection(ConnString)
cn.Open()
Dim restrictions() As String = New String(3) {}
restrictions(3) = "Table"
Dim userTables As DataTable = Nothing
userTables = cn.GetSchema("Tables", restrictions)
DataGridView1.DataSource = userTables
In fact, I suggest not using a ListView at all anyway, if you're using Details view. If you just want to display a list of table names then you can use a ListBox and you can use a DataGridView for one or more columns, either of which is easier and better than a ListView.
Re: MS Access Table name is Nothing, but Table does exist
SOLUTION: The table inside the .accdb file was an imported Excel workbook file (.xlsx) with an Excel icon, so I learned that those won't show up when trying to programmatically list the table. So I right-clicked on the Excel-based table and selected "Convert to Local Table" and then it was converted to an Access table. After this, the table is now visible and the row count in the dt is 1.