We help IT Professionals succeed at work.

Export MS Access query to another database as a table (using VBA)

giswest
giswest asked
on
778 Views
Last Modified: 2012-02-14
I have a situation where I've contructed a query in Database 1, and I would like to export this query (using VBA) to another database (Database 2), but when I export the query, I would like it to be saved as a TABLE in Database 2.   Basically, I want to copy/export the information contained in the query located in Database 1, and save it as a table in Database 2.  

1. Database 1 is an open database, and contains the query to be exported.
2. Database 2 is an existing database, is not open, and we'll say is located at:
    C:\Documents and Settings\GIS Admin\Desktop\Delete\Database 2.accdb

I don't know if this is possible, so I thought that I would see if anyone in the EE community has any suggestions.

Any help is appreciated.
Comment
Watch Question

Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
currentDB.execute "SELECT YourQuery.* INTO YourTableQuery FROM YourQuery;"
Docmd.copyobject
DoCmd.CopyObject "C:\Yourfolder\YourDestinationDB.mdb", "YourTableQuery", acTable, "YourTableQuery"
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
Sorry, just something like this works fine for me:

CurrentDb.Execute "SELECT YourQuery.* INTO YourTableQuery FROM YourQuery;"
DoCmd.CopyObject "C:\Yourfolder\YourDestinationDB.accdb", "YourTableQuery", acTable, "YourTableQuery"
CERTIFIED EXPERT
Top Expert 2016

Commented:
you can do this without using vba..
convert your select query into  a make table query
give a name for the table to be created
select the option Another Database, click Browse and locate the Database 2
save the query

run the query

if you want to run this query using vba,

docmd.openquery "NameOf Query"
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
to run this directly from VBA

currentdb.execute "SELECT  LocalTableName.* INTO TableName IN 'C:\Documents and Settings\GIS Admin\Desktop\Delete\Database 2.accdb'
FROM LocalTableName", dbfailonerror
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
cap, the reason I posted the code was so that the OP could see that it could be re-used for another query(s).

Then possibly doing cool stuff like selecting the query name from combobox,...
...and/or using a dialog box to browse to the destination DB...

;-)

Jeff
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
boaq and cap,

I appreciate your quick responses.  I don't think that I've ever had so many responses in such a short period of time, my email was really blowing up there for minute :).

Both or your suggestions worked great, and I'm sure that I'll use them again on future projects.  I'll get your points awarded here shortly.

Thanks for  the support!
Jeffrey CoachmanMIS Liason
CERTIFIED EXPERT
Most Valuable Expert 2012

Commented:
No problem, it is always best to know more than one way of doing something...

;-)

jeff

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.