Solved

Import from Excel to Access

Posted on 2011-03-02
2
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

739 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