PC Review


Reply
Thread Tools Rate Thread

Query Inside an SQL statement

 
 
Mr. Bud
Guest
Posts: n/a
 
      4th May 2010
Hi and TIA. What I'm wondering is can you use aquery name in place of a
table name when building an sql statement in code? Example below can I
replace MyTable with MyQuery. It bombs out on me when I run the code. My
goal is to creat several Union queries in my db. Have the user select
tables on my form to use in the results and depending on what they select I
will use that saved Union query in place of the table name (MyTable)
below.Thanks for your time.


Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
If Me!lstUIC_ID.ItemsSelected.Count > 0 Then
For Each varItem In Me!lstUIC_ID.ItemsSelected
strCriteria = strCriteria & "MyTable.UIC = " & Chr(34) _
& Me!lstUIC_ID.ItemData(varItem) & Chr(34) & "OR "
Next varItem
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
Else
strCriteria = "MyTable.UIC Like '*'"
End If
strSQL = "SELECT * FROM MyTable" & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "9h1_qryMultiSelect"


--

Reggie

 
Reply With Quote
 
 
 
 
Stefan Hoffmann
Guest
Posts: n/a
 
      4th May 2010
hi,

On 04.05.2010 09:21, Mr. Bud wrote:
> Hi and TIA. What I'm wondering is can you use aquery name in place of a
> table name when building an sql statement in code?

You can.

> Example below can I
> replace MyTable with MyQuery. It bombs out on me when I run the code.

hmm, "bombs out" is not a quite accurate error description.

> Set db = CurrentDb()
> Set qdf = db.QueryDefs("qryMultiSelect")
> If Me!lstUIC_ID.ItemsSelected.Count > 0 Then
> For Each varItem In Me!lstUIC_ID.ItemsSelected
> strCriteria = strCriteria & "MyTable.UIC = " & Chr(34) _
> & Me!lstUIC_ID.ItemData(varItem) & Chr(34) & "OR "
> Next varItem
> strCriteria = Left(strCriteria, Len(strCriteria) - 3)
> Else
> strCriteria = "MyTable.UIC Like '*'"
> End If
> strSQL = "SELECT * FROM MyTable" & _
> "WHERE " & strCriteria & ";"
> qdf.SQL = strSQL

Use Debug.Ptin strSQL to output the SQL statement to the immediate
window. Copy'n'paste it into a new query. Try to fix it. Then adjust
your code...




mfG
--> stefan <--
 
Reply With Quote
 
 
 
 
Bob Barrows
Guest
Posts: n/a
 
      4th May 2010
Mr. Bud wrote:
> Hi and TIA. What I'm wondering is can you use aquery name in place
> of a table name when building an sql statement in code? Example
> below can I replace MyTable with MyQuery.


Yes. A saved query looks like a table to the query engine.

> It bombs out on me when I run the code.


:-)
We're not looking over your shoulder at your screen. What is the error
message you get?

> My goal is to creat several Union queries in my db.


? Why several union queries? Or do you mean to say you have created a union
query that combines the data from several tables?

> Have the user select tables on my form to use in the results and
> depending on what they select I will use that saved Union query in
> place of the table name (MyTable) below.Thanks for your time.
>
>
> Set db = CurrentDb()
> Set qdf = db.QueryDefs("qryMultiSelect")
> If Me!lstUIC_ID.ItemsSelected.Count > 0 Then
> For Each varItem In Me!lstUIC_ID.ItemsSelected
> strCriteria = strCriteria & "MyTable.UIC = " & Chr(34) _
> & Me!lstUIC_ID.ItemData(varItem) & Chr(34) &
> "OR " Next varItem
> strCriteria = Left(strCriteria, Len(strCriteria) - 3)
> Else
> strCriteria = "MyTable.UIC Like '*'"
> End If
> strSQL = "SELECT * FROM MyTable" & _
> "WHERE " & strCriteria & ";"
> qdf.SQL = strSQL


This sql statement will raise a syntax error because you have failed to put
a space between MyTable and WHERE. While debugging, you should look at the
results of your concatenation. You can do that by using msgbox or
debug.print.

debug.print strSQL

> DoCmd.OpenQuery "9h1_qryMultiSelect"

Do you have a saved query called "9h1_qryMultiSelect"? if not, this last
line will raise an "object not found" error.

Does qryMultiSelect contain your union query? And does that union query
return a field called UIC? If so, your code is replacing that union query
with a simple select statement.

Or is there another saved query that does the union? And qryMultiSelect is
intended just to be used in your OpenQuery statement? If so, then you should
be able to use the name of that saved union query (whatever its name is) in
your sql. Like this:

strSQL = "SELECT * FROM qryUnion " & _
"WHERE " & strCriteria & ";"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryMultiSelect"

My preference is not to display the results of a query in a query window.
Given that this is read-only data, I would create a report whose data source
is qryMultiSelect and use DoCmd.OpenReport instead. Alternatively, you can
use a subform whose form's data source is qryMultiSelect and use
subformname.Requery


--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Mr. Bud
Guest
Posts: n/a
 
      4th May 2010
Responses below:

Thanks All for your responses and sorry for my lame post. I figured out the
problem it was because the query name I am using has a leading number in the
name. Once I put the name in brackets it runs like a charm. Guess the
number was throwing off the sql statement.


"Bob Barrows" <(E-Mail Removed)> wrote in message
news:ugkSR$(E-Mail Removed)...
> Mr. Bud wrote:
>> Hi and TIA. What I'm wondering is can you use aquery name in place
>> of a table name when building an sql statement in code? Example
>> below can I replace MyTable with MyQuery.

>
> Yes. A saved query looks like a table to the query engine.
>
>> It bombs out on me when I run the code.

>
> :-)
> We're not looking over your shoulder at your screen. What is the error
> message you get?
>
>> My goal is to creat several Union queries in my db.

>
> ? Why several union queries? Or do you mean to say you have created a
> union query that combines the data from several tables?
>
>> Have the user select tables on my form to use in the results and
>> depending on what they select I will use that saved Union query in
>> place of the table name (MyTable) below.Thanks for your time.
>>
>>
>> Set db = CurrentDb()
>> Set qdf = db.QueryDefs("qryMultiSelect")
>> If Me!lstUIC_ID.ItemsSelected.Count > 0 Then
>> For Each varItem In Me!lstUIC_ID.ItemsSelected
>> strCriteria = strCriteria & "MyTable.UIC = " & Chr(34) _
>> & Me!lstUIC_ID.ItemData(varItem) & Chr(34) &
>> "OR " Next varItem
>> strCriteria = Left(strCriteria, Len(strCriteria) - 3)
>> Else
>> strCriteria = "MyTable.UIC Like '*'"
>> End If
>> strSQL = "SELECT * FROM MyTable" & _
>> "WHERE " & strCriteria & ";"
>> qdf.SQL = strSQL

>
> This sql statement will raise a syntax error because you have failed to
> put a space between MyTable and WHERE. While debugging, you should look
> at the results of your concatenation. You can do that by using msgbox or
> debug.print.
>
> debug.print strSQL
>
>> DoCmd.OpenQuery "9h1_qryMultiSelect"

> Do you have a saved query called "9h1_qryMultiSelect"? if not, this last
> line will raise an "object not found" error.
>
> Does qryMultiSelect contain your union query? And does that union query
> return a field called UIC? If so, your code is replacing that union query
> with a simple select statement.
>
> Or is there another saved query that does the union? And qryMultiSelect is
> intended just to be used in your OpenQuery statement? If so, then you
> should be able to use the name of that saved union query (whatever its
> name is) in your sql. Like this:
>

This is correct. Using my list box on the form the user wil select a table
or a combination of the tables (only 3 off them)
Depending On their selection I will pass a string (strQueryName) to this
procedure containing the name of the predefined union query and use in place
of qryUnion you have below. The qryMultiSelect is just a holder query and
built on the fly

> strSQL = "SELECT * FROM qryUnion " & _
> "WHERE " & strCriteria & ";"
> qdf.SQL = strSQL
> DoCmd.OpenQuery "qryMultiSelect"
>
> My preference is not to display the results of a query in a query window.
> Given that this is read-only data, I would create a report whose data
> source is qryMultiSelect and use DoCmd.OpenReport instead. Alternatively,
> you can use a subform whose form's data source is qryMultiSelect and use
> subformname.Requery


Yep. I'm simply exporting the results to excel.
>
>
> --
> Microsoft MVP - ASP/ASP.NET - 2004-2007
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>





--

Reggie

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF statement inside a SUMIF statement.... or alternative method Sungibungi Microsoft Excel Worksheet Functions 3 4th Dec 2009 07:22 PM
If Statement inside If statement.... Shhhh Microsoft Excel Discussion 3 19th May 2009 06:44 AM
run SQL statement from same SQL statement for searching =?Utf-8?B?TWlrZQ==?= Microsoft Access VBA Modules 4 15th Apr 2005 06:47 PM
Calling MS SQL's Query Designer to reformat SQL Statement? Norton Microsoft VB .NET 1 30th Sep 2004 09:18 PM
OR statement inside a COUNTIF statement Carla Microsoft Excel Worksheet Functions 3 25th Jun 2004 07:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:05 PM.