Connecting Tech Pros Worldwide Forums | Help | Site Map

Conditionally select fields in SQL statement. Possible?

SueB
Guest
 
Posts: n/a
#1: Nov 13 '05
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 ***

Tom van Stiphout
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


On Mon, 26 Sep 2005 16:56:59 GMT, SueB <slbrick@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.


[color=blue]
>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 ***[/color]

Susan Bricker
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


Tom,

Wow. A really simple solution. Thanks.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
SueB
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


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 ***
Jana
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


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

SueB
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


Jana,

Thanks, so much.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Mike Preston
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


On Thu, 29 Sep 2005 17:15:18 GMT, SueB <slbrick@verizon.net> wrote:
[color=blue]
>IIF(titleabbrev = 'UDX-H' OR titleabbrev = 'OTCH-H','' AS
>classtxt,tblClasses.class & ' Class' AS classtxt),[/color]

Replace the above with:

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

mike

Mike Preston
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


On 29 Sep 2005 10:50:44 -0700, "Jana" <Bauer.Jana@gmail.com> wrote:
[color=blue]
>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![/color]

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
Jana
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


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

Jana

Mike Preston
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


On 30 Sep 2005 10:14:19 -0700, "Jana" <Bauer.Jana@gmail.com> wrote:
[color=blue]
>Mike--sorry, I don't know what you mean by 'threaded properly'. My
>post showed up in Google Groups just fine...[/color]

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
Jana
Guest
 
Posts: n/a
#11: Nov 13 '05

re: Conditionally select fields in SQL statement. Possible?


Mike:

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

Jana

Closed Thread


Similar Microsoft Access / VBA bytes