Solved

Running access query with specs to .txt file

Posted on 2015-02-19
5
69 Views
Last Modified: 2016-02-10
I have a form in Access 2010 that runs the following line of code when a button is pressed:

DoCmd.TransferText acExportDelim, "mySpecs", "myQueryName", "C:\myFileName.txt", False

I wish to use Excel and perform this same exact functionality using VBA within Excel.

I am able to run the query and have the output in Excel just fine, but I need to use the specs that are saved within the database.  Any ideas?

Thanks!
0
Comment
Question by:mak345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40618788
HI,

pls try

Dim acObj As Object
Set acObj = CreateObject("Access.Application")
acObj.Application.Visible = True
acObj.OpenCurrentDatabase "C:\testMDB\TEST.accdb",,"ADatabasePassword"
acObj.DoCmd.TransferText acExportDelim, "mySpecs", "myQueryName", "C:\myFileName.txt", False
acObj.Application.Quit
Set acObj = Nothing

Open in new window

Regards
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40618797
You can use Access automation from within Excel.  It would look something like this:
Dim oACC as object
dim oDB as object
set oACC = Createobject("access.application")
oACC.Opendatabase "your database path and name"
oACC.DoCmd.TransferText acExportDelim, "mySpecs", "myQueryName", "C:\myFileName.txt", False
oACC.Close
oACC.Quit
set oACC = nothing

Open in new window


You could also populate an ADO recordset variable in Excel and do your exporting directly from the recordset or through an intermediate variant array (populated with GetRows method)
0
 

Author Comment

by:mak345
ID: 40618899
Using either of the above code, I get the following message:

The Microsoft Access database engine could not find the object 'myFileName.txt'.  Make sure the object exists and that you spell its name and the path name correctly.  If 'myFileName.txt' is not a local object, check your network connection or contact the server administrator.
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40618972
Since you're in  the Excel environment, the acExportDelim constant is probably unknown.
Dim oACC as object
dim oDB as object
const acExportDelim as long = 2
set oACC = Createobject("access.application")
oACC.Opendatabase "your database path and name"
oACC.DoCmd.TransferText acExportDelim, "mySpecs", "myQueryName", "C:\myFileName.txt", False
oACC.Close
oACC.Quit
set oACC = nothing

Open in new window

0
 

Author Closing Comment

by:mak345
ID: 40619877
Thank You!!
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question