Solved

Import from Excel to Access

Posted on 2011-03-02
2
295 Views
Last Modified: 2012-05-11
I am using the following below to import from an Excel worksheet to Access 2003.

I want it to always grab the first sheet in the workbook but the first sheet always has a different name. How can I specific this?

DoCmd.TransferSpreadsheet , acSpreadsheetTypeExcel5, "tmp_Import", FilePath, False, "Sheet1!A3:F100"
0
Comment
Question by:Pdeters
2 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 35018001
Hi Pdeters,
Create a function in your code like this

Function GetSheetName$(strWorkBookPath$, intSheetIndex%)
'*********************************************************************
' Get name of XL sheet specified by the index
'*********************************************************************
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
 
 Set objXL = New Excel.Application
 
   objXL.Visible = False
   Set objWkb = objXL.Workbooks.Open(strWorkBookPath$)
   Set objSht = objWkb.Worksheets(1)
   GetSheetName$ = objSht.Name
   
 Set objSht = Nothing
 Set objWkb = Nothing
 Set objXL = Nothing
 
'*********************************************************************
End Function

Modify your subroutine that calls the DoCmd.Transfer to be something like this
Private Sub cmdGetSheetName_Click()
   
    Dim strSheetNameToImport$
    Dim strFullSheetRangeName$
    Dim FilePath As String
   
    ' Full Path\File Name of spreadsheet
    FilePath = "C:\Finance_Data\2010_Costings.xlsx"
   
    ' Get the name of first sheet in the spreadsheet
    strSheetNameToImport$ = GetSheetName$(FilePath, 1)
   
    ' Use sheet name to construct the full XL range reference
    strFullSheetRangeName$ = strSheetNameToImport$ & "!A3:F100"
   
    ' Do the import using strFullSheetRangeName$ as the Range parameter
    DoCmd.TransferSpreadsheet , acSpreadsheetTypeExcel5, "tmp_Import", FilePath, False, strFullSheetRangeName$

End Sub

In the Code Window go to the Tools and then References and choose Microsoft Excel xx.0 Object Library
0
 

Author Closing Comment

by:Pdeters
ID: 35020229
Perfect.
Thanks
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auto Filter in Combo Box 7 33
Parse last 2 characters of text value if they are letters 7 17
Export Query data to excel file 14 37
Access 2010 Query Syntax 5 23
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

867 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

21 Experts available now in Live!

Get 1:1 Help Now