Solved

Import from Excel to Access

Posted on 2011-03-02
2
294 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

708 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

15 Experts available now in Live!

Get 1:1 Help Now