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

Print multidimeisonal array to .txt file

18
Hi Guys,

what is the correct syntax for printing a multidimensioanl 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

Expand|Select|Wrap|Line Numbers
  1. If Not blmFileOpen Then
  2.  
  3.             Open "C:\Temp\MarketData\ArrayTest.txt" For Output As #1
  4.             'Print #1, arrCount(n, j);
  5.             Print #1, n + 1 & ",";
  6.             Print #1, arrCount(n, 0) & ",";
  7.             Print #1, arrCount(n, 1) & ",";
  8.             Print #1, arrCount(n, 2) & ",";
  9.             Print #1, arrCount(n, 3) & ",";
  10.             Print #1, arrCount(n, 4) & ",";
  11.             Print #1, arrCount(n, 5) & ",";
  12.             Print #1, arrCount(n, 6) & ",";
  13.             Print #1, arrCount(n, 7) & ",";
  14.             Print #1, arrCount(n, 8) & ",";
  15.             Print #1, arrCount(n, 9) & ",";
  16.             Print #1, arrCount(n, 10) & ",";
  17.             Print #1, arrCount(n, 11) & ",";
  18.             Print #1, arrCount(n, 12) & ",";
  19.             Print #1, arrCount(n, 13) & ",";
  20.             Print #1, arrCount(n, 14) & vbCrLf;
  21.  
  22.           '  n = n + 1
  23.            ' j = j + 1
  24.             blmFileOpen = True
  25.             Close #1
  26.         Else
  27.  
  28.             Open "C:\Temp\MarketData\ArrayTest.txt" For Append As #1
  29.            ' Print #1, arrCount(n, j);
  30.            Print #1, n + 1 & ",";
  31.             Print #1, arrCount(n, 0) & ",";
  32.             Print #1, arrCount(n, 1) & ",";
  33.             Print #1, arrCount(n, 2) & ",";
  34.             Print #1, arrCount(n, 3) & ",";
  35.             Print #1, arrCount(n, 4) & ",";
  36.             Print #1, arrCount(n, 5) & ",";
  37.             Print #1, arrCount(n, 6) & ",";
  38.             Print #1, arrCount(n, 7) & ",";
  39.             Print #1, arrCount(n, 8) & ",";
  40.             Print #1, arrCount(n, 9) & ",";
  41.             Print #1, arrCount(n, 10) & ",";
  42.             Print #1, arrCount(n, 11) & ",";
  43.             Print #1, arrCount(n, 12) & ",";
  44.             Print #1, arrCount(n, 13) & ",";
  45.             Print #1, arrCount(n, 14) & vbCrLf;
  46.             'n = n + 1
  47.            ' j = j + 1
  48.             Close #1
  49.     End If
Jul 3 '13 #1
11 8840
ADezii
8,834 Expert 8TB
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:
    Expand|Select|Wrap|Line Numbers
    1. 'Declare a Multi-Dimensional Array of Integers (12 Elements)
    2. Dim aintTest(1 To 3, 1 To 4) As Integer
    3. Dim bytDim1 As Byte         'First Dimension
    4. Dim bytDim2 As Byte         'Second Dimension
    5. Dim bytCtr1 As Byte
    6. Dim bytCtr2 As Byte
    7.  
    8. Open CurrentProject.Path & "\Test.txt" For Output As #1
    9.  
    10. 'Populate Array with some meaningless Values
    11. For bytCtr1 = LBound(aintTest(), 1) To UBound(aintTest(), 1)    '1st Dimension
    12.   For bytCtr2 = LBound(aintTest(), 2) To UBound(aintTest(), 2)  '2nd Dimension
    13.     aintTest(bytCtr1, bytCtr2) = (bytCtr1 * bytCtr2) ^ 4        'Assign Values
    14.   Next bytCtr2
    15. Next bytCtr1
    16.  
    17. 'Read back the Elements in the Array and write to a Text File
    18. 'in the same Directory as the Database named Array.txt
    19. For bytCtr1 = LBound(aintTest(), 1) To UBound(aintTest(), 1)    '1st Dimension
    20.   For bytCtr2 = LBound(aintTest(), 2) To UBound(aintTest(), 2)  '2nd Dimension
    21.     Print #1, FormatNumber(aintTest(bytCtr1, bytCtr2), 0)         'Write to Text File
    22.   Next bytCtr2
    23. Next bytCtr1
    24.  
    25. Close #1
    26.  
  4. Contents of Test.txt:
    Expand|Select|Wrap|Line Numbers
    1. 1
    2. 16
    3. 81
    4. 256
    5. 16
    6. 256
    7. 1,296
    8. 4,096
    9. 81
    10. 1,296
    11. 6,561
    12. 20,736
Jul 3 '13 #2
rizo98
18
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.
Expand|Select|Wrap|Line Numbers
  1. Print #1, arrCount(n, 0) & ",";
to the below does the trick
Expand|Select|Wrap|Line Numbers
  1. Print #1, arrCount(n, 0) & vbTab
full code is below


Expand|Select|Wrap|Line Numbers
  1.     'write the array to txt file
  2.     If Not blmFileOpen Then
  3.  
  4.             Open "C:\Temp\MarketData\ArrayTest.txt" For Output As #1
  5.             'Print #1, arrCount(n, j);
  6.             Print #1, n + 1 & vbTab;
  7.             Print #1, arrCount(n, 0) & vbTab;
  8.             Print #1, arrCount(n, 1) & vbTab;
  9.             Print #1, arrCount(n, 2) & vbTab;
  10.             Print #1, arrCount(n, 3) & vbTab;
  11.             Print #1, arrCount(n, 4) & vbTab;
  12.             Print #1, arrCount(n, 5) & vbTab;
  13.             Print #1, arrCount(n, 6) & vbTab;
  14.             Print #1, arrCount(n, 7) & vbTab;
  15.             Print #1, arrCount(n, 8) & vbTab;
  16.             Print #1, arrCount(n, 9) & vbTab;
  17.             Print #1, arrCount(n, 10) & vbTab;
  18.             Print #1, arrCount(n, 11) & vbTab;
  19.             Print #1, arrCount(n, 12) & vbTab;
  20.             Print #1, arrCount(n, 13) & vbTab;
  21.             Print #1, arrCount(n, 14) & vbCrLf;
  22.  
  23.           '  n = n + 1
  24.            ' j = j + 1
  25.             blmFileOpen = True
  26.             Close #1
  27.         Else
  28.  
  29.             Open "C:\Temp\MarketData\ArrayTest.txt" For Append As #1
  30.            ' Print #1, arrCount(n, j);
  31.            Print #1, n + 1 & vbTab;
  32.             Print #1, arrCount(n, 0) & vbTab;
  33.             Print #1, arrCount(n, 1) & vbTab;
  34.             Print #1, arrCount(n, 2) & vbTab;
  35.             Print #1, arrCount(n, 3) & vbTab;
  36.             Print #1, arrCount(n, 4) & vbTab;
  37.             Print #1, arrCount(n, 5) & vbTab;
  38.             Print #1, arrCount(n, 6) & vbTab;
  39.             Print #1, arrCount(n, 7) & vbTab;
  40.             Print #1, arrCount(n, 8) & vbTab;
  41.             Print #1, arrCount(n, 9) & vbTab;
  42.             Print #1, arrCount(n, 10) & vbTab;
  43.             Print #1, arrCount(n, 11) & vbTab;
  44.             Print #1, arrCount(n, 12) & vbTab;
  45.             Print #1, arrCount(n, 13) & vbTab;
  46.             Print #1, arrCount(n, 14) & vbCrLf;
  47.             'n = n + 1
  48.            ' j = j + 1
  49.             Close #1
  50.     End If
  51.  
  52.     Next n
Jul 3 '13 #3
ADezii
8,834 Expert 8TB
Couldn't you condense the Code, something similar to:
Expand|Select|Wrap|Line Numbers
  1. Dim bytRowCtr As Byte
  2.  
  3. Print #1, n + 1 & vbTab;
  4.  
  5. For bytRowCtr = 0 To 14
  6.   If bytRowCtr = 14 Then
  7.     Print #1, arrCount(n, bytRowCtr) & vbCrLf;
  8.   Else
  9.     Print #1, arrCount(n, bytRowCtr) & vbTab;
  10.   End If
  11. Next
Jul 3 '13 #4
zmbd
5,501 Expert Mod 4TB
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!
Jul 3 '13 #5
rizo98
18
I did this but now I'm getting an overflow message.
Run-time error (6)
Overflow


Expand|Select|Wrap|Line Numbers
  1.  For bytCtr1 = LBound(arrCount(), 1) To UBound(arrCount(), 1)    '1st Dimension
  2.   For bytCtr2 = LBound(arrCount(), 2) To UBound(arrCount(), 2)  '2nd Dimension
  3.     Print #1, arrCount(bytCtr1, bytCtr2)         'Write to Text File
  4.   Next bytCtr2
  5. Next bytCtr1
Jul 4 '13 #6
rizo98
18
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
Expand|Select|Wrap|Line Numbers
  1. 23
  2. Name
  3. 14.90%
  4. 33,558,819,050,000.00
  5. 225286973.5
  6. 23
  7. Name
  8. 14.90%
  9. 23
  10. Name
  11. 14.90%

How I want to write to the file.
Expand|Select|Wrap|Line Numbers
  1. 23    Name    14.90%    33,558,819,050,000.00    225286973.5    23    Name    14.90%    23    Name    14.90%
  2.  
Jul 4 '13 #7
zmbd
5,501 Expert Mod 4TB
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.
Jul 4 '13 #8
zmbd
5,501 Expert Mod 4TB
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.
Jul 4 '13 #9
rizo98
18
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.
Jul 4 '13 #10
zmbd
5,501 Expert Mod 4TB
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.
Jul 4 '13 #11
rizo98
18
Cool. I will do. Thanks @zmbd
Jul 4 '13 #12

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

Similar topics

1
by: Stephen | last post by:
I have two arrays: int array1 = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2 = { { 1, 2 }, { 3 }, { 4, 5, 6 } }; I need to print both of them by column like this:
1
by: Brian Hanson | last post by:
Hi, I am trying to write a javascript function that gets called from a within an asp.net application to print a pdf file(s) programmatically. I am a pretty experienced developer in several...
4
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
3
by: C.K | last post by:
Hello all, I am still working on my calendar program, and I have decided that the best way is for my vb program to write html tags into a file and then to print the file from ie6. Everything is...
11
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
7
by: Ron | last post by:
Hi All, Is it possible to have Access print a report, identical to one that would print to a printer, only print to a "standard" text file? I can't find it in help and when I try to just print...
1
by: Ninja12 | last post by:
I created a program, and the input data gathered from the program is stored in a .dat file. There are three files. I want print the third file. My files are named (1), (2) (3). I use the Common...
2
by: Samdaye | last post by:
I am trying to print a PDF file directly from Access. There are hundreds of PDFs that are in a folder, where i know the path, and the file name is determined from the data that I pull back in...
5
rahulephp
by: rahulephp | last post by:
Hi I need to print below array in the following manner. Output would be: Property Name: 1 Property Address: 1 Price: 1 Property Size: 1 URL 1
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.