VBA Access OutputTo without the table formatting - ms-access

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

Related

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.

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

Writing a single column from a table in MS Access VBA to .txt file

this is my first time posting a question here, but I almost always get good answers from searching this site. I'm trying to find out if there is a better way to speed up this process I have of writing a table column in Access to a .txt file. Here is the code I have, which works, but it's on the slow side. It takes about 45s to write around 7000 items.
lsFileName2 = "E:\DOI_Inventory2\SyncData\EquipUser.txt"
LiFileNumb = FreeFile
SysCmd acSysCmdSetStatus, "Updating User Equipment List"
Open lsFileName2 For Output As LiFileNumb
With rst
Do While Not .EOF
Write #LiFileNumb, ![EqUserFile]
.MoveNext
Loop
End With
dbs.Close
Close LiFileNumb
I'm fairly new to the IT field so any help would be greatly appreciated. Thanks
Just to add a note, the actual query is fine. I already checked that and it's pretty fast.
Create a query, save it with a name, and then use TransferText to export the query's data to your text file.
So assuming you have a SELECT query named qryExportMe like this which returns your table's column data correctly ...
SELECT EqUserFile
FROM YourTable;
... refer to this TransferText example and adapt it to fit your needs.
DoCmd.TransferText TransferType:=acExportDelim, _
Tablename:="qryExportMe", _
FileName:="E:\DOI_Inventory2\SyncData\EquipUser.txt", _
HasFieldNames:=True
Check the TransferText options at that linked page or from Access' built in help system.
Note you are not required to include the option names. I added them to help you keep track of which is which.
If this approach speeds up your export operation adequately, I think it will be because Access handles the task as a single set-based file write. Your recordset approach required Access to process one row at a time ... and one reason such approaches are called RBAR (row by agonizing row) is because they are often painfully slow.

Output MS Access Query to Excel with vba

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.

MS Access Copy Table Structure Only

I have a number of Linked (ODBC) tables in my MSAccess DB.
I what I want to do is create a copy of each table (preferably the Structure Only).
ive tried,
DoCmd.CopyObject , "NewTableName", acTable, "SourceTableName"
DoCmd.TransferDatabase acImport, "Microsoft Access", _
SrcDatabase, acTable, SrcTable, DstTable, StructureOnly
but these just seem to make a copy of the linked table.
think ive got it sorted, but ill leave it open incase someone has a better solution,
DoCmd.RunSQL ("Drop Table [LocalTable]")
DoCmd.RunSQL ("SELECT * INTO [LocalTable] FROM [ODBCTable];")
Had the same problem. Found out that the "DoCmd.Transferdatabase" worked, but had to use "1" instead of the keyword "True". And if the source table is a linked table the destination table appears as linked as well (with the arrow in the table list), although it really is in the local database.