473,609 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Importing multiple sheets from a excel spreadsheet into multiple tables

3 New Member
hello all,

I am trying to code, but i am just stuck after importing one sheet. so here is the gist of what i need help with.

In a workbook at the start of the year (january) i will have 4 sheets and these sheets will keep increasing to 12 when the month is december. so i want is, sheet 1 (named abc) should go into table 1 (named abc), sheet 2 (named def)should go into table 2 (named def), sheet 3 (named xyz) should go into table 3 (named xyz). then sheet 4 through the next sheets till sheet 12 (named balance1, balance2, abalance3...so on till balance12) should all go into one table 'table4' (named balance) and all these sheets should keep appending starting from 1 then 2 all the way to 12.

I am able to put one sheet into one table and below is the code....i need help with the other part of my requirement

Expand|Select|Wrap|Line Numbers
  1. Dim cn As ADODB.Connection
  2. Dim oRs As New ADODB.Recordset
  3. Dim cnAccess As ADODB.Connection
  4. Dim rsAccess As New ADODB.Recordset
  5.  
  6.  
  7. ' Open Excel Connection
  8. Set cn = New ADODB.Connection
  9. With cn
  10.     .Provider = "Microsoft.Jet.OLEDB.4.0"
  11.     .ConnectionString = "Data Source=C:\Test.xls;" & _
  12.     "Extended Properties=Excel 8.0;"
  13.     .Open
  14. End With
  15.  
  16. ' Open Access Connection
  17. Set cnAccess = New ADODB.Connection
  18. With cnAccess
  19.     .Provider = "Microsoft.Jet.OLEDB.4.0"
  20.     .ConnectionString = "Data Source=C:\Documents and Settings\krishnam\Desktop\Intercompany Consolidation.mdb;"
  21.     .Open
  22. End With
  23.  
  24.  ' Load ADO Recordset with Excel Sheet1Data
  25.  
  26. oRs.Open "Select * from [abc$]", cn, adOpenStatic
  27. MsgBox oRs.RecordCount
  28.  
  29.  
  30. ' Load ADO Recordset with Access Data
  31. rsAccess.Open "select * from tbl_abc", cnAccess, adOpenStatic, adLockOptimistic
  32. MsgBox rsAccess.RecordCount
  33.  
  34. 'Synchronize Recordsets and Batch Update
  35. Do While Not (oRs.EOF)
  36.         rsAccess.AddNew
  37.         For i = 0 To 11   -----11 columns in table 1
  38.         rsAccess.Fields(i).Value = oRs.Fields(i).Value
  39.         Next
  40.         rsAccess.Update
  41.         oRs.MoveNext
  42.  
  43. Loop
  44.  
  45. End Sub
  46.  
  47.  

please help me so that i can move forward...
Feb 28 '08 #1
4 7345
VBWheaties
145 New Member
Your sheets names are used as table names in the SELECT.

Do you need an idea about how to write what you need?

I would make a sub routine and just call the subroutine for every sheet in the excel workbook.

Heres an example sub routine (bold is code I added):

Expand|Select|Wrap|Line Numbers
  1. Public Sub ReadInExcelSheet(szExcelFileName As String, szSheetName As String, szDestinationTableName As String) 
  2.  
  3. Dim cn As ADODB.Connection
  4. Dim oRs As New ADODB.Recordset
  5. Dim cnAccess As ADODB.Connection
  6. Dim rsAccess As New ADODB.Recordset
  7.  
  8.  
  9. ' Open Excel Connection
  10. Set cn = New ADODB.Connection
  11. With cn
  12.     .Provider = "Microsoft.Jet.OLEDB.4.0"
  13.     .ConnectionString = "Data Source=" & szExcelFileName & ";" & _
  14.     "Extended Properties=Excel 8.0;"
  15.     .Open
  16. End With
  17.  
  18. ' Open Access Connection
  19. Set cnAccess = New ADODB.Connection
  20. With cnAccess
  21.     .Provider = "Microsoft.Jet.OLEDB.4.0"
  22.     .ConnectionString = "Data Source=C:\Documents and Settings\krishnam\Desktop\Intercompany Consolidation.mdb;"
  23.     .Open
  24. End With
  25.  
  26.  ' Load ADO Recordset with Excel Sheet1Data
  27.  
  28. oRs.Open "Select * from [" & szSheetName & "$]", cn, adOpenStatic
  29. MsgBox oRs.RecordCount
  30.  
  31.  
  32. ' Load ADO Recordset with Access Data
  33. rsAccess.Open "select * from " & szDestinationTableName & ", cnAccess, adOpenStatic, adLockOptimistic
  34. MsgBox rsAccess.RecordCount
  35.  
  36. 'Synchronize Recordsets and Batch Update
  37. Do While Not (oRs.EOF)
  38.         rsAccess.AddNew
  39.         For i = 0 To 11   -----11 columns in table 1
  40.         rsAccess.Fields(i).Value = oRs.Fields(i).Value
  41.         Next
  42.         rsAccess.Update
  43.         oRs.MoveNext
  44.  
  45. Loop
  46. End Sub
  47.  
Feb 28 '08 #2
Harshe
3 New Member
Thanks a lot for your quick reply. I am a newbee here. so i did see your comments, but i am not sure how this would work as All sheets do not have same number of columns. the piece of code that says 'Synchronize Recordsets and Batch Update' that was for only 1 sheet and not for all. so maybe i didnt understand what you were trying to say.

thanks.

Your sheets names are used as table names in the SELECT ...
Feb 28 '08 #3
VBWheaties
145 New Member
Thanks a lot for your quick reply. I am a newbee here. so i did see your comments, but i am not sure how this would work as All sheets do not have same number of columns. the piece of code that says 'Synchronize Recordsets and Batch Update' that was for only 1 sheet and not for all. so maybe i didnt understand what you were trying to say.

thanks.
You never said columns vary. If columns vary, your problem becomes more complex.

My solution would be to use TransferSpreadS heet of the Access.Applicat ion object. First, set a reference to Microsoft Access x.0 Object Library where x is the number corresponding to your version.

Then, here is an example of how I would do:

Expand|Select|Wrap|Line Numbers
  1.     Dim acc As Access.Application
  2.  
  3.     Set acc = New Access.Application
  4.  
  5.     acc.OpenCurrentDatabase "C:\MyData.MDB"   'replace with your mdb path
  6.  
  7.     acc.DoCmd.TransferSpreadsheet acImport, _                      
  8.                                 acSpreadsheetTypeExcel97, _                
  9.                                 "SpreadSheetName", _                   'Replace with name of the spread sheet
  10.                                 "C:\excelData.xls"  'replace with path of the xls file. 
  11.  
  12.     acc.Quit
  13.     Set acc = Nothing
  14.  
Feb 29 '08 #4
Harshe
3 New Member
Thanks for your reply....
i initially used the below code using TransferSpreadS heet to make it work, but my problem with that code was that it used up a lot of resources and after it imported some tabs, it used to give 'out of memory' error and hence my manager told me to write using different style. the code is as below...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim aryList() As String
  3. Dim strTable, strJunk As String
  4. Const acImport = 0
  5. Const acSpreadsheetTypeExcel9 = 8
  6. Set objAccess = CreateObject("Access.Application")
  7. objAccess.OpenCurrentDatabase "C:\Documents and Settings\sanghvih\Desktop\December07_AccessOOBData_01_07_08\transferData.mdb"
  8.  
  9. Set objExcel = CreateObject("Excel.Application")
  10. objExcel.Visible = True
  11.  
  12. strFileName = "C:\Documents and Settings\sanghvih\Desktop\December07_AccessOOBData_01_07_08\December07_AccessOOBData_01_07_08.xls"
  13.  
  14. Set objWorkbook = objExcel.Workbooks.Open(strFileName)
  15. Set colWorksheets = objWorkbook.Worksheets
  16.  
  17. For Each objWorksheet In colWorksheets
  18.     Set objRange = objWorksheet.UsedRange
  19.     strworksheetname = objWorksheet.Name & "!" & objRange.Address(False, False)
  20.     strsheet = ""
  21.     strsheet = strworksheetname
  22.     aryList = Split(strsheet, "!", , vbTextCompare)
  23.     intupper = UBound(aryList)
  24.     strJunk = ""
  25.     strTable = ""
  26.     For a = 0 To UBound(aryList)
  27.         If a = UBound(aryList) Then
  28.             strJunk = aryList(a)
  29.         Else
  30.             strTable = aryList(a)
  31.         End If
  32.     Next a
  33.  
  34.     If strTable = "All_GL_Accts" Then
  35.         objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
  36.        "tbl_Chart_of_Accounts", strFileName, True, strworksheetname
  37.     ElseIf strTable = "BU_Legal_Consol" Then
  38.         objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
  39.        "tbl_Business_Units", strFileName, True, strworksheetname
  40.     ElseIf strTable = "ELIM Sets" Then
  41.         objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
  42.        "tbl_Elimination_Sets", strFileName, True, strworksheetname
  43.  
  44.     Else
  45.         objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
  46.        "tbl_Ledger_Balances_Combined", strFileName, True, strworksheetname
  47.     End If
  48.  
  49. Next
  50.  
  51.  
  52. End Sub
  53.  
  54.  
  55.  


Also each tab has around 18K rows to import....
Mar 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1282
by: Doug Beacher | last post by:
I need to import data from a spreadsheet that is created new every day into an MS Access database. The data is in a workbook consisting of multiple work sheets. I plan to import using the TransferSpreadsheet method. The name of the spreadsheet will change every day, but the position of the data to be imported will always be the same. Here is my problem: Having never used this method before, I am unsure as to what to put in the...
9
3908
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. There is no further explanation. What are the kinds of things that make this happen? Thanks from an obvious rookie. Gordon
3
8915
by: Conrad F | last post by:
Hello All, I know how to import a specific named excel sheet into a datagrid using ADO.NET by setting up a JET connection and then SELECTing data from the sheet. However, for a real world application, I would typically not know what the names of the sheets are going to be before I open the Excel file. I am wondering if there is a SELECT statement that would get me the list of sheet names so that I can place them in a combobox and
1
2450
by: Geoff Jones | last post by:
Hi I have a question which I hope somebody can answer. I have written a VB application with which I want to import an Excel file, analyze the data within it and do some calculations. There are in fact five sheets in the Excel file. My original idea was to import the file into access and create a database file; which I did and worked beautifully. It generated five tables in the database as expected. However, I then thought why not...
1
2797
by: madeleine.macphail | last post by:
All I'm currently attempting to move us from a spreadsheet based system to a database system. The first phase is to import the data on a regular basis from the spreadsheets to get the database working the way we need it to prior to switching users over to it. Each team has a workbook, each workbook contains multiple worksheets, only some of the sheets contain the ranges that I want to import.
0
2996
by: Anish G | last post by:
Hi All, I am getting the below given error while running my application in live server. In my local machine, its working fine. Please help me as it is very urgent for me. Exception from HRESULT: 0x800A03EC Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details:...
5
6400
by: rewalk | last post by:
Hello all, I've set up protected Excel sheets that users can paste data into. I then need to be able to pull the data they paste into multiple access tables and then reset the Excel spreadsheet back to the template. For example, the first few columns go to the first table, the next 3 into a second table and so on. Is this possible? I can't think of a way to do it. Thanks in advance for all your help!
0
767
by: DrewYK | last post by:
I have the project that may never end in front of me. I am creating a routine that will take SpreadSheets from Excel and bring them into Access. I am not using any "DoCmd"s because the goal is for the import code to be moved to a stand alone VB app which will use the Access DB as a workspace to process the data from the spreadsheets. Quite honestly, done right this may not even require Access or Excel to be on the users machine. ...
4
4919
by: Chloe C | last post by:
Hi I've got an Ingres database of some 200 tables which I need to import every night into SQL Server 2005 for use by Reporting Services. Most of the tables will come across unchanged (a few need massaging to handle time intervals correctly), but the Import Wizard only seems to want to import one table (or more accurately query) at a time. I seem to remember the old 2000 Import Wizard handled multiple tables - is there any way of...
0
8121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8559
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8519
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8208
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4010
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4068
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.