Reginald
Reginald

Reputation: 23

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.

Upvotes: 0

Views: 266

Answers (2)

onedaywhen
onedaywhen

Reputation: 57023

First, 'fix' the design to make things it easy for your users:

CREATE VIEW People ( family_name, first_name ) AS
SELECT LastName, FirstName
  FROM TableA
UNION
SELECT SurName, FirstName
  FROM TableB;

Then the query becomes obvious:

SELECT *
  FROM People
 WHERE family_name ALIKE 'Reg%';

Upvotes: 1

Thollitez Turqueza
Thollitez Turqueza

Reputation: 14

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

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

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

Upvotes: 0

Related Questions