Hi
I have a listbox called lstTableNames, which returns a list of all of the tables in my database. When I select the table in the list box, I want a combo box to return all of the fields in the selected table. For example, if I select “company” (a table in my database) from the listbox, I want the combo box to populate itself with the fields in that table (such as “name”, “Address1”, “Address2”, etc.)
Is there an easy way of achieving this?
Here’s my code to populate the list box…
'Form Load
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\svr1\database\MyDatabase.mdb;Persist Security Info=False" 'DataBase Connection DBConnection.ConnectionString = ConnString
DBConnection.Open()
Call FillTableNames()
DBConnection.Close()
End Sub
'Fill the listbox with the table names in my database
Private Sub FillTableNames()
Dim DBSchema As New OleDb.OleDbSchemaGuid
Dim DBTableNames As New DataTable
DBTableNames = DBConnection.GetOleDbSchemaTable(DBSchema.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
'Filling the ListBox With TableNames and DataSet Table Records
Dim DBTableRow As DataRow
Dim SQLString As String
lstTableNames.Items.Clear()
For Each DBTableRow In DBTableNames.Rows
lstTableNames.Items.Add(DBTableRow.Item(2))
'Index 2 Specifies TableName Column in the Schema Record
SQLString = "Select * from " & CStr(DBTableRow.Item(2))
Dim DBDataAdapter As New OleDb.OleDbDataAdapter(SQLString, ConnString)
DBDataAdapter.Fill(DBDataSet, CStr(DBTableRow.Item(2)))
Next
End Sub
Thanks
Andrew
I have a listbox called lstTableNames, which returns a list of all of the tables in my database. When I select the table in the list box, I want a combo box to return all of the fields in the selected table. For example, if I select “company” (a table in my database) from the listbox, I want the combo box to populate itself with the fields in that table (such as “name”, “Address1”, “Address2”, etc.)
Is there an easy way of achieving this?
Here’s my code to populate the list box…
'Form Load
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\svr1\database\MyDatabase.mdb;Persist Security Info=False" 'DataBase Connection DBConnection.ConnectionString = ConnString
DBConnection.Open()
Call FillTableNames()
DBConnection.Close()
End Sub
'Fill the listbox with the table names in my database
Private Sub FillTableNames()
Dim DBSchema As New OleDb.OleDbSchemaGuid
Dim DBTableNames As New DataTable
DBTableNames = DBConnection.GetOleDbSchemaTable(DBSchema.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})
'Filling the ListBox With TableNames and DataSet Table Records
Dim DBTableRow As DataRow
Dim SQLString As String
lstTableNames.Items.Clear()
For Each DBTableRow In DBTableNames.Rows
lstTableNames.Items.Add(DBTableRow.Item(2))
'Index 2 Specifies TableName Column in the Schema Record
SQLString = "Select * from " & CStr(DBTableRow.Item(2))
Dim DBDataAdapter As New OleDb.OleDbDataAdapter(SQLString, ConnString)
DBDataAdapter.Fill(DBDataSet, CStr(DBTableRow.Item(2)))
Next
End Sub
Thanks
Andrew