473,503 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditionally select fields in SQL statement. Possible?

I currently have a 'mail-merge' process in my Access db project. It
generates custom filled out Award Certificates based on an SQL SELECT
statement in a VBA routine invoked by clicking on a command button.

The "problem": I want to conditionally insert some text into the award
certificate based on a field selected by the SELECT statement. Is this
possible?

Details:
One of the fields selected is a concatenation of a value from a table
(class from tblClasses with values of "Novice", "Open", or "Utility")
and the text " Class". The end text is "Novice Class" or "Open Class"
or "Utility Class". However, there are certain award certificates that
should not get this text inserted, when the award is for a unique
situation.

it looks like (in the SQL statement):
"SELECT ....., ([tblClasses].[class] + ' Class') as classtxt, ..."

The inserted field (into the merge template) is classtxt.
Sometimes, however, I don't want to insert anything for classtxt. I can
"gate" this based on another selected field in the SQL statement,
[tblTitles].[titleabbrev].
Is it possible to conditionally populate a selected field in an SELECT
statement and then if the field is not selected then don't populate
(insert) the data in the merged document?
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #1
10 2457
On Mon, 26 Sep 2005 16:56:59 GMT, SueB <sl*****@verizon.net> wrote:

Yes. You could use the IIf function or the Choose function. Here is an
example that would work in the Northwind sample application:
select 'This customer lives in ' & iif(State='TX', 'the Greatest
State', 'just another state') from Customers

Btw, note how I'm using the official & sign to concatenate strings.
Better than + in some situations.

-Tom.
I currently have a 'mail-merge' process in my Access db project. It
generates custom filled out Award Certificates based on an SQL SELECT
statement in a VBA routine invoked by clicking on a command button.

The "problem": I want to conditionally insert some text into the award
certificate based on a field selected by the SELECT statement. Is this
possible?

Details:
One of the fields selected is a concatenation of a value from a table
(class from tblClasses with values of "Novice", "Open", or "Utility")
and the text " Class". The end text is "Novice Class" or "Open Class"
or "Utility Class". However, there are certain award certificates that
should not get this text inserted, when the award is for a unique
situation.

it looks like (in the SQL statement):
"SELECT ....., ([tblClasses].[class] + ' Class') as classtxt, ..."

The inserted field (into the merge template) is classtxt.
Sometimes, however, I don't want to insert anything for classtxt. I can
"gate" this based on another selected field in the SQL statement,
[tblTitles].[titleabbrev].
Is it possible to conditionally populate a selected field in an SELECT
statement and then if the field is not selected then don't populate
(insert) the data in the merged document?
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***


Nov 13 '05 #2
Tom,

Wow. A really simple solution. Thanks.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #3
Tom,

Well I guess it's not as simple as I thought. Here is the SELECT
statement that I have to work with. The field/variable that I need to
setup is "classtxt". You will see that currently I am selecting
[tblClasses].[class] and appending the text " Class" to the data. But
in some instances I want to set "classtxt" to a single blank (" ") or ""
(either one ... basically no text).

This way sometimes the merged document will insert some text and
sometimes it will not insert some text (depending on the value of
another variable in the SELECT statement (tblTitles.titleabbrev).

Here is the original SELECT statement followed by the updated SELECT
statement that does not work.
Do you have any ideas?

strSelect = "SELECT tblDogTitles.dogtitleID, tblDogTitles.dogregnbr,
tblDogTitles.processeddt, Day(tblTrials.trialdt) & ' ' &
MonthName(Month(tblTrials.trialdt)) & ' ' & Year(tblTrials.trialdt) AS
outtrialdt, tblTitles.title, tblTitles.titleabbrev, tblDogs.formalname,
tblPeople.fname & (' '+tblPeople.midinit+'.') & ' ' & tblPeople.lname &
(' '+tblPeople.suffix+'.') AS owner, tblPeople.altperson AS altowner,
tblClasses.class & ' Class' AS classtxt, tblUserSettings.uval FROM
tblUserSettings, tblTrials INNER JOIN (((tblClasses INNER JOIN
tblTrialClass ON tblClasses.classID = tblTrialClass.classID) INNER JOIN
tblTitles ON tblClasses.classID = tblTitles.classID) INNER JOIN
(tblPeople INNER JOIN (tblDogs INNER JOIN tblDogTitles ON
tblDogs.dogregnbr = tblDogTitles.dogregnbr) ON tblPeople.peopleID =
tblDogs.peopleID) ON (tblTrialClass.trialclassID =
tblDogTitles.trialclassID) AND (tblTitles.titleID =
tblDogTitles.titleID)) ON tblTrials.trialID = tblTrialClass.trialID "
strSELECT = "SELECT tblDogTitles.dogtitleID, tblDogTitles.dogregnbr,
tblDogTitles.processeddt, Day(tblTrials.trialdt) & ' ' &
MonthName(Month(tblTrials.trialdt)) & ' ' & Year(tblTrials.trialdt) AS
outtrialdt, tblTitles.title, tblTitles.titleabbrev, tblDogs.formalname,
tblPeople.fname & (' '+tblPeople.midinit+'.') & ' ' & tblPeople.lname &
(' '+tblPeople.suffix+'.') AS owner, tblPeople.altperson AS altowner,
IIF(titleabbrev = 'UDX-H' OR titleabbrev = 'OTCH-H','' AS
classtxt,tblClasses.class & ' Class' AS classtxt), tblUserSettings.uval
FROM tblUserSettings, tblTrials INNER JOIN (((tblClasses INNER JOIN
tblTrialClass ON tblClasses.classID = tblTrialClass.classID) INNER JOIN
tblTitles ON tblClasses.classID = tblTitles.classID) INNER JOIN
(tblPeople INNER JOIN (tblDogs INNER JOIN tblDogTitles ON
tblDogs.dogregnbr = tblDogTitles.dogregnbr) ON tblPeople.peopleID =
tblDogs.peopleID) ON (tblTrialClass.trialclassID =
tblDogTitles.trialclassID) AND (tblTitles.titleID =
tblDogTitles.titleID)) ON tblTrials.trialID = tblTrialClass.trialID "

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #4
Sue:

Your line with the IIF should be like this IIF(test,
trueresult,falseresult) AS variablename. Don't put the AS classtext
within the IIF. Corrected, it should look like this:

IIF(titleabbrev = 'UDX-H' OR titleabbrev = 'OTCH-H','',
tblClasses.class & ' Class') AS classtxt,

That should do it!

Jana

Nov 13 '05 #5
Jana,

Thanks, so much.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #6
On Thu, 29 Sep 2005 17:15:18 GMT, SueB <sl*****@verizon.net> wrote:
IIF(titleabbrev = 'UDX-H' OR titleabbrev = 'OTCH-H','' AS
classtxt,tblClasses.class & ' Class' AS classtxt),


Replace the above with:

IIF((titleabbrev = 'UDX-H') OR (titleabbrev
='OTCH-H'),'',tblClasses.class & ' Class') AS classtxt,

mike

Nov 13 '05 #7
On 29 Sep 2005 10:50:44 -0700, "Jana" <Ba********@gmail.com> wrote:
Sue:

Your line with the IIF should be like this IIF(test,
trueresult,falseresult) AS variablename. Don't put the AS classtext
within the IIF. Corrected, it should look like this:

IIF(titleabbrev = 'UDX-H' OR titleabbrev = 'OTCH-H','',
tblClasses.class & ' Class') AS classtxt,

That should do it!


Jana,

Did this message thread properly for you? It would save people a lot
of work if all responses properly thread. I'm just wondering if it
threaded on your newsreader?

mike
Nov 13 '05 #8
Mike--sorry, I don't know what you mean by 'threaded properly'. My
post showed up in Google Groups just fine...

Jana

Nov 13 '05 #9
On 30 Sep 2005 10:14:19 -0700, "Jana" <Ba********@gmail.com> wrote:
Mike--sorry, I don't know what you mean by 'threaded properly'. My
post showed up in Google Groups just fine...


Interesting. I'm using FreeAgent, and your response started a new
thread. I read the "other" thread and noticed that there weren't any
responses, so I responded. Had I seen your response, I wouldn't have.
Did you post from google?

mike
Nov 13 '05 #10
Mike:

Yes, I posted from Google. Everything seems fine here...

Jana

Nov 13 '05 #11

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

Similar topics

2
2456
by: ghasem | last post by:
Dear mySQL community, I have once again turned to the user groups for a problem I cannot solve myself! Atually, I have read all I can from the newsgroups on this but I could not solve it myself....
1
355
by: Amanda | last post by:
We use an oracle database. I want to select adress, postal code en city from the table 'subject'. But those fields are not allways filled. If they are empty, there is allways an unique adres...
4
10301
by: Ben | last post by:
I believe I am missunderstanding how subqueries work. I simple subquery works fine but when I wish do compare 2 or more fields at once I don't get the results I wish. Table A...
4
2817
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
5
1950
by: Takeadoe | last post by:
I've got a favor to ask - Consider the following numeric field: 511 6805 3205 403 I need to make 2 new numeric fields from this variable, call it CS
6
18233
by: dhek | last post by:
Hi, I have a very simple issue: for simplicity lets say I have 2 tables, A and B. - Table A contains 5 fields. Amongst these there is a 'id'-field which is but a reference to table B. - Table...
1
2885
by: P3Eddie | last post by:
Hello All! Sorry if this is basic, but I'm having trouble finding a way to specify unique fields (like you would ADD CONSTRAINT in an ALTER TABLE statement) for a new table I'm creating with a...
1
1289
by: crazyhouse | last post by:
I have 2 tables - tblCertData and tblHL tblCertData includes multiple fields, but the ones that matter at this point are (number field) and (text field). tblHL includes fields (number field)...
45
27635
by: dizzydangler | last post by:
Hi, I'm new to access (2007, running on XP), but I've gotten some great help from this site on my project so far. However, the more I know, the more complex the problems seem to become.... I'm...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.