[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Import from Excel to Access

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

649 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