Reginald Reginald - 1 year ago 88 Question

VB.NET - Is there a way to get the name of one column in an MS Access table for a query used on any table?

I am trying to get the name of a single column in a table and use that in a query for sorting.

As an example of what I'm trying to achieve would be as follow:

TableA has 2 columns: "FirstName" and "LastName".

I make a query to

SELECT * FROM TableA WHERE LIKE [column 2 (LastName)]
(don't take this query too literally, please)

Then there is TableB with 2 columns as well: "FirstName" and "SurName". Second column name is different in both tables as seen.

I would have to change the query in the code to that of the column "SurName" for TableB.

I was wondering if there was a way to call out a column in any table and maybe store it in a variable and use it on the query so that it may be used "universally" by any tables that is used on?

I did search and came up with results of getting all the column names in a table. I'm trying to look for only one column name.

Thank you in advanced.

Answer Source
You can call a specific column name from a datatable.
It means, you will have to pass the informations first from db to datatable.

Dim Con as New SqlConnection("ur connection str")
Dim da as New SqlDataAdapter("Select * from [table]",Con)
Dim dt as New Datatable

Me.Label1.Text = dt.Columns(1).ColumnName

Hope it helps.. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download