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 -
Dim cn As ADODB.Connection
-
Dim oRs As New ADODB.Recordset
-
Dim cnAccess As ADODB.Connection
-
Dim rsAccess As New ADODB.Recordset
-
-
-
' Open Excel Connection
-
Set cn = New ADODB.Connection
-
With cn
-
.Provider = "Microsoft.Jet.OLEDB.4.0"
-
.ConnectionString = "Data Source=C:\Test.xls;" & _
-
"Extended Properties=Excel 8.0;"
-
.Open
-
End With
-
-
' Open Access Connection
-
Set cnAccess = New ADODB.Connection
-
With cnAccess
-
.Provider = "Microsoft.Jet.OLEDB.4.0"
-
.ConnectionString = "Data Source=C:\Documents and Settings\krishnam\Desktop\Intercompany Consolidation.mdb;"
-
.Open
-
End With
-
-
' Load ADO Recordset with Excel Sheet1Data
-
-
oRs.Open "Select * from [abc$]", cn, adOpenStatic
-
MsgBox oRs.RecordCount
-
-
-
' Load ADO Recordset with Access Data
-
rsAccess.Open "select * from tbl_abc", cnAccess, adOpenStatic, adLockOptimistic
-
MsgBox rsAccess.RecordCount
-
-
'Synchronize Recordsets and Batch Update
-
Do While Not (oRs.EOF)
-
rsAccess.AddNew
-
For i = 0 To 11 -----11 columns in table 1
-
rsAccess.Fields(i).Value = oRs.Fields(i).Value
-
Next
-
rsAccess.Update
-
oRs.MoveNext
-
-
Loop
-
-
End Sub
-
-
please help me so that i can move forward...
4 7323
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): - Public Sub ReadInExcelSheet(szExcelFileName As String, szSheetName As String, szDestinationTableName As String)
-
-
Dim cn As ADODB.Connection
-
Dim oRs As New ADODB.Recordset
-
Dim cnAccess As ADODB.Connection
-
Dim rsAccess As New ADODB.Recordset
-
-
-
' Open Excel Connection
-
Set cn = New ADODB.Connection
-
With cn
-
.Provider = "Microsoft.Jet.OLEDB.4.0"
-
.ConnectionString = "Data Source=" & szExcelFileName & ";" & _
-
"Extended Properties=Excel 8.0;"
-
.Open
-
End With
-
-
' Open Access Connection
-
Set cnAccess = New ADODB.Connection
-
With cnAccess
-
.Provider = "Microsoft.Jet.OLEDB.4.0"
-
.ConnectionString = "Data Source=C:\Documents and Settings\krishnam\Desktop\Intercompany Consolidation.mdb;"
-
.Open
-
End With
-
-
' Load ADO Recordset with Excel Sheet1Data
-
-
oRs.Open "Select * from [" & szSheetName & "$]", cn, adOpenStatic
-
MsgBox oRs.RecordCount
-
-
-
' Load ADO Recordset with Access Data
-
rsAccess.Open "select * from " & szDestinationTableName & ", cnAccess, adOpenStatic, adLockOptimistic
-
MsgBox rsAccess.RecordCount
-
-
'Synchronize Recordsets and Batch Update
-
Do While Not (oRs.EOF)
-
rsAccess.AddNew
-
For i = 0 To 11 -----11 columns in table 1
-
rsAccess.Fields(i).Value = oRs.Fields(i).Value
-
Next
-
rsAccess.Update
-
oRs.MoveNext
-
-
Loop
-
End Sub
-
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 ...
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: -
Dim acc As Access.Application
-
-
Set acc = New Access.Application
-
-
acc.OpenCurrentDatabase "C:\MyData.MDB" 'replace with your mdb path
-
-
acc.DoCmd.TransferSpreadsheet acImport, _
-
acSpreadsheetTypeExcel97, _
-
"SpreadSheetName", _ 'Replace with name of the spread sheet
-
"C:\excelData.xls" 'replace with path of the xls file.
-
-
acc.Quit
-
Set acc = Nothing
-
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... -
-
Dim aryList() As String
-
Dim strTable, strJunk As String
-
Const acImport = 0
-
Const acSpreadsheetTypeExcel9 = 8
-
Set objAccess = CreateObject("Access.Application")
-
objAccess.OpenCurrentDatabase "C:\Documents and Settings\sanghvih\Desktop\December07_AccessOOBData_01_07_08\transferData.mdb"
-
-
Set objExcel = CreateObject("Excel.Application")
-
objExcel.Visible = True
-
-
strFileName = "C:\Documents and Settings\sanghvih\Desktop\December07_AccessOOBData_01_07_08\December07_AccessOOBData_01_07_08.xls"
-
-
Set objWorkbook = objExcel.Workbooks.Open(strFileName)
-
Set colWorksheets = objWorkbook.Worksheets
-
-
For Each objWorksheet In colWorksheets
-
Set objRange = objWorksheet.UsedRange
-
strworksheetname = objWorksheet.Name & "!" & objRange.Address(False, False)
-
strsheet = ""
-
strsheet = strworksheetname
-
aryList = Split(strsheet, "!", , vbTextCompare)
-
intupper = UBound(aryList)
-
strJunk = ""
-
strTable = ""
-
For a = 0 To UBound(aryList)
-
If a = UBound(aryList) Then
-
strJunk = aryList(a)
-
Else
-
strTable = aryList(a)
-
End If
-
Next a
-
-
If strTable = "All_GL_Accts" Then
-
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
-
"tbl_Chart_of_Accounts", strFileName, True, strworksheetname
-
ElseIf strTable = "BU_Legal_Consol" Then
-
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
-
"tbl_Business_Units", strFileName, True, strworksheetname
-
ElseIf strTable = "ELIM Sets" Then
-
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
-
"tbl_Elimination_Sets", strFileName, True, strworksheetname
-
-
Else
-
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
-
"tbl_Ledger_Balances_Combined", strFileName, True, strworksheetname
-
End If
-
-
Next
-
-
-
End Sub
-
-
-
Also each tab has around 18K rows to import....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |