Solved

Import data in access from Excel using vba

Posted on 2014-04-03
11
698 Views
Last Modified: 2014-04-09
Hi I need to import or link to an excel file worksheet. One file can have many worksheet, i need to be able to link or import particular worksheet in access 2013.

I use the following code:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_Data", c:\file.xlsx, True, "My Dates!"

thanks for help
0
Comment
Question by:maximyshka
  • 7
  • 4
11 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39976104
That is VBA.  What is wrong with it?  Are you asking to do the import with OLE automation?  Why would you do that if the TransferSpreadsheet works for your situation?  There is no advantage to using OLE automation and in fact there are disadvantages.  The only reason you would use automation is if the TransferSpreadsheet didn't work because you wanted to import individual cells at specific locations rather than importing a whole sheet or named range.
0
 

Author Comment

by:maximyshka
ID: 39976122
I have a different problem now, unrelated to my previous questions.
I have excel files with many worksheets in them. For example one file might have worksheet1, worksheet2, worksheet3... I need to import in access only worksheet3. The code I provided does not work . It only works if i do not specify worksheet. In that case it takes only worksheet1
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39976328
Are you sure your Worksheet name is correct?  I thought they couldn't contain spaces or special characters?
0
 

Author Comment

by:maximyshka
ID: 39976415
Yes it is correct. The do contain spaces.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39976744
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tbl_Data", "c:\file.xlsx", True, "My Dates!"

The file name was missing quotes.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:maximyshka
ID: 39978100
I use string in real code, it does not work
0
 

Author Comment

by:maximyshka
ID: 39978178
the problem is that I have sheet names with spaces . The code is working when sheet name does not have spaces. Is there any way to rename the sheets in excel file using access vba?
0
 

Author Comment

by:maximyshka
ID: 39978232
strange when i create worksheet named "sheet 2!" it is working, but it doesn't work with worksheet named "Add Bills-Electricity!" . I've attached template i am getting. In case any one can help.

I need to make a link table or import data doesn't metter. when i use it with current sheet names i get
error 3129, invalid SQL statement. Expected DELETE, INSERT, PROCEDURE, SELECT, UPDATE.

thanks,
Max
test-file.xlsx
0
 

Accepted Solution

by:
maximyshka earned 0 total points
ID: 39978614
I got the answer " 'Add Bills-Electricity!' " - it works .
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39978677
Just another of the little problems caused by using object names with embedded spaces or special characters.  I knew when I saw your statement that the problem was with the sheet name.  Glad you found the solution and thanks for posting back.
0
 

Author Closing Comment

by:maximyshka
ID: 39988243
I found the answer which works
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

929 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

11 Experts available now in Live!

Get 1:1 Help Now