473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Importing multiple sheets from a excel spreadsheet into multiple tables

3
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 7323
VBWheaties
145 100+
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
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 100+
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 TransferSpreadSheet of the Access.Application 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
Thanks for your reply....
i initially used the below code using TransferSpreadSheet 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
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...
9
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. ...
3
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...
1
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...
1
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...
0
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...
5
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...
0
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...
4
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.