473,382 Members | 1,480 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,382 software developers and data experts.

How to transfer a 2d array into a table

TheSmileyCoder
2,322 Expert Mod 2GB
I have a 2d array, with 10 columns, and somewhere between 200 and 10.000 rows. I was hoping there was a simple(and efficient) way to transfer the entire array into a table containing over a million rows, and growing.

I allready know how to transfer it by looping through the array, adding 1 row at a time. However, this is not that effective, and I believe that one of the problems stems from the fact that the main table has its indexes updated each time a row is added. I could probably work around this by first moving the array into a temporary table, and then use SQL to add all the rows at once.

Either way, Im still looking to see if there is a efficient way to transfer an array into a table. Work Smarter, Not Harder

TheSmileyCoder
Sep 1 '11 #1
6 11957
NeoPa
32,556 Expert Mod 16PB
The first idea would be to to delete the index(es) prior to adding the data, then add it (them) again afterwards. Fitting that into a single transaction might not be too good an idea though.
Sep 1 '11 #2
Stewart Ross
2,545 Expert Mod 2GB
Interestingly, although it will not help you in this application, Excel has this capability built in. The contents of an array can be directly assigned to a range in code - and it is much faster to do so than to transfer the array contents line by line. Here is a simple test example with two subs, one to build an array of numbers, the other to transfer it to Excel. The transfer is breathtakingly simple.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Const cCols = 10
  4. Const cRows = 10000
  5. Public arSomeArray(cRows - 1, cCols - 1) As Long
  6.  
  7. Public Sub SetArray()
  8.     Dim i As Integer
  9.     Dim j As Integer
  10.     For i = 0 To cRows - 1
  11.         For j = 0 To cCols - 1
  12.             arSomeArray(i, j) = i + j
  13.         Next
  14.     Next
  15. End Sub
  16.  
  17. Public Sub TransferArray()
  18.     Dim objWs As Worksheet
  19.     Set objWs = Application.ActiveSheet
  20.     With objWs
  21.         .Range(.Cells(1, 1), .Cells(cRows - 1, cCols - 1)) = arSomeArray
  22.     End With
  23. End Sub
Although I know this is not the kind of solution you are looking for, it seems appropriate to mention it here given that you have an array as a source object.

-Stewart
Sep 3 '11 #3
NeoPa
32,556 Expert Mod 16PB
I thought I knew most of what there is to know about manipulating data on a worksheet Stewart, but I didn't know that. Very interesting. What would make it even more interesting woul be the ability to assign data back the other way, but that seems not to be available. Seems a shame, as that could prove very powerful for data manipulation in Excel :-(
Sep 3 '11 #4
NeoPa
32,556 Expert Mod 16PB
I knew there was something else I was thinking about for this, but it turns out that it (GetRows()) is only applicable for dealing with reading the data into an array too. I could find nothing for transferring data from an array into your table (which seems a shame).

My best suggestion is still to remove the indices before adding the data then re-apply them afterwards. To minimise exposure to failure within the process I'd probably transfer the data first into a temporary table before removing the indices, bulk-inserting the data across to the newly, and temporarily, indexless table, then re-applying the indices. Running this within a transaction is still likely to create very heavy overheads on your database of course.
Sep 3 '11 #5
ADezii
8,834 Expert 8TB
After much thought, I created a System, using ADO that will enable you to transfer the contents of a 2-Dimensional Array, by only iterating thru the 1st Dimension (Rows) of the Array. The down side of this approach is that you must manually enter the Indices for the 2nd Dimension (Cols) of the Array. The gain should be substantial by eliminating an entire Looping Structure. I'll leave the testing to you.
  1. Table Name: tblInsertArray
    1. [First]{TEXT}
    2. [Last]{TEXT}
    3. [Address]{TEXT}
    4. [Zip]{LONG}
  2. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim astrData(2, 3)
    2. Dim bytRow As Byte
    3. Dim bytCol As Byte
    4. Dim rec As ADODB.Recordset
    5. Set rec = New ADODB.Recordset
    6.  
    7. rec.Open "tblInsertArray", CurrentProject.Connection, adOpenKeyset, adLockOptimistic
    8.  
    9. 'Populate the Test Array
    10. astrData(0, 0) = "Herman"
    11. astrData(0, 1) = "Munster"
    12. astrData(0, 2) = "1313 Mockingbird Lane"
    13. astrData(0, 3) = 99999
    14.  
    15. astrData(1, 0) = "Babe"
    16. astrData(1, 1) = "Ruth"
    17. astrData(1, 2) = "111 Hall of Fame Drive"
    18. astrData(1, 3) = 67541
    19.  
    20. astrData(2, 0) = "Barack"
    21. astrData(2, 1) = "O'Bama"
    22. astrData(2, 2) = "White House"
    23. astrData(2, 3) = 98766
    24.  
    25. For bytRow = LBound(astrData, 1) To UBound(astrData, 1)
    26.   rec.AddNew Array("First", "Last", "Address", "Zip"), Array(astrData(bytRow, 0), _
    27.              astrData(bytRow, 1), astrData(bytRow, 2), astrData(bytRow, 3))
    28.   rec.Update
    29. Next
    30.  
    31. rec.Close: Set rec = Nothing
  3. OUTPUT (tblInsertArray after Code Execution):
    Expand|Select|Wrap|Line Numbers
    1. First    Last      Address                    Zip
    2. Herman   Munster   1313 Mockingbird Lane      99999
    3. Babe     Ruth      111 Hall of Fame Drive     67541
    4. Barack   O'Bama    White House                98766
    5.  
Sep 3 '11 #6
TheSmileyCoder
2,322 Expert Mod 2GB
Thank you for your replies. In the mean time, I have been temporarily pulled from that project to put out a fire in another project. I should be back working on it next week, and will take a look at all your, as always, excellent replies.
Sep 7 '11 #7

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

Similar topics

1
by: kamalkumar | last post by:
Hi Please help for this simple problem DTS Transfer or any other method?
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
1
by: ttan | last post by:
I had 4 arrays string LastName; string FirstName; string BirthDay; string Phone; What is the good way to store these information in a table? and when user click on LastName/FirstName...
1
by: Henri Schomäcker | last post by:
Hi folks, what I need is a kind of 2D-Array which should, in the end, represent a typical html-table and which should be sortable by column. I thought of an implementation, where an vector...
1
by: Sandesh | last post by:
Hello All, I have a scenario in which my stored procedure has to return few variables with their value and also the collection. Now in SQL their is no such as array, so the best is to return the...
0
by: blainegray | last post by:
Greetings This is one of those Access is not closing Excel problems. The first time through the code works fine. The second time there is a problem. After lots of combinations, I finally...
1
by: umamaheshg | last post by:
Hi All, I tried to transfer a table in .mdb file to table in sybase database I used following query in VB. But i m getting many errors from this. Please correct the below query....
4
by: flavourofbru | last post by:
Hi all... I declared a matrix ofl arge size as follows: int (* abc) = new int; now I need to transfer all the contents of the above array into another array of same size. How can I do that? ...
4
by: mostafijur | last post by:
Hello I want to transfer database table from one pc to another pc. 1. How it possible? Pls can u tell me step by step? 2. Where the table and database store in my PC? OS:...
1
by: nuhbye | last post by:
I am trying to transfer output of a query to some file whose name will be available only at the run time. My CODE looks something like this, this code is a part of stored procedure. set...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.