TransferSpreadsheet with dynamic query... in an adp project

I try to use TransferSpreadsheet with dynamic query but it doesn't want to accept syntax like:

DoCmd.TransferSpreadsheet acExport, , "SELECT * From outtab" & GetLogin & OrdStr, myPath

I get Run-time Error '7871' : The table name you entered doesn't follow the object-naming rules

it accepts with table but not with sql statment.

I really need TransferSpreadsheet and not OutputTo (TransferSpreadsheet can export more records)

Thanks
meciabAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nico5038Commented:
Just create a "dummy" query named "qryDynam"
and use this to fill the SQL text:

dim qd as querydef
set qd = currentdb.querydefs("qryDynam")
qd.SQL = "SELECT * From outtab" & GetLogin & OrdStr

After this you can use:
DoCmd.TransferSpreadsheet acExport, , "qryDynam", myPath

Nic;o)
meciabAuthor Commented:
for the querydef declaration compilator says "User-defined type not defined"
nico5038Commented:
Sorry, you need to open Tools/References when in the VBA editor and check the "Microsoft DAO version #.##" library.

Nic;o)
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

meciabAuthor Commented:
Ok but on
set qd = currentdb.querydefs("qryDynam")
I have a "Object variable or variable with block variable not set" error
nico5038Commented:
Hmm, try:

Dim qd As DAO.QueryDef
Set qd = CurrentDb.QueryDefs("qryDynam")

Nic;o)
meciabAuthor Commented:
I have the same error (I don't know if it's related with but it's an ADP project)
nico5038Commented:
Hmm Access is sometimes stubborn, try

Dim qd As DAO.QueryDef
Dim db as database

set db = currentdb
Set qd = db.QueryDefs("qryDynam")

Nico)
meciabAuthor Commented:
I have the same error (I don't know if it's related with but it's an ADP project)
nico5038Commented:
OK, back to basic, first create a saved query with fixed values and then try:

DoCmd.TransferSpreadsheet acExport, , qryFixed

Nic;o)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.