Select Query IIF Not testing column value

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

I have a query that I use to export a text file for another database. I am currently testing changes, and need the same output, but formatted into a series of "Insert Into" statements to be able to develop. So I wrote another query to produce a fully formatted text string for each of the four "TempTbl's". The query pulls the correct number of rows, and all the correct information, EXCEPT for each value of the TempTbl. I only return the final temp table in the last nested "else" part of IIF (i.e. "#TempW ").

How can I get the query to insert "#Temp4 ", "#Temp5 ", etc.?

Many thanks!

Code:
SELECT (IIf([tmpTbl]="[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL] ","INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  VALUES ('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL] ','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')",IIf([tmpTbl]="[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp5]#Temp5[/URL] ","INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp5]#Temp5[/URL]  VALUES ('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp5]#Temp5[/URL] ','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')",IIf([tmpTbl]="[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempG]#TempG[/URL] ","INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempG]#TempG[/URL]  VALUES ('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempG]#TempG[/URL] ','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')","INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempW]#TempW[/URL]  VALUES ('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempW]#TempW[/URL] ','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')")))) AS SQLstring
FROM qryFormattedToast2;
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
As far as I know this can't be done using SQL in Access - can be done in VBA, however, it is not clear what tells the query which temp table to use...i.e. what is [tmpTbl]
 
Upvote 0
Also note that this query has #Temp4 hard coded in so I think it will *always* be an "insert into #Temp4 ..."
Code:
SELECT (IIf([tmpTbl]="[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  ","INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]   VALUES ('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  ','"&...

You need that part of the result to also be dynamic (a variable).
Possibly something like:
Code:
SELECT (IIf([tmpTbl]="[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  ","INSERT INTO " & [B][TmpTbl][/B] & " VALUES ('" & [B][TmpTbl][/B] & "','"&...
 
Last edited:
Upvote 0
Also note that this query has #Temp4 hard coded in so I think it will *always* be an "insert into #Temp4 ..."
Code:
SELECT (IIf([tmpTbl]="[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4"]#Temp4[/URL]  ","INSERT INTO [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4"]#Temp4[/URL]   VALUES ('[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4"]#Temp4[/URL]  ','"&...

You need that part of the result to also be dynamic (a variable).
Possibly something like:
Code:
SELECT (IIf([tmpTbl]="[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4"]#Temp4[/URL]  ","INSERT INTO " & [B][TmpTbl][/B] & " VALUES ('" & [B][TmpTbl][/B] & "','"&...

look at the rest of the string
the nested iifs go on forever LOL
 
Upvote 0
oh okay. Hmmm not sure then.

Note: perhaps a little bit of sample data would make it clearer.
 
Last edited:
Upvote 0
Ha ha ha ... James_Lankford ... they are pretty nasty Iif's.

I attempted to single down to just a single 'tmpTbl', using a WHERE condition. It didn't work ...

SELECT ("INSERT INTO " & [#Temp4 ] & " VALUES('" & [#Temp4 ] & "','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')") AS SQLstring
FROM qryFormattedToast2
WHERE tmpTbl = "#Temp4 ";

Is it possible for it to work in a single Select statement like this? If I were referencing a permanent table instead of a query, would that change what is returned? Is my query attempting to pull in too many columns on each iteration?

Here is some sample data... (I took out some of the variables for size)

[tempTbl] [CHKAccount] [RequestID] [PlanID] [SSN] [AMT] [DBID]
#Temp4 1000000001 042020180001 00000ABC 000-00-0000 10.00 0001
#Temp5 1000000001 042020180002 00000DEF 111-11-1111 20.00 0002
#TempG 1000000001 00000STU 222-22-2222 30.00 0003
#TempW 1000000001 00000XYZ 333-33-3333 40.00 0004
 
Upvote 0
I changed the base query into a Make Table query, and then ran the following:

SELECT ("INSERT INTO " & [tempTbl] & " VALUES('" & [tempTbl] & "','"&[CHKAccount]&"','"&[RequestID]&"','"&[PlanID]&"','"&[Payroll]&"','"&[SSN]&"','"&[AMT]&"','"&[PayrollAMT]&"','"&[SOURCE]&"','"&[DBID]&"')") AS SQLstring
FROM tblFormattedToast2;

I need to verify, but it seems to have worked. Why would a permanent table work, but not another query? I don't want to add tables to my database ...
 
Upvote 0
Back to Post #6 this worked for me as a quick rewrite:
Code:
SELECT ("INSERT INTO " & [TempTbl ] & " VALUES('" & [TempTbl] & "','"&[CHKAccount]&"')") AS SQLstring
FROM qryFormattedToast2
WHERE tempTbl = "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  ";

qryTempTable returns the results:
Code:
[tempTbl] [CHKAccount] [RequestID] [PlanID] [SSN] [AMT] [DBID] 
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  1000000001 042020180001 00000ABC 000-00-0000 10.00 0001
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp5]#Temp5[/URL]  1000000001 042020180002 00000DEF 111-11-1111 20.00 0002
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempG]#TempG[/URL]  1000000001 00000STU 222-22-2222 30.00 0003
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=TempW]#TempW[/URL]  1000000001 00000XYZ 333-33-3333 40.00 0004

Result of the query:
Code:
INSERT INTO [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL]  VALUES('[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Temp4]#Temp4[/URL] ','1000000001')

Note that TmpTbl became TempTbl in this version of the data (probably by mistake but I stayed with what was given).

It seems like there is ongoing confusion between the name of the column (TmpTbl or TempTbl), which is constant, and the values of the column (#Temp4 , etc.) which is variable, and also how to use these in the query. It's a little extra confusing also because the query produces a query string as a result - however, from the point of view of running this query, it's just a string like any other.


Note that I apologize but I am ignoring post 7 for new. I'm not sure what you are doing with make table queries (the query is not a make-table query and it isn't using a make table query).
 
Last edited:
Upvote 0
Xenou,

Thank you. You are correct, my post seven was not a make-table query, although I did end up making a table to get my code to work. However, following your response, I carefully checked and updated my queries, and all work perfectly ( and without the need to make an extra table).
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top