Selecting database tables name and retrieve all records

zemier

New member
Joined
Oct 16, 2011
Messages
2
Programming Experience
Beginner
I have my combobox retrieve all my database tables in MS access 2010, now i want to retrieve all the records in a Listview when i select a table name
Below is my code:
i'm getting Syntax error. Incomplete query clause


Dim tableName As String = ComboBox1.SelectedIndex
strSQL = "SELECT * FROM '" + tableName + "'"
Dim dbcon As OleDb.OleDbConnection
dbcon = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\mydb.accdb")
dbcon.Open()


cmd = New OleDb.OleDbCommand(strSQL, dbcon)
odr = cmd.ExecuteReader()
ListView1.Items.Clear()


Do While odr.Read()
a = (odr.Item("record_no").ToString())
b = (odr.Item("name").ToString())



Dim lv As ListViewItem = ListView1.Items.Add(a)
lv.SubItems.Add(b)



Loop
odr.Close()
cmd.Dispose()
Connect.Close()
 
Actually, your ComboBox doesn't retrieve anything. It just displays what you retrieve using ADO.NET.

As for the issue, it's most likely because you are wrapping your table name in single quotes. Single quotes are for text, not identifiers.
 
G'd Afternoon Zemier,
To get the table names from your combobox just use ComboBox1.text. To fill your listview could be alittle more elaborated, since not all tables have the same number of columns, you will need to get the columns first, the number, name and type, then you will need to add dinamically each column to your listview.
I don't know how you plan to get the tables' structure. In the old days previous to ms access 2007, we use to do it with the ADOX Library. If you need help with that part let us know.
G'd luck
 
Back
Top