Access VBA: Create 2D Array, and Then Update Table With Array Data
Results 1 to 5 of 5

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

  1. #1
    New Member
    Join Date
    Apr 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

  2. #2
    Board Regular
    Join Date
    Jul 2010
    Posts
    425
    Post Thanks / Like
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

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

    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 by stumac; Jun 21st, 2019 at 03:52 AM.

  3. #3
    New Member
    Join Date
    Apr 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by stumac View Post
    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.

  4. #4
    Board Regular
    Join Date
    May 2013
    Posts
    619
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

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

    Open the file in Word, and select the Hidden Formatting icon. That will show you.
    Or use Notepad++
    Office 2007
    Access novice. Sometimes trying to give something back

  5. #5
    New Member
    Join Date
    Apr 2018
    Posts
    28
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Quote Originally Posted by Peter_W View Post
    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!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •