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

Peter_W

New Member
Joined
Apr 2, 2018
Messages
29
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.
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

stumac

Active Member
Joined
Jul 16, 2010
Messages
471
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:

Peter_W

New Member
Joined
Apr 2, 2018
Messages
29
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.
 

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
820
Open the file in Word, and select the Hidden Formatting icon. That will show you.
Or use Notepad++
 

Peter_W

New Member
Joined
Apr 2, 2018
Messages
29
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,102,101
Messages
5,484,672
Members
407,460
Latest member
Fakxi

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top