Why do I get error 3073 in VBA when using "TransferSpreadsheet acImport" but not when using "TransferSpreadsheet acLink" - sql

I just want to import a number of Excel sheets into MS Access to combine data from several months (each in one .xlsx file). Got it to work as long as I link the .xlsx files using
DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, tableName, fileName, True, "A4:L23"
but not when using acImport option
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tableName, fileName, True, "A4:L23"
I get an 'error 3073 - Operation must use an updatable query' when I change the option from Link to Import.
As I want to add a column containing the month and year for which the data applies, I need to be able to add a variable which i cannot do using linked tables.
Thanks for your suggestions or an explanation of what I am doing wrong.
KR,
Martin

You probably need to use acSpreadsheetTypeExcel12Xml:
AcSpreadSheetType enumeration (Access)

Andre and Gustav,
thanks to both of you for your help!! Really appreciate it!
I thought I should test the code in a new Access DB and the code does work there. The issue seems to be caused when you switch between linking and importing in the same database.
In the new DB I can switch between importing and linking (acLink/acimport in the DoCmd.TransferSpreadsheet statement), but imported or linked tables must be deleted in Access BEFORE running the vba code. Maybe that is obvious to most of you, but was not to me :-)

Related

Access VBA - Export table to CSV file

I know many variations of this question were already asked but I kinda hit a dead end. I am trying to export a table with 2 columns into a .csv file. I tried it using similar question with this line of code.
Once without specification:
DoCmd.TransferText acExportDelim, , "+SapDebExp", "U:\Desktop\sapDeb1.csv", True
Once with specification:
DoCmd.TransferText acExportDelim, a11, "TSapDebExp", "U:\Desktop\sapDeb.csv", True
But no matter what I try all the columns are compressed into a single column in the created .csv
I also tried a workaround with this:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TSapDebExp", "U:\Desktop\sapDeb.xlsx", True
FileCopy "U:\Desktop\sapDeb.xlsx", "U:\Desktop\sapDeb.csv"
This worked and the columns were separated in the end file but when you first open the .csv there comes a pop-up message saying "you sure you want to trust this file". This wouldn't be a problem since you only need to click it once but I want to import this file somewhere else (so no one will open the .csv file between the export and import). Is there another workaround or am I doing something wrong with the first method?

Access VBA Convert Linked Table to Local

I am running into a problem and can use some advice. I am linking to an excel spreadsheet and then i am trying to convert that link into a table.
my very basic function is as follows
dim mypath as string
docmd.transferspreadsheet aclink, acspreadsheettypeexcel112xml, "importeddata", mypath, true
runcommand acCmdConvertLinkedTableToLocal
this gives me a runtime error 2046
The command or action 'ComvertLinkedTableToLocal' isn't available now.
So, the reason for doing the link and then the conversion is when done manually, it will get rid of all the conversion errors that a normal import will do and then I have my basic VBA scripts which seem to run far quicker in Access than Excel.
Again, any help is duly duly appreciated
Groundhog.
I believe you need to have a specific linked table selected in order to run the acCmdConvertLinkedTableToLocal command. Try something like this after linking:
DoCmd.SelectObject acTable, "importeddata", True
DoCmd.RunCommand acCmdConvertLinkedTableToLocal
Alternatively, have you tried creating an empty table that matches the spreadsheet structure (i.e. using text fields instead of numeric fields) so that you don't get the import errors when you import instead of link?
Thank you for sharing this code example. I had to make one addition to get this to work correctly for me. I was getting a runtime message that the command was not available before adding a DoEvents after the SelectObject and prior to acCmdConvertLinkedTableToLocal.

VBA_Excel Into Access

I am trying to use VBA to import an excel file (which I have on a local drive) into Microsoft Access.
Currently this is the code that I have, but I know that it is incorrect:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "example", selectFile, True
Can someone please help?
The complete template for a spreadsheet transfer is as such:
expression.TransferSpreadsheet(TransferType, SpreadsheetType, TableName, _
FileName, HasFieldNames, Range, UseOA)
where TransferType, in your case, is acImport and SpreadsheetType is acSpreadsheetTypeExcel12Xml (assuming it is, in fact, an Office 2010 XML file).
TableName would be the name of the table in the Access database that you're importing your spreadsheet into, FileName is the name (including the full path) of the file you're importing. HasFieldNames refers to whether or not the first row in your spreadsheet contains field names or not. Range would be used if you intended to only import a specific range from your spreadsheet, and UseOA is no longer supported.
Since you give no specific information as to your file name, table name or anything else, all I can offer is this "example" code. You will have to replace your information in it yourself. Note that Range is optional, if you don't want to input a specific range then simply delete that piece.
DoCmd.TransferSpreadsheet acImport, 10, "MyTableName", _
"C:\MyDocuments\MyFile.xls", True, "A1:G12"
Note that SpreadsheetType can use a numerical value instead of the full text value, both are acceptable.

Export Access Query WITHOUT Formatting

Relatively simple, but I can't seem to work it out. I want to export a query from access into a .csv (tab or comma delimited). When I do it manually through the wizard it works fine. But when I do it via vba, it comes complete with dash formatting that looks like the borders in the table!
I tried two methods and got the same results
DoCmd.OutputTo acOutputQuery, "Qry_GRADE", "MS-DOSText(*.txt)",_
"grade.csv", True, *ExportSpec*, , acExportQualityScreen
I used it with or without "ExportSpec", which is a specification I created when exporting manually.
This is the second method:
Dim testSQL As String
Dim qd As DAO.QueryDef
testSQL = "SELECT * FROM Qry_Grade"
Set qd = db.CreateQueryDef("tmpExport", testSQL)
DoCmd.TransferText acExportDelim, , "tmpExport",_
"C:\Users\Databoe\Documents\KidsTV\grade.csv"
db.QueryDefs.Delete "tmpExport"
This is a solution I've found which seems like overkill
And this is what the output looks like:
You can see it's not actually split any of the columns when opening the file in excel and that every second line is just a string of "-"'s
What about DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, myQueryName, myExportFileName, True for direct excel file export.
I tried your approaches, but I only get formated text with your first try DoCmd.OutputTo acOutputQuery, "Qry_GRADE", "MS-DOSText(*.txt)",_
"grade.csv", True, *ExportSpec*, , acExportQualityScreen which is as expected because it's a text export not csv.
With your second method I always get an excel compatible result. Maybe you have an error trap that hides an error and the first grade.csv is not overwritten. Use a different filename for the second method to prevent that.
Found a second trap. You don't specify full file path in first method, but in second. If C:\Users\Databoe\Documents\KidsTV is not your default document path, you have 2 grade.csv in different folders, but you maybe think that you only have one that gets overwritten.
I just ran into this problem myself, and found a great work around. It doesn't save as a .csv, but you can save as a comma delimited .txt file.
Use the export wizard on the External Data tab to export your query as a .txt file without formatting.
Once the file is exported you get a dialogue box asking if you want to save export steps. Click the box and save the export.
There is an action available in the Macro wizard called "Run Saved Import/Export." Select this action and choose your saved export from the dropdown menu.
Very frustrating that even now I cant seem to make Access export a simple csv file. I do not know why they think I need pretty formatting. Try this: open Excel, Click Get Data, From Database, From MicroSoft Access Database. Select the Access Database you wish to export from. Select the table/query we want saved as an csv. This will set up a link to this table. Once imported, save the Excel file to an csv file.

Import online xls data into MS access via VBA

I need to import exchange rate data stored in an Excel spreadsheet online into an Access data table. However first I need to manipulate it so I would like to import it into an array and then write the array to the table. The code I used for Excel doesn't seem to work with Access...
Dim arr as variant
Workbook.Open ("http://www.rba.gov.au/statistics/tables/xls-hist/f11hist.xls")
arr=activeworkbook.worksheets("Data").Range("A12:X" & Range("A1045876").end(xldown).Row)
'data manipulation ommitted
'add to data table
Clearly this doesn't work in Access, but I've got no idea how to open the file and read the data. Any help appreciated!
Your question is overly broad, so the answer is generic. You can use Microsoft Excel object library in MS Access application by adding the reference to that library and start using its methods, similar to what you have done in Excel. More details in: https://msdn.microsoft.com/en-us/library/office/ff194944.aspx. Hope this may help.
I guess this line can help you.
Docmd.Transferspreadsheet acImport,,"name of excel table","link to find the table (ex:c:.....)",true
that will help to take a excel table and import its on access .
Excel does this by - behind the scene - first downloading the file then reading it.
Access can't do this, but you can use VBA to download the file Download file from URL and then create a link to a Worksheet or a Named Range in it. Or open the file via automation.