Import online xls data into MS access via VBA - 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.

Related

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

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 :-)

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.

Python script to run vba Error msg

I've posted the same question here
pyvot: can I run Excel VBA macros from python script?
I'm new to Python, so I do alot of searching.
Trying to Run a VBA project using snippets of Python code
Have encountered this error message, Win 7 based error I think.
Error message from PyScriptor reads as:
com_error:(-2147352567,'Exception occured',(0,"Microsoft \excel',
Excel cannot access 'MYFOLDERNAME,, The document may be read-only or
encrypted,", 'xlmain11.chm.,0,-2146827284),none)
Sorry about the forum errors, thought it was being helpfull, as I'm not taking the credit for this actual peiece of code.
import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\MYFOLDERNAME",ReadOnly=0)
xl.Application.Run("py_counter_test")
xl.Workbooks(1).Close(SaveChanges=1)
xl.Application.Quit()
xl=0
This the error I get, believe it's Win 7 issue.
I know I have not encrypted the folder, it is simply a "New Folder" in C:\
com_error:(-2147352567,'Exception occured',(0,"Microsoft \excel', Excel cannot access 'MYFOLDERNAME,, The document may be read-only or encrypted,", 'xlmain11.chm.,0,-2146827284),none)
Thanks
I saw the error,( over-sight ) left out the workbook name.
Amended code error.
import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="C:\MYFOLDERNAME\PY-COUNTER.xlsm",ReadOnly=0)
xl.Application.Run("Macro1")

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.

Identifying Excel filepath in SSIS

I'm using this ForEach loop tutorial in an attempt to import all my Excel files from a folder to an SQL table via SSIS. I followed the steps exactly but can't get the Excel source block to recognize the variable filename. The error I get shows that the filepath did not work at all:
Could not retrieve the table information for the connection manager ConnectionManagerName
The source file formatting, type, and folder are consistent. Has anyone else had this problem? I suspect I'm missing something between steps 2-4 in the link, but I wouldn't know what it is.
I've solved it! I used a ConnectionString in Step 3b instead of ExcelFileName. For anyone reading this answer, the exact ConnectionString format will vary (see here) but will be something like
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";