473,381 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

Binding Results from UNION Query to Controls

Lyn
Hi,
How do you bind the output columns from a UNION query when the fields from
the two tables have different names? Consider this query (WHERE clauses
omitted)...

SELECT SurnameBirth, FNameBirth, DOB, 'Birth' from tblBirth UNION
SELECT SurnameAlias, FNameAlias, #1/1/100#, 'Alias' from tblAlias;

What I am doing here is searching two tables for a person where the name
given may be the person's original name (at birth) or subsequent changed
name (marriage, etc). There are four fields to be returned from each table
with corresponding data types. The tblAlias does not have DOB so I am
returning a dummy literal value that will be recognised by code. In
addition, the fourth field is another literal that identifies the source of
the data row ("Birth" or "Alias"). The search criteria have been omitted
for simplicity.

I am trying to display the columns returned in text boxes in a listing in
Continuous Mode. The form displaying this data is called by another form
which does the query to produce a public recordset which is then used to set
the Recordset property of the listing form via the Load event.

Because the field names are different, depending on the source of the row, I
have tried to bind the columns to the controls using the generic syntax
"rst.Fields(n)" in an expression. While this syntax is accepted, it results
in a display of "#Name" in each text box.

I have found by trial and error that if I set "tblBirth" as the
RecordSource, I can then bind the controls using the field names from this
table, and it works even with rows returned from tblAlias including the
dummy DOB value. The only problem now is how to display the literal values
"Birth" and "Alias" as neither corresponds to a column in tblAlias.

Sorry for the lengthy post, but my questions really are:
1) Can you bind data to controls using "rst.Fields(n)" syntax somehow?
2) If not, how can you bind a dummy column which doesn't have a field name?

One solution to 2) might be to use VBA as in (eg):

if rst.Fields(4) = "Birth" then
'set text box to "Birth"
else
'set text box to "Alias"
end if

This would have to be done for every row returned.

As a final question, is it possible in SQL to return the table name itself
as a "column"? If so, I wouldn't need to use the literal to identify the
source table.

TIA.

--
Cheers,
Lyn.
Nov 13 '05 #1
2 2716
Use the AS keyword to alias the field names so they are consistent between
the two tables, e.g.:

SELECT SurnameBirth, FNameBirth, DOB, 'Birth' AS Source
FROM tblBirth
UNION ALL
SELECT SurnameAlias, FNameAlias, Null AS DOB, 'Alias' AS Source
FROM tblAlias;
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Lyn" <lh******@ihug.com.au> wrote in message
news:d2**********@lust.ihug.co.nz...
Hi,
How do you bind the output columns from a UNION query when the fields from
the two tables have different names? Consider this query (WHERE clauses
omitted)...

SELECT SurnameBirth, FNameBirth, DOB, 'Birth' from tblBirth UNION
SELECT SurnameAlias, FNameAlias, #1/1/100#, 'Alias' from tblAlias;

What I am doing here is searching two tables for a person where the name
given may be the person's original name (at birth) or subsequent changed
name (marriage, etc). There are four fields to be returned from each
table with corresponding data types. The tblAlias does not have DOB so I
am returning a dummy literal value that will be recognised by code. In
addition, the fourth field is another literal that identifies the source
of the data row ("Birth" or "Alias"). The search criteria have been
omitted for simplicity.

I am trying to display the columns returned in text boxes in a listing in
Continuous Mode. The form displaying this data is called by another form
which does the query to produce a public recordset which is then used to
set the Recordset property of the listing form via the Load event.

Because the field names are different, depending on the source of the row,
I have tried to bind the columns to the controls using the generic syntax
"rst.Fields(n)" in an expression. While this syntax is accepted, it
results in a display of "#Name" in each text box.

I have found by trial and error that if I set "tblBirth" as the
RecordSource, I can then bind the controls using the field names from this
table, and it works even with rows returned from tblAlias including the
dummy DOB value. The only problem now is how to display the literal
values "Birth" and "Alias" as neither corresponds to a column in tblAlias.

Sorry for the lengthy post, but my questions really are:
1) Can you bind data to controls using "rst.Fields(n)" syntax somehow?
2) If not, how can you bind a dummy column which doesn't have a field
name?

One solution to 2) might be to use VBA as in (eg):

if rst.Fields(4) = "Birth" then
'set text box to "Birth"
else
'set text box to "Alias"
end if

This would have to be done for every row returned.

As a final question, is it possible in SQL to return the table name itself
as a "column"? If so, I wouldn't need to use the literal to identify the
source table.

TIA.

--
Cheers,
Lyn.

Nov 13 '05 #2
Lyn
Allen,
Many thanks. That worked just as I wanted!

--
Cheers,
Lyn.

"Allen Browne" <Al*********@SeeSig.Invalid> wrote in message
news:42***********************@per-qv1-newsreader-01.iinet.net.au...
Use the AS keyword to alias the field names so they are consistent between
the two tables, e.g.:

SELECT SurnameBirth, FNameBirth, DOB, 'Birth' AS Source
FROM tblBirth
UNION ALL
SELECT SurnameAlias, FNameAlias, Null AS DOB, 'Alias' AS Source
FROM tblAlias;
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
"Lyn" <lh******@ihug.com.au> wrote in message
news:d2**********@lust.ihug.co.nz...
Hi,
How do you bind the output columns from a UNION query when the fields
from the two tables have different names? Consider this query (WHERE
clauses omitted)...

SELECT SurnameBirth, FNameBirth, DOB, 'Birth' from tblBirth UNION
SELECT SurnameAlias, FNameAlias, #1/1/100#, 'Alias' from tblAlias;

What I am doing here is searching two tables for a person where the name
given may be the person's original name (at birth) or subsequent changed
name (marriage, etc). There are four fields to be returned from each
table with corresponding data types. The tblAlias does not have DOB so I
am returning a dummy literal value that will be recognised by code. In
addition, the fourth field is another literal that identifies the source
of the data row ("Birth" or "Alias"). The search criteria have been
omitted for simplicity.

I am trying to display the columns returned in text boxes in a listing in
Continuous Mode. The form displaying this data is called by another form
which does the query to produce a public recordset which is then used to
set the Recordset property of the listing form via the Load event.

Because the field names are different, depending on the source of the
row, I have tried to bind the columns to the controls using the generic
syntax "rst.Fields(n)" in an expression. While this syntax is accepted,
it results in a display of "#Name" in each text box.

I have found by trial and error that if I set "tblBirth" as the
RecordSource, I can then bind the controls using the field names from
this table, and it works even with rows returned from tblAlias including
the dummy DOB value. The only problem now is how to display the literal
values "Birth" and "Alias" as neither corresponds to a column in
tblAlias.

Sorry for the lengthy post, but my questions really are:
1) Can you bind data to controls using "rst.Fields(n)" syntax somehow?
2) If not, how can you bind a dummy column which doesn't have a field
name?

One solution to 2) might be to use VBA as in (eg):

if rst.Fields(4) = "Birth" then
'set text box to "Birth"
else
'set text box to "Alias"
end if

This would have to be done for every row returned.

As a final question, is it possible in SQL to return the table name
itself as a "column"? If so, I wouldn't need to use the literal to
identify the source table.

TIA.

--
Cheers,
Lyn.


Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alan | last post by:
I'm just about to start a project that needs to combine the results of a SQL Server query with the results of an Index Server query. The basic idea is that the user enters/selects a bunch of search...
3
by: Matt O'Donnell | last post by:
Does anyone know how I can 'join' the results of one SQL query to the bottom of another? Eg. I have two queries: 1. SELECT Name, Surname FROM People WHERE Surname = Smith NAME ...
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
3
by: axlchris | last post by:
Is it possible to bind two different tables to a listbox, or do I have to iterate through the result using the datareader and manually add each item in the listbox? thanks --- Posted using...
0
by: Ryan Ternier | last post by:
Don't ask why it's coded this way.. the clueless developer before me coded things upside down and insideout :\ Anyways, We have a simple SQL Query that reads a table into a DataSet. Here's...
7
by: vivekian | last post by:
Hi , I need to place the results of two different queries in the same result table parallel to each other. So if the result of the first query is 1 12 2 34 3 45
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
1
by: bgreenspan | last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am doing and what I tried. My database has entries with Contract Names and Expiry Dates, among other fields. I have a form...
1
by: Ken Fine | last post by:
I have set up Microsoft Search Server 2008 Express. I want to know how I can query against it and return results in a form that can be bound to ASP.NET controls like ListViews. If there's simply...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.