Solved

Running access query with specs to .txt file

Posted on 2015-02-19
5
64 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
  • 2
  • 2
5 Comments
 
LVL 48

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now