Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

query di UNIONE da VBA con accodamento.???

154 views
Skip to first unread message

Roby36

unread,
Feb 10, 2010, 10:13:42 AM2/10/10
to
Ciao, sto impazzendo, non riesco ad inserire in una tabella il
risultato di una query di unione da VBA, la sintassi che utilizzo e'
la seguente ma non so qual'e' l'errore. Potete aiutarmi a trovare la
giusta sintassi?

StringaSQL= INSERT INTO Tabella3 " & _
"SELECT T1.NOME, T1.COGNOME, Sum(T1.MAX) AS VALOREMAX, T1.DATE " & _
"FROM T1 " & _
"GROUP BY T1.NOME, T1.COGNOME,T1.DATE " & _
"UNION " & _
"SELECT T2.NOME, T2.COGNOME, Sum(T2.MAX) AS VALOREMAX, T2.DATE " & _
"FROM T2 " & _
"GROUP BY T1.NOME, T1.COGNOME,T1.DATE;"

docmd.runsql StringaSQL


Grazie

Karl Donaubauer

unread,
Feb 10, 2010, 10:41:26 AM2/10/10
to

Non � un problema della sintassi ma una limitazione generale
che in Access non si puo combinare INTO e UNION.
(vedi p.e. http://support.microsoft.com/kb/208819/en-us)
Devi salvare la query UNION e poi inserire dalla query salvata.

Se l'enunciato SQL � molto variabile puoi usare una query salvata
"qryDummy" con un qualsiasi testo SQL. Poi nel codice fai:

StringaSQL = "SELECT T1.NOME, ecc. UNION ecc."

Currentdb.QueryDefs("qryDummy").SQL = StringaSQL

StringaSQL = "INSERT INTO Tabella3 SELECT * FROM qryDummy"

--
HTH
Karl
*********
Access FAQ: www.donkarl.com/it


Roby36

unread,
Feb 10, 2010, 11:27:08 AM2/10/10
to
On 10 Feb, 16:41, "Karl Donaubauer" <NoS...@donkarl.com> wrote:
> Roby36 wrote:
> > Ciao, sto impazzendo, non riesco ad inserire in una tabella il
> > risultato di una query di unione da VBA,  la sintassi che utilizzo e'
> > la seguente ma non so qual'e' l'errore. Potete aiutarmi a trovare la
> > giusta sintassi?
>
> > StringaSQL= INSERT INTO Tabella3  " & _
> > "SELECT T1.NOME, T1.COGNOME, Sum(T1.MAX) AS VALOREMAX, T1.DATE " & _
> > "FROM T1 " & _
> > "GROUP BY T1.NOME, T1.COGNOME,T1.DATE " & _
> > "UNION " & _
> > "SELECT T2.NOME, T2.COGNOME, Sum(T2.MAX) AS VALOREMAX, T2.DATE " & _
> > "FROM T2 " & _
> > "GROUP BY T1.NOME, T1.COGNOME,T1.DATE;"
>
> > docmd.runsql StringaSQL
>
> Non è un problema della sintassi ma una limitazione generale

> che in Access non si puo combinare INTO e UNION.
> (vedi p.e.http://support.microsoft.com/kb/208819/en-us)

> Devi salvare la query UNION e poi inserire dalla query salvata.
>
> Se l'enunciato SQL è molto variabile puoi usare una query salvata

> "qryDummy" con un qualsiasi testo SQL. Poi nel codice fai:
>
> StringaSQL = "SELECT T1.NOME, ecc. UNION ecc."
>
> Currentdb.QueryDefs("qryDummy").SQL = StringaSQL
>
> StringaSQL = "INSERT INTO Tabella3 SELECT * FROM qryDummy"
>
> --
> HTH
> Karl
> *********
> Access FAQ:www.donkarl.com/it
Ciao Karl, purtroppo alla riga

> Currentdb.QueryDefs("qryDummy").SQL = StringaSQL
ricevo il runtime error 3265: Item not found in this collection.
Naturalmente ho provato la query di unione prima di riportarla in VBA.
Non so proprio cosa sia dovuto il problema. Puoi iutarmi?

grazie

Karl Donaubauer

unread,
Feb 10, 2010, 11:50:21 AM2/10/10
to
Roby36 wrote:
> Karl Donaubauer wrote:
>> ...
>> Se l'enunciato SQL � molto variabile puoi usare una query salvata

>> "qryDummy" con un qualsiasi testo SQL. Poi nel codice fai:
>>
>> StringaSQL = "SELECT T1.NOME, ecc. UNION ecc."
>>
>> Currentdb.QueryDefs("qryDummy").SQL = StringaSQL
>>
>> StringaSQL = "INSERT INTO Tabella3 SELECT * FROM qryDummy"
>>
> Ciao Karl, purtroppo alla riga
>> Currentdb.QueryDefs("qryDummy").SQL = StringaSQL
> ricevo il runtime error 3265: Item not found in this collection.
> Naturalmente ho provato la query di unione prima di riportarla in VBA.
> Non so proprio cosa sia dovuto il problema. Puoi iutarmi?

L'errore molto probabilmente significa che non hai salvata
una query con il nome "qryDummy".
Come scritto devi fare questo come primo passo.

giorgio rancati

unread,
Feb 10, 2010, 3:44:29 PM2/10/10
to

"Roby36" <ri...@libero.it> ha scritto nel messaggio
news:4c83acd3-ada6-4eae...@w16g2000yqw.googlegroups.com...

Ciao Roby,
puoi fare anche cos�:
----


StringaSQL= "INSERT INTO Tabella3 " & _

"SELECT * FROM (


"SELECT T1.NOME, T1.COGNOME, Sum(T1.MAX) AS VALOREMAX, T1.DATE " & _
"FROM T1 " & _
"GROUP BY T1.NOME, T1.COGNOME,T1.DATE " & _
"UNION " & _
"SELECT T2.NOME, T2.COGNOME, Sum(T2.MAX) AS VALOREMAX, T2.DATE " & _
"FROM T2 " & _

"GROUP BY T1.NOME, T1.COGNOME,T1.DATE) AS T; "

docmd.runsql StringaSQL
----

Ciao
--
Giorgio Rancati
[Office Access MVP]


MA

unread,
Feb 11, 2010, 6:28:22 AM2/11/10
to

"giorgio rancati" <giorgio_No_Sp...@tiscali.it> ha scritto nel
messaggio news:hkv5rg$fjj$1...@speranza.aioe.org...

Grande Giorgio...

Italia - Austria = 1-0

Goal di Rancati!!!!

;-)

giorgio rancati

unread,
Feb 11, 2010, 7:47:12 AM2/11/10
to

"MA" <sp...@spam.it> ha scritto nel messaggio
news:OQAjU1wq...@TK2MSFTNGP02.phx.gbl...

>
> Grande Giorgio...
>
> Italia - Austria = 1-0
>
> Goal di Rancati!!!!

LOL se segnavi tu cosa avresti scritto ?
Napoli - Austria = 1-0
:-D

qualche volta segno io, qualche volta segna Karl
:-)

Roby36

unread,
Feb 11, 2010, 11:14:04 AM2/11/10
to
On 10 Feb, 21:44, "giorgio rancati"
<giorgio_No_Spalmer_ranc...@tiscali.it> wrote:
> "Roby36" <ri...@libero.it> ha scritto nel messaggionews:4c83acd3-ada6-4eae...@w16g2000yqw.googlegroups.com...

>
> > Ciao, sto impazzendo, non riesco ad inserire in una tabella il
> > risultato di una query di unione da VBA,  la sintassi che utilizzo e'
> > la seguente ma non so qual'e' l'errore. Potete aiutarmi a trovare la
> > giusta sintassi?
>
> > StringaSQL= INSERT INTO Tabella3  " & _
> > "SELECT T1.NOME, T1.COGNOME, Sum(T1.MAX) AS VALOREMAX, T1.DATE " & _
> > "FROM T1 " & _
> > "GROUP BY T1.NOME, T1.COGNOME,T1.DATE " & _
> > "UNION " & _
> > "SELECT T2.NOME, T2.COGNOME, Sum(T2.MAX) AS VALOREMAX, T2.DATE " & _
> > "FROM T2 " & _
> > "GROUP BY T1.NOME, T1.COGNOME,T1.DATE;"
>
> > docmd.runsql StringaSQL
>
> Ciao Roby,
> puoi fare anche così:

> ----
>  StringaSQL= "INSERT INTO Tabella3  " & _
>  "SELECT * FROM (
>  "SELECT T1.NOME, T1.COGNOME, Sum(T1.MAX) AS VALOREMAX, T1.DATE " & _
>  "FROM T1 " & _
>  "GROUP BY T1.NOME, T1.COGNOME,T1.DATE " & _
>  "UNION " & _
>  "SELECT T2.NOME, T2.COGNOME, Sum(T2.MAX) AS VALOREMAX, T2.DATE " & _
>  "FROM T2 " & _
>  "GROUP BY T1.NOME, T1.COGNOME,T1.DATE) AS T; "
>
> docmd.runsql StringaSQL
> ----
>
> Ciao
> --
> Giorgio Rancati
> [Office Access MVP]

Grazie, funziona anke se credo che suddividero' le query in due in
quanto visto che il db sorgente e' molto grande e tale query impiega
molto tempo.

Roby36

unread,
Feb 11, 2010, 11:14:42 AM2/11/10
to
On 10 Feb, 17:50, "Karl Donaubauer" <NoS...@donkarl.com> wrote:
> Roby36 wrote:
> > Karl Donaubauer wrote:
> >> ...
> >> Se l'enunciato SQL è molto variabile puoi usare una query salvata

Si, non avevo una qryDummy. Sorry hai ragione.
Grazie Mille.

0 new messages