Print multidimeisonal array to .txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizo98
    New Member
    • Jun 2013
    • 18

    #1

    Print multidimeisonal array to .txt file

    Hi Guys,

    what is the correct syntax for printing a multidimensioan l array to a .txt file?
    I know a simgle dimension array can be printed like this.
    Print #1, arrCount(1);

    Currently I am using the following code below to do it but this way I am not able set the delimiters and so unable to open in in excel with all data in different cell. I'm using comma as seen in the code but I want a better solution.

    Before getting sidetracked, here is the question. How do you print a milti dimensioanl array to a file?
    thanks guys

    Code:
    If Not blmFileOpen Then
        
                Open "C:\Temp\MarketData\ArrayTest.txt" For Output As #1
                'Print #1, arrCount(n, j);
                Print #1, n + 1 & ",";
                Print #1, arrCount(n, 0) & ",";
                Print #1, arrCount(n, 1) & ",";
                Print #1, arrCount(n, 2) & ",";
                Print #1, arrCount(n, 3) & ",";
                Print #1, arrCount(n, 4) & ",";
                Print #1, arrCount(n, 5) & ",";
                Print #1, arrCount(n, 6) & ",";
                Print #1, arrCount(n, 7) & ",";
                Print #1, arrCount(n, 8) & ",";
                Print #1, arrCount(n, 9) & ",";
                Print #1, arrCount(n, 10) & ",";
                Print #1, arrCount(n, 11) & ",";
                Print #1, arrCount(n, 12) & ",";
                Print #1, arrCount(n, 13) & ",";
                Print #1, arrCount(n, 14) & vbCrLf;
    
              '  n = n + 1
               ' j = j + 1
                blmFileOpen = True
                Close #1
            Else
            
                Open "C:\Temp\MarketData\ArrayTest.txt" For Append As #1
               ' Print #1, arrCount(n, j);
               Print #1, n + 1 & ",";
                Print #1, arrCount(n, 0) & ",";
                Print #1, arrCount(n, 1) & ",";
                Print #1, arrCount(n, 2) & ",";
                Print #1, arrCount(n, 3) & ",";
                Print #1, arrCount(n, 4) & ",";
                Print #1, arrCount(n, 5) & ",";
                Print #1, arrCount(n, 6) & ",";
                Print #1, arrCount(n, 7) & ",";
                Print #1, arrCount(n, 8) & ",";
                Print #1, arrCount(n, 9) & ",";
                Print #1, arrCount(n, 10) & ",";
                Print #1, arrCount(n, 11) & ",";
                Print #1, arrCount(n, 12) & ",";
                Print #1, arrCount(n, 13) & ",";
                Print #1, arrCount(n, 14) & vbCrLf;
                'n = n + 1
               ' j = j + 1
                Close #1
        End If
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I have written some simple Code that should clearly illustrate how to write a Multi-Dimensional Array to a Text File. The following Code will:
    1. Populate a Multi-Dimensional Array with some Dummy Values.
    2. Read back those Values and write then to a Text File in the Database's Directory.
    3. Sample Code:
      Code:
      'Declare a Multi-Dimensional Array of Integers (12 Elements)
      Dim aintTest(1 To 3, 1 To 4) As Integer
      Dim bytDim1 As Byte         'First Dimension
      Dim bytDim2 As Byte         'Second Dimension
      Dim bytCtr1 As Byte
      Dim bytCtr2 As Byte
      
      Open CurrentProject.Path & "\Test.txt" For Output As #1
      
      'Populate Array with some meaningless Values
      For bytCtr1 = LBound(aintTest(), 1) To UBound(aintTest(), 1)    '1st Dimension
        For bytCtr2 = LBound(aintTest(), 2) To UBound(aintTest(), 2)  '2nd Dimension
          aintTest(bytCtr1, bytCtr2) = (bytCtr1 * bytCtr2) ^ 4        'Assign Values
        Next bytCtr2
      Next bytCtr1
      
      'Read back the Elements in the Array and write to a Text File
      'in the same Directory as the Database named Array.txt
      For bytCtr1 = LBound(aintTest(), 1) To UBound(aintTest(), 1)    '1st Dimension
        For bytCtr2 = LBound(aintTest(), 2) To UBound(aintTest(), 2)  '2nd Dimension
          Print #1, FormatNumber(aintTest(bytCtr1, bytCtr2), 0)         'Write to Text File
        Next bytCtr2
      Next bytCtr1
      
      Close #1
    4. Contents of Test.txt:
      Code:
      1
      16
      81
      256
      16
      256
      1,296
      4,096
      81
      1,296
      6,561
      20,736

    Comment

    • rizo98
      New Member
      • Jun 2013
      • 18

      #3
      I've done it. I'm posting how I fixed it below in the hope that it will help someone; just like you guys.
      thanks

      Changing e.g.
      Code:
      Print #1, arrCount(n, 0) & ",";
      to the below does the trick
      Code:
      Print #1, arrCount(n, 0) & vbTab
      full code is below


      Code:
          'write the array to txt file
          If Not blmFileOpen Then
          
                  Open "C:\Temp\MarketData\ArrayTest.txt" For Output As #1
                  'Print #1, arrCount(n, j);
                  Print #1, n + 1 & vbTab;
                  Print #1, arrCount(n, 0) & vbTab;
                  Print #1, arrCount(n, 1) & vbTab;
                  Print #1, arrCount(n, 2) & vbTab;
                  Print #1, arrCount(n, 3) & vbTab;
                  Print #1, arrCount(n, 4) & vbTab;
                  Print #1, arrCount(n, 5) & vbTab;
                  Print #1, arrCount(n, 6) & vbTab;
                  Print #1, arrCount(n, 7) & vbTab;
                  Print #1, arrCount(n, 8) & vbTab;
                  Print #1, arrCount(n, 9) & vbTab;
                  Print #1, arrCount(n, 10) & vbTab;
                  Print #1, arrCount(n, 11) & vbTab;
                  Print #1, arrCount(n, 12) & vbTab;
                  Print #1, arrCount(n, 13) & vbTab;
                  Print #1, arrCount(n, 14) & vbCrLf;
      
                '  n = n + 1
                 ' j = j + 1
                  blmFileOpen = True
                  Close #1
              Else
              
                  Open "C:\Temp\MarketData\ArrayTest.txt" For Append As #1
                 ' Print #1, arrCount(n, j);
                 Print #1, n + 1 & vbTab;
                  Print #1, arrCount(n, 0) & vbTab;
                  Print #1, arrCount(n, 1) & vbTab;
                  Print #1, arrCount(n, 2) & vbTab;
                  Print #1, arrCount(n, 3) & vbTab;
                  Print #1, arrCount(n, 4) & vbTab;
                  Print #1, arrCount(n, 5) & vbTab;
                  Print #1, arrCount(n, 6) & vbTab;
                  Print #1, arrCount(n, 7) & vbTab;
                  Print #1, arrCount(n, 8) & vbTab;
                  Print #1, arrCount(n, 9) & vbTab;
                  Print #1, arrCount(n, 10) & vbTab;
                  Print #1, arrCount(n, 11) & vbTab;
                  Print #1, arrCount(n, 12) & vbTab;
                  Print #1, arrCount(n, 13) & vbTab;
                  Print #1, arrCount(n, 14) & vbCrLf;
                  'n = n + 1
                 ' j = j + 1
                  Close #1
          End If
          
          Next n

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Couldn't you condense the Code, something similar to:
        Code:
        Dim bytRowCtr As Byte
        
        Print #1, n + 1 & vbTab;
        
        For bytRowCtr = 0 To 14
          If bytRowCtr = 14 Then
            Print #1, arrCount(n, bytRowCtr) & vbCrLf;
          Else
            Print #1, arrCount(n, bytRowCtr) & vbTab;
          End If
        Next

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          sigh,
          Call your array upper scipt via ubound function
          Call your array elements within nested loops against the dimensions of the array and print the result.
          Depending on the desired format depends on where within the loops you place the tab or commas and the carrage/linefeed to seperate elements and groups.

          Before getting sidetracked, here is the question. How do you print a milti dimensioanl array to a file?
          Normally, we don't answer homework questions; however, it appears that you made a good faith effort - thus the help.
          Keep up the good work!

          Comment

          • rizo98
            New Member
            • Jun 2013
            • 18

            #6
            I did this but now I'm getting an overflow message.
            Run-time error (6)
            Overflow


            Code:
             For bytCtr1 = LBound(arrCount(), 1) To UBound(arrCount(), 1)    '1st Dimension
              For bytCtr2 = LBound(arrCount(), 2) To UBound(arrCount(), 2)  '2nd Dimension
                Print #1, arrCount(bytCtr1, bytCtr2)         'Write to Text File
              Next bytCtr2
            Next bytCtr1

            Comment

            • rizo98
              New Member
              • Jun 2013
              • 18

              #7
              I've chaned "bytCtr1 As Byte" to a long so that now fixes the problem since byte holds values only upto 255.

              Now the problem I'm facing is that each array is being written to a new line where I want it to write to the file in table form.

              Current form
              Code:
              23
              Name
              14.90%
              33,558,819,050,000.00
              225286973.5
              23
              Name
              14.90%
              23
              Name
              14.90%

              How I want to write to the file.
              Code:
              23	Name	14.90%	33,558,819,050,000.00	225286973.5	23	Name	14.90%	23	Name	14.90%

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Typically this occurs with the wrong data-type for a variable.

                Please post the entire code from the "Sub()" to the "End Sub."

                Also please follow the instructions for debugging: > Before Posting (VBA or SQL) Code repeat the compile step until no errors are returned.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  rizo98

                  Looks like I cross posted with you and as I suspected there was a wrongly defined variable data-type.

                  As for formatting your text output: I remind you,
                  Bytes is neither a code writing nor homework service. Please read the FAQ and posting guidelines. Further the formatting is offtopic to the thread.

                  Your original question has been answered by both ADezii and myself.

                  Comment

                  • rizo98
                    New Member
                    • Jun 2013
                    • 18

                    #10
                    Im not asking you to do my work for me.
                    It was very kindly suggested an alternative/suggestion by ADezii and yourself so I took on the challenge to learn more about acces vba. That is all.

                    Comment

                    • zmbd
                      Recognized Expert Moderator Expert
                      • Mar 2012
                      • 5501

                      #11
                      Be that as it may... the formatting is still a new question; thus a new thread. You can and should back reference to this thread when you start the new question.

                      Comment

                      • rizo98
                        New Member
                        • Jun 2013
                        • 18

                        #12
                        Cool. I will do. Thanks @zmbd

                        Comment

                        Working...