Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 22nd, 2009, 03:43 PM
tech21 tech21 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 1 tech21 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 24 sec
Reputation Power: 0
Using VBA to select/copy xls data into Access

Hello all,

I've been trying numerous methods of getting an Excel worksheet into Access 2003 via VBA to no avail. Transferspreadsheet doesn't work (Excel data problems) but manually copying the same Excel data then Paste Appending into the Access table works fine.

So, what I'd like to do in VBA is to select and copy all the data in the first worksheet then Paste Append into a named table. I'm ok with establishing a connection to Excel and selecting the necessary xls but am stumped as to how you select and copy all data in the first worksheet. Also, no doubt it's possible, but how do you get the name of the worksheet via VBA?

Thanks in advance.

Chris

Reply With Quote
  #2  
Old September 23rd, 2009, 08:24 AM
dragonbite dragonbite is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Connecticut, USA
Posts: 42 dragonbite User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 16 m
Reputation Power: 3
Quote:
Originally Posted by tech21
Hello all,

I've been trying numerous methods of getting an Excel worksheet into Access 2003 via VBA to no avail. Transferspreadsheet doesn't work (Excel data problems) but manually copying the same Excel data then Paste Appending into the Access table works fine.

So, what I'd like to do in VBA is to select and copy all the data in the first worksheet then Paste Append into a named table. I'm ok with establishing a connection to Excel and selecting the necessary xls but am stumped as to how you select and copy all data in the first worksheet. Also, no doubt it's possible, but how do you get the name of the worksheet via VBA?

Thanks in advance.

Chris


What about going row-by-row?

Open the Excel sheet, copy the cell contents into a variable and insert it into the Access table?

Is all of the data formatted nicely for merging into Access?

I have Access VBA modules that takes data from Excel (one is nicely formatted, the other is a printable spreadsheet) and pull it into Access. I think I do it row-by-row.

Reply With Quote
  #3  
Old September 23rd, 2009, 04:06 PM
AlanSidman's Avatar
AlanSidman AlanSidman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Location: Lake County, IL
Posts: 175 AlanSidman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 6 h 38 m 20 sec
Reputation Power: 2
Here is code I use to import excel spreadsheet into a table in Access. Once you have it in a table, then the easy thing is to append it to another table that you desire to accumulate data to.
Code:
Private Sub Command16_Click()
'---------------------------------------------------------------------------------------
' Procedure : ImportXLSheetsAsTables
' Author    : jed
' Date      : 1/16/2009
' Purpose   : To import all worksheets in a specified workbook into
'             individual tables in Access.
' Tables get names: Tbl_ + name of the worksheet
'
' NOTE: Must have a reference to the Microsoft Excel Object Library
'---------------------------------------------------------------------------------------
'
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.Worksheet
Dim strValue As String

   On Error GoTo ImportXLSheetsAsTables_Error

Set appExcel = CreateObject("Excel.Application")
Set wb = appExcel.Workbooks.Open("C:\Documents and Settings\" & Environ("UserName") & "\Desktop\GDOCS.xls")
For Each sh In wb.Sheets
Debug.Print sh.Name
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "GDOCS", "C:\Documents and Settings\" & Environ("UserName") & "\Desktop\GDOCS.xls", True, sh.Name & "!"
Next

wb.Close
appExcel.Quit

   MsgBox "SpreadSheets Imported.  Continue"
    
   On Error GoTo 0
   Exit Sub

ImportXLSheetsAsTables_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ImportXLSheetsAsTables of Module Module9"



End Sub
__________________
Alan Sidman
__________
If I helped you, I'd like to know. Click on the icon next to the thread number and tell me.

Last edited by AlanSidman : September 23rd, 2009 at 04:16 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Using VBA to select/copy xls data into Access


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek