Output MS Access Query to Excel with vba - ms-access

Working with MS Access 2007, I have a query I'd like to run and export the results to a specific workbook in a saved Excel workbook. I have the following code written using DoCmd. First I open the query (this works) and then I try to output the results to excel.
DoCmd.OpenQuery "MyQueryName", acViewNormal, acEdit
DoCmd.OutputTo acOutputQuery, "Aging By Desk - Onboarding Team", acFormatXLS, _
"filepath.SuperTest.xls", "SuperTest.xls", True
However, when this code is run, I get the following error message: "An Expression you entered is the wrong data type for one of the arguments". I've been playing around with each argument, but can't seem to locate the problem. Any ideas? Am I on the right path?

You've got too many arguments. From Microsoft's website:
expression.OutputTo(ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding)
Take out one of those Excel filenames you have and it should work.

Related

VBA Access OutputTo without the table formatting

I'm having a lot of trouble trying to export a table (actually a query, but I made it create this table just to see if something changed) to a .csv file, I was using this line to make it:
DoCmd.TransferSpreadsheet acExport, , "TABLE", "TEST.csv", False
But it gives me error 3027 (something about read-only database or object), but I created this table, and as far as I'm concerned, it is possible to write on it!
Then I tried using this line here:
DoCmd.OutputTo acOutputTable, "TABLE", acFormatTXT, "test.csv"
And it works just fine. But When I came to see the results, they came with a table drawn around my data, and it's just horrible!
Anyways, it doesn't matter really which command I want, I'd appreciate any help as long as it works. Thanks!
TransferSpreadsheet is for spreadsheet files. Try:
DoCmd.TransferText acExportDelim, , "TABLE", "d:\path\TEST.csv", False

Access VBA Macro to run pass through query

I have a pass through query built in Teradata set to export data to an Excel spreadsheet. I'm trying to automate it, but when I run the macro or open the query, a window pops up asking for the data source. I have an ODBC connection created and I'm thinking there has to be a way to make the macro pass the data source name so it will run without interaction.
Edit: Adding Macro as requested
Function AutoExec()
On Error GoTo AutoExec_Err
DoCmd.OutputTo acOutputQuery, "Performance Interval Data", "ExcelWorkbook(*.xlsx)", _
"filepath\filename.xlsx", False, "", , acExportQualityPrint
DoCmd.Quit acExit
AutoExec_Exit:
Exit Function
AutoExec_Err:
MsgBox Error$
Resume AutoExec_Exit
End Function
Couple of concerns, (can't validate any of this right now as I do not currently have access to Access for testing), but it looks like:
You're trying to OutputTo a query, to the best of my knowledge that
is not feasible.
Your file path is setup as filepath\filename.xlsx unless that is the actual location and name of your Excel sheet, something seems
wrong there to me.
I don't really think this macro relates to an ODBC of any sort in its current state.
But, you should at least start with fixing the filepath issue. That should be the full path to your Excel file and the full name of the file as well. (i.e. C:\TEMP\TestExcelSheet.xlsx)
All that being said, you may want to just go with something like this (although its a little difficult to tell if this is what you actually want or not):
'Export Excel file from Query
DoCmd.TransferSpreadsheet acExport, , "acOutputQuery", _
"C:\TEMP\TestExcelSheet.xlsx", True
NOTE: "acOutputQuery" should be the actual name of your passthrough query, "C:\TEMP\TestExcelSheet.xlsx" would be your destination path, and True adds the query's headers into the sheet, False to ignore the headers.

OpenQuery action was cancelled. Error code 800A09C5

Up to a couple days ago I had this very simple script running just fine;
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "\\mynetwork\myconnection\mydatabase.accdb"
appAccess.DoCmd.OpenQuery "Update", acViewNormal, acEdit
Set appAccess = Nothing
Unfortunately now, all of a sudden, throws me an error message:
I've tried Googling some solutions but it seems that the error code 800A09C5 is rather uncommon.
The interesting part is that this other code works fine, so I'm really confused as to why I'm getting the above error.
SET oAcc = CREATEOBJECT("Access.Application")
oAcc.OpenCurrentDatabase "\\mynetwork\myconnection\mydatabase.accdb"
oAcc.DoCmd.OutputTo acOutputTable, "PR_NOTES", "Excel97-Excel2003Workbook
(*.xls)", "\\mynetwork\OutputFiles\File_" & replace(FormatDateTime
(now,2),"/","-") & "at" & replace(FormatDateTime(now,4),":","-") &".xls", False, "",
, acExportQualityPrint
Any insight is appreciated.
This code line of:
appAccess.DoCmd.OpenQuery "Update", acViewNormal, acEdit
will not work for several reasons. First up the code constants of acViewNomral and code constant of acEdit are NOT part of vbScript, but are constants from Access. Vb scripting has NO idea what the value of these constants are. So you have to replace the constants with the actual values from Access. (so in the debug window in access you print out the constants by type in
? acViewNormal
And
? acEdit
Now a quick test shows above constants to be “0”, so you would replace the constants with a 0 (no quotes).
Next up your command is failing because you opening the query in EDIT mode – that mode is to edit and modify the query – not run the query.
Next up and a greater issue is when you use docmd. To execute a query, you will receive “dialog” boxes such as do you want to update all these rows yes/no – because of script and automation you will be un-able to click ok and answer yes to those prompts. You can change setwarnings, but most easy to just use the execute method.
So in the syntax of
appAccess.DoCmd.OpenQuery "Update"
should work – but then those nasty prompts such as "you are about to update xxx rows etc. will appear - and you can't answer those prompts in the script.
Better to use the execute method, and thus use
appAccess.currentdb.Execute "Update"

MS Access unable to locate my excel range when importing

I have this code that works on one spreadsheet, but not another. I am just trying to automate the transfer of an excel data range to an access table, like so
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "UsysFastTrack", strFilePath, False, strRange
strFilePath and strRange are just strings that contain the full file path (including the worksheet name and extension) and the name of an excel range in the worksheet, respectively. This line causes an error
The Microsoft Access database engine could not find the object ...
This error appears a lot online and somewhere I saw the advice to try the import wizard to see what I get and lo and behold, right as I hit the last Next
This is the exact same error and it stops me dead in my tracks. What's funny is that you can see the range exists in the spreadsheet before your very eyes.
What's going on here?
Looks like the names of your named ranges are not valid, they are similar to regular ranges. Try to change names

The text file specification does not exist

I'm trying to run a simple access program that exports data from a select query. However I keep getting the error:
Run-time error '3625'
The text file specification 'Deposits Link Specification' does not exist. You cannot import, export, or link using the specification.
I didn't set up a text file specification, mainly because I haven't had to do this before. How would I go about fixing this?
My simple code, is below:
'*************************************************************************
Public Function startupdate()
'*************************************************************************
DoCmd.SetWarnings False
DoCmd.TransferText acExportDelim, , "DepositsToChecklist", "X:\InHouseApps\SummerCamps\TMS\EEChecklistImport.csv", True
DoCmd.OpenQuery "qryAppendDepositsToChecklist"
DoCmd.Quit
End Function