Access VBA: Create 2D Array, and Then Update Table With Array Data

Peter_W

New Member
Joined
Apr 2, 2018
Messages
31
Hello, and as a non-programmer I struggle much with 2D arrays. I waive the proverbial white flag of surrender, and any help would be most merciful.

I have a text download, which I have converted to a one dimensional array. But I would like to convert it to a two-dimensional array, which would have 3 columns. Finally, if possible, I would like to use the contents of the 2D array to populate an Access table.

So my initial dataset that Access imports from the clipboard looks like this, with pipes "|" separating values:

12345678 | 3346555 | 345.24 |
33448999 | 5566899 | 683.56 |
66655442 | 7774485 | 557.85 |
etc.

I have been able to create a 1D array, and then update a table with array contents with this code:
(only showing bottom half of the sub for simplicity sake, with variables properly dimensioned)

Code so far, and I suspect I would need to ReDim due to a varying number of rows:

Set rsINPUT1 = CurrentDb.OpenRecordset("INPUT1")


' Retrieve clipboard contents into data object, which has the separating pipes:
dataobj.GetFromClipboard


' Clipboard to string variable
strString = dataobj.GetText



' Convert string variable to array
arrayString = Split(strString, "|")



' Post Array to Table using DAO recordset
For i = 0 To UBound(arrayString) - 1


'Update Table with data from clipboard (realizing I am only showing 1 of 3 fields in the table)
rsINPUT1.AddNew
rsINPUT1!Field1 = arrayString(i)
rsINPUT1.Update

Next I


So my problems are 2-fold:

1) How do I first convert the imported text into a 2D array, and then
2) How do I populate the resulting 3 columns of data into records in the Access table, INPUT1?


Any help would be most appreciated, thanks in advance, PW.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is there a carriage return at the end of the rows you could use to split on before splitting on the pipe? Alternatively if it is always 3 fields then you could just fill the table one row at a time.


Something like:
Code:
For i = 0 To UBound(arrayString) - 1    
    rsINPUT1.AddNew
    rsINPUT1!Field1 = arrayString(i)
    i = i + 1
    rsINPUT1!Field2 = arrayString(i)
    i = i + 1
    rsINPUT1!Field3 = arrayString(i)
    rsINPUT1.Update
Next i
 
Last edited:
Upvote 0
Is there a carriage return at the end of the rows you could use to split on before splitting on the pipe? Alternatively if it is always 3 fields then you could just fill the table one row at a time.


Something like:
Code:
For i = 0 To UBound(arrayString) - 1    
    rsINPUT1.AddNew
    rsINPUT1!Field1 = arrayString(i)
    i = i + 1
    rsINPUT1!Field2 = arrayString(i)
    i = i + 1
    rsINPUT1!Field3 = arrayString(i)
    rsINPUT1.Update
Next i

Thank you Stumac. I will try your code suggestion. Its looks very interesting.

I am personally stumped by the carriage return aspect.
The original downloaded file is from SAP, and all I can visually
see is a pipe "|" at the end of each row.
But somehow, the original data comes into Access, in rows, without any additional coding. The problem is, there are 3 columns of data all concatenated together.

Again, I am stumped as to whether the original file has carriage returns, and worse yet, if I need to add carriage returns programatically inside my code?

Again, just learning 2D arrays, in my usual painful way. Thanks again for your response. Any clarification of carriage returns would be great.
 
Upvote 0
Open the file in Word, and select the Hidden Formatting icon. That will show you.
Or use Notepad++
 
Upvote 0
Thank you Stumac. I will try your code suggestion. Its looks very interesting.

I am personally stumped by the carriage return aspect.
The original downloaded file is from SAP, and all I can visually
see is a pipe "|" at the end of each row.
But somehow, the original data comes into Access, in rows, without any additional coding. The problem is, there are 3 columns of data all concatenated together.

Again, I am stumped as to whether the original file has carriage returns, and worse yet, if I need to add carriage returns programatically inside my code?

Again, just learning 2D arrays, in my usual painful way. Thanks again for your response. Any clarification of carriage returns would be great.


Stumac, your code works great! Thank you!!!

I did need to tweak your code for Field3 as follows:

For i = 0 To UBound(arrayString) - 1
rsINPUT1.AddNew
rsINPUT1!Field1 = arrayString(i)
i = i + 1
rsINPUT1!Field2 = arrayString(i)
i = i + 1
rsINPUT1!Field3 = arrayString(i)
i = i+1
rsINPUT1.Update
Next I

So it appears that the code is taking the new array created from the clipboard data, taking the 3 different values between the pipes, and then placing each of the 3 values in Fields 1 to Field 3, for each record. Probably need to study this more, but for now it works great. Just what I need, thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,560
Messages
6,173,039
Members
452,500
Latest member
jesuisazep

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top