?
Solved

Import from Excel to Access

Posted on 2011-03-02
2
Medium Priority
?
300 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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.
Suggested Courses

771 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