Link to home
Start Free TrialLog in
Avatar of gonzal13
gonzal13Flag for United States of America

asked on

Save and read an array

I would like to save an array that has been generated and then recall it in anther click event.

I tried the following without sucess: The data are INTEGERS. All I need is to read into memory the array and also save the array

Dim Var1
       
         Var1 = ABC(K, 2)
       
      Open "c:\program files\Microsoft Visual Studio\database\????? _
        For Output As #1
       Write #1, ABC(K, 2)
       Close

Dim Var1
       
         Var1 = ABC(K, 2)
       
      Open "c:\program files\Microsoft Visual Studio\database\????? _
        For Output As #1
       GET#1, ABC(K, 2)
       Close


           
         
       
           
            Close #1
Avatar of gonzal13
gonzal13
Flag of United States of America image

ASKER

I did a little research in a book called 'The Complete Visual Basic 6 Language and found two sets of code. Each of one do not work.

I made a file through Dos called Binary.dat

Dim Scores(100) as Integer
Open "Binary.dat" For Binary As #fNum
Put #1,  ,Scores()
Close #1

Do I need to type in a For Loop after Put #1?

Can the data as mentioned in the text book be Binary.txt?

gonzal13(joe)
ASKER CERTIFIED SOLUTION
Avatar of bingie
bingie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi again:

I have been learning allot from Idle_Mind over the last month.

The arrays vary from two to three dimentional arrays. They are generated thrue code of course and then I need to save the arrays which can go up to 400 rows. I then need to read the arrays into memory. Now at times the arrays are generated in one click event and I need to access them in another click event. Do I generate the arrays in a module? If so, then how do I access them in the click events. I imagine that I set up a function in the module to generate the data and then call the function from a click event.

It may sound if I know what I am talking about{Idle_Mind and the book you suggested have helped me}, but this is all new to me including using a module, setting up a function and calling it.

Gonzal13(joe)
You have an array defined within a module, which is read from the click event of buttons, in a form?

If so, the important thing to remember is to make the array Global, so it can be referenced both by the module and the form.  A simple VBA example follows, with one module and one form (with a button and a listbox in it):

   [Module code]
   Global intArray()     'THIS ARRAY IS SHARED WITH ALL FORMS/MODULES

   Sub main()

       intArray() = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)        'POPULATE GLOBAL ARRAY
       UserForm1.Show                                               'SHOW USERFORM1

   End Sub

   [Form code]
   Private Sub CommandButton1_Click()

       Dim ArrayItem
   
       For Each ArrayItem In intArray()            'LOOP THROUGH THE GLOBAL ARRAY
   
           ListBox1.AddItem CStr(ArrayItem)      'ADD EACH ITEM IN THE GLOBAL ARRAY
       
       Next

   End Sub

If you did not specify your array as global, in the module, your form would look for the array within the form code -- it wouldn't find it, so it would break with an error (probably 'Sub or Function not defined')

Global statements have to be placed within a module, not a form.

HTH
Example of writing an array, then reading it back:

Dim aOut(99) As Long
Dim aIn(99) As Long
Dim i As Long

'Load up some data
For i = 0 To 99
    aOut(i) = i
Next

Open "c:\test.dat" For Binary As #1
'write the array
Put #1, , aOut
'point back to the head
Seek #1, 1
'read the array
Get #1, , aIn

Close #1

For i = 0 To 99
    Debug.Print aOut(i), aIn(i)
Next
hi:

I have read all your comments. Would you put them in a form using the sample name array ABC(q,2) or Abc(x,y,x)

You have an array defined within a module, which is read from the click event of buttons, in a form?

If so, the important thing to remember is to make the array Global, so it can be referenced both by the module and the form.  A simple VBA example follows, with one module and one form (with a button and a listbox in it):
   
   Interpretations:



   [Module code]
   Global intArray()     'THIS ARRAY IS SHARED WITH ALL FORMS/MODULES

   Sub main()

       intArray() = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)        'POPULATE GLOBAL ARRAY
       UserForm1.Show                                               'SHOW USERFORM1

   End Sub

   [Form code]
   Private Sub CommandButton1_Click()

       Dim ArrayItem
   
       For Each ArrayItem In intArray()            'LOOP THROUGH THE GLOBAL ARRAY
   
           ListBox1.AddItem CStr(ArrayItem)      'ADD EACH ITEM IN THE GLOBAL ARRAY
       
       Next

   End Sub

If you did not specify your array as global, in the module, your form would look for the array within the form code -- it wouldn't find it, so it would break with an error (probably 'Sub or Function not defined')

Global statements have to be placed within a module, not a form.

HTH
>> Would you put them in a form using the sample name array ABC(q,2) or Abc(x,y,x)

No, you have to put Global statements in a module.  You can use Global ABC(5, 5) to create a global fixed-size array, but your use of ABC(q, 2) suggests that you do not know how big the array is going to be yet, so you may wish to declare ABC() as a dynamic array -- with Global ABC().  If you use this:

[Module code]
Global ABC()


[Form code]
'...After you have defined q

Dim TempArray(q, 2)

'...Put the values in TempArray()

ABC() = TempArray()


For a (very simple) example:

   Global ABC()

   Sub Doit()
       
       Const x = 1
       Const y = 1

       Dim TempArray(x, y)
       Dim i
   
       TempArray(0, 0) = 1
       TempArray(1, 0) = 2
       TempArray(0, 1) = 3
       TempArray(1, 1) = 4
   
       ABC() = TempArray()
   
       For Each i In ABC
   
          MsgBox i
       
       Next

   End Sub

You'll have to adapt the concepts above to match your code...
Sorry I pressed a button and it saved the above by accident:
I raised the points to 500 to be able not only to use this question to obtain the code but to be able to understand the code.

Hey Joe! :) Long time,

I would love to use a function in a module to save the array.. Then I could call it from a click event after the the arrays are generated. Also to call the read back the data as needed in other click events.

Basically, you will need to step through the array in order to read and write it. If you are using the code as above, there are some main problems with it.

An array is a compound variable, that is variables which can hold multiple values. An array is designed to hold homogeneous values. That is, every value in an array is of the same type. We then access each type by it's index.

You are basically declaring a three dimensional array, of dimension K by 3. This is probably more than you need.

ABC(K,3) Can hold up to 400 rows. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I have an array ABC(x, y, z) that is used to ask a question. The answer to it is 1 or 0.

I then intend to use for example the following:

If x = 9 and y = 3 and z = 4 and ABC(x,y.z) = to 1 then do something. Complicated but it should work. I have already coded it.

The second is as mentioned a maximum of 400 rows. The first column contains the data. I would have loved to separate the array into the following ABC(K) but it does not affect anything, since all I am concerned with is the first row.




A one dimensional array will hold data as follows:

Dim arrNum(0 to 4) As Integer 'Will declare an integer array of 6 items
'To save data in the array
arrNum(0) = 5
arrNum(1) = 6
arrNum(2) = 55
arrNum(3) = 66
arrNum(4) = 636

Now you can access each element by arrNum(i) where i = the index of the element you need.

A two dimensional array, akin to a spread sheet, will hold data like:

Dim arrNum(0 to 4, 0 to 1) as Interger 'Declares a 5 X 2 integer array
'To save data in the array
arrNum(0,0) = 45
arrNum(0,1) = 350
arrNum(1,0) = 43
arrNum(1,1) = 454

'Can be thought to appear like this:

45   350
43   454

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

So, to answer your question, you are opening the text file and reading the first line into an array at the
kth X 3 element. You did not specify your array dimensions or what K represents. I would need to know how
the data is stored in the file.

For example, if the data is stored in a file "C:test.txt" like this:
45
353
45654
3535
4455

Add two command buttons and paste the following. You can open the file and read the data into the array with it. Then use the second to write the array to a new file.

Dim var1(0 To 20) As String

Private Sub Command1_Click()
i = 0
Open "c:\test.txt" For Input As #1
Do Until EOF(1)    'Do the following until we are at the End Of File
    Line Input #1, var1(i)  'Save the current line at the current index i
    i = i + 1   'increment i
Loop   'Go to the next line in the file
Close
End Sub

Private Sub Command2_Click()
Open "c:\test2.txt" For Output As #1
    For i = 0 To UBound(var1) - 1
    Print #1, var1(i)
Next
Close
End Sub

This will only work if the are less than 20 items in the file, since the array will only hold that many items.

Hope I helped :)

'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

jimbobmcgee  feedback
Date: 09/29/2004 04:48AM PDT
 Comment  

Comments and interpretations:

You have an array defined within a module, which is read from the click event of buttons, in a form?

 I the click event, I assume I would do the data input and then call the save function from the module. Also, I assume that I can call the function to read back the data to the click event.


If so, the format would be for a function and then called by the click event to dave the data" Also need the code to read back the data into memory from any click event.

I am using several click events to insure the code is correct. It is my intention to combine the code into one click event in the future. I am estimating at least 10000 lines of code. I love to attack large projects. I was taught as an engineer to attack projects for which I had only the basic knowledge and then do it with confidence. Hopefully by the time I finish, I will have a fair handle on VB 6.
 
For example Module:

In the Module I would state a Function (Private or Public)?
Private Function generateSet(ByVal rangeMin As Integer, ByVal rangeMax As Integer, ByVal setSize As Integer) As Variant
        'GENERATESET(RANGEMIN, RANGEMAX, SETSIZE)

End Function

I could then 'Call' it from any click event



Thus using th basic code I gave you, and a two or three dimentional array, what would the code be for the Function to save the array? (Now my knowledge is increasing as i read books on this wonderful subject, but I still am dangerious with it) I have realized that I must go very slowly and check immediately each set of codes before amplifying them.

Also I would need the code in the click event to call the function. This of course means reading back the data.
 



If so, the important thing to remember is to make the array Global, so it can be referenced both by the module and the form.  A simple VBA example follows, with one module and one form (with a button and a listbox in it):

   [Module code]
   How do I Call' this code? I assume it should be a Function? XXXXXXXXXXXXXXXX
'   This should be a save function. I can generate the array in the click event XXXXXXXXXXXXXXXXXXXXXXXXX
   Global intArray()     'THIS ARRAY IS SHARED WITH ALL FORMS/MODULES
   DIM STATEMENTS NEEDED ???   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
   Sub main()

       intArray() = ABC()        'POPULATE GLOBAL ARRAY XXXXXXXXXXXXXXXX
       UserForm1.Show                                               'SHOW USERFORM1

   End Sub

   [Form code]
   Private Sub CommandButton1_Click()

       Dim ArrayItem
   
       For Each ArrayItem In intABC()        'XXXXXXXXXXXXXXX   'LOOP THROUGH THE GLOBAL ARRAY
   
           ListBox1.AddItem CStr(ArrayItem)      'ADD EACH ITEM IN THE GLOBAL ARRAY
       
       Next

   End Sub

If you did not specify your array as global, in the module, your form would look for the array within the form code -- it wouldn't find it, so it would break with an error (probably 'Sub or Function not defined')

Global statements have to be placed within a module, not a form.

HTH
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
Example of writing an array, then reading it back:

'My arrays are two and three dimentional. How would you do it with the array ABC(K, Q)?
Dim aOut(99) As Long  'What does 'Long' mean?  Mine are integers.  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Dim aIn(99) As Long  'What does ain(99) do?  XXXXXXXXXXXXXXXXXXXXXXXXXXX
Dim i As Long

'Load up some data
For i = 0 To 99
    aOut(i) = i
Next

Open "c:\test.dat" For Binary As #1
'write the array
Put #1, , aOut  'Put means Write?  'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'point back to the head
Seek #1, 1  'What does Seek #1, 1 do? XXXXXXXXXXXXXXXXXXXXXXXX
'read the array
Get #1, , aIn  'I assume to read the array you use Get #1, aIn instead of Put #1, , aOut? XXXXXXXXXXXXXXXXX

I would appreciate that you split the save code and read back the date into two distinct sets.

Also, would it not as mentioned above to use a function and then call the functions as needed? For example, I would create the data in one click event and then call the save function. then in another click event I would call the read function.

Close #1
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx
For i = 0 To 99
    Debug.Print aOut(i), aIn(i)
Next
 
Ok, after dinner I'll explain with a 2 dimensional array.
In the mean time, open Help from VB and look up Put, Seek, and Get.
Erick:

Take your time. I realize that I presented many questions. I have 4 books on the subject along with the msdm data base.

Thanks

Gonzal13(Joe)
Hi Joe,

There seems to be a mix of Visual Basic code and VBA code going on here.  Which are you using?  Are you using Forms in a stand alone application, or UserForms through MS Office?
Hi:

I am using VB code. I am using stand alone forms and in some cases using a mnu... File format to access the various complete section of code. I am not using UserForms through MS Office nor am I using MS Office.

Being new at this I do not know what VBA code is. The samples I gave you were from a visual basic book and one that I had used previously for saving data in text boxes.

gonzal13(joe)
Good.

Here is some code which writes a 2 dimensional array in the Command1_Click event and reads it back in the Command2_Click event.  Feel free to ask any questions that you may have.  I tried to document it as best I could with the comments.

'~~~~the code~~~~
'Always use Option Explicit - it forces you to declare all your variables
'Go to Tools->Options->[Editor tab] and check "Require Variable Declaration"

Option Explicit

'Declare a form level array which can be used by any function within this form
Dim ABC() As Integer

'Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767
'Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647


Private Sub Command1_Click()
'
'Save a 2 dimensional array
'
'
    'We can redimension the array during runtime if needed:
    ReDim ABC(9, 9) 'Make a 10x10 array
   
    Dim i As Long, j As Long 'Just some variable we use for indexing
    Dim lCount As Long 'just a counter
    Dim ff As Long 'Used in the file write routine
   
    'The following code only fills the array with sequential numbers
    For i = 0 To UBound(ABC, 1) 'Upper bound of 1st Dimension
        For j = 0 To UBound(ABC, 2) 'Upper bound of 2nd dimension
            ABC(i, j) = lCount 'save the number in the current location
            lCount = lCount + 1 'increment the count
        Next
    Next
   
    'The next part writes the array to a binary file
    ff = FreeFile 'Get the first available file number (probably #1)
    Open "c:\test.dat" For Binary As #ff
    'Data written with Put is usually read from a file with Get
    Put #ff, , ABC
    'Close the file
    Close #ff

End Sub

Private Sub Command2_Click()
'
'Read a 2 dimensional array
'
    Dim i As Long, j As Long 'Just some variable we use for indexing
    Dim sText As String
    Dim ff As Long 'Used in the file write routine
   
    'The next part reads the array from a binary file
    ff = FreeFile 'Get the first available file number (probably #1)
    Open "c:\test.dat" For Binary As #ff
    'Data read with Get is usually written to a file with Put
    Get #ff, , ABC
    'Close the file
    Close #ff
   
    'The following code only displays the contents of the array
    For i = 0 To UBound(ABC, 1) 'Upper bound of 1st Dimension
        For j = 0 To UBound(ABC, 2) 'Upper bound of 2nd dimension
            sText = sText & CStr(ABC(i, j)) & " " 'convert to string and concatenate to sText
        Next
        sText = sText & vbCrLf 'Next line
    Next
   
    MsgBox sText, vbExclamation, "The Array Contents"
   
End Sub

Private Sub Form_Load()

End Sub
Erick37:

Looks simple enough. I shall try it out.

Still need to know how to make it a function in a module and then call it from a click event. The reason is that the data will be used in a couple of click events. Also, it would be great to know how to write and call the function.

gonzal13
Erick:

I think the way you presented your answer is wonderful.

I have been since 1999 and recently being very active in Windows 98. I take the approach of assuming that the questioner may not have the knowledge to accomplish some tasks suggsted nor he may not know why they are suggested. Thus I make sure that I present a comment in detail, step by step and also provide why a comment was suggested.

It gives the client a certain amount of security and confidence in the comments presented. I also present, when needed, several possible solutions in order of importance along why they are in a certain order.

I get great satisfaction in helping others even if sometimes I do spend time on research before commenting.

I am also glad to see that the 'experts' do not critizize other comments. The site is very professional.

I guess my next step is to be on the XP site. I plan to install on one of the three hard drives that I have as slaves the new simplified XP. I really at this point do not have a use for it but I need to keep up with the technology.

Oh, I have a compulsion to buy professional grade software. I have quite a collection, but not allot of time to learn them. I guess I shall begin collecting software for XP. I retired at 50 because my wife was taking out million dollar accident policies because I was flying so much. She convinced me ten years ago to quit. I know now if it was not for the computer and now VB I would be bored. My associates would not be on a plane with me. My son saved my life one time by demanding I come home one day earlier. Well the commuter plane that I used from LAX that I had reservations, the next day when he was about to take off and on visual, was landed upon by a 737,

While I am in the mood, one other comment. I wanted to go to Belarus to do some consulting work. Well I recently found out due to th Chernoble accident the country is 99 percent contaminated.

Anyway I shall be waiting for your response. I really would love to use the module approach, place a function in it and then be able to all it from any click event. The code will give me a guide in writing other function and calls.

I do not know why the second sample I gave at the beginning would not work.

Joe
>> I the click event, I assume I would do the data input and then call the save function from the module. Also, I assume that I can call the function to read back the data to the click event.

The array must be Global: it's the only way to make sure that the array can be read or written in both the form and module.  To do this, you _must_ put a Global YourArrayName() statement in a module.


>> If so, the format would be for a function and then called by the click event to dave the data" Also need the code to read back the data into memory from any click event.

I would create a Public function (or sub) in your module, that reads from the file and stores it in the array, a Public function (or sub) in your module, that writes to the array, and a Public function (or sub) that displays the contents of the array.  Once these functions (or subs) are written, I would call them in the individual Click event subs for each control that accesses the array.

The difference between a sub and a function is that a function returns a value.  This is important if you need a piece of code to calculate a result.  A sub is a piece of code that returns no value.  If you need to check the result of your array access, you should use a function, otherwise use a sub.  A sub is called by typing:

                         Call MySubName(property1, property2, ...)    'DOES NOT STORE A RESULT

whereas a function is called by typing:
   
                         MyVariable = MyFunctionName(property1, property2, ...)  'MYVARIABLE STORES THE RESULT

Public subs/functions are useable from any module/form in your project.  Private subs/functions can only be called from the module/form it was written in.  I hope this helps with your functions.


>> I am using several click events to insure the code is correct. It is my intention to combine the code into one click event in the future. I am estimating at least 10000 lines of code. I love to attack large projects.

Handling 10000 lines in a single Click event sub is a difficult task and I don't recommend it.  You should, where possible, try to modularise your code, into smaller chunks that can be used again and again.  They will be clearer to read and debug (especially if commented).  Smaller, reuseable modules save time, in the long run.


>> Thus using th basic code I gave you, and a two or three dimentional array, what would the code be for the Function to save the array? (Now my knowledge is increasing as i read books on this wonderful subject, but I still am dangerious with it) I have realized that I must go very slowly and check immediately each set of codes before amplifying them.


>>   [Module code]
   How do I Call' this code? I assume it should be a Function? XXXXXXXXXXXXXXXX
'   This should be a save function. I can generate the array in the click event XXXXXXXXXXXXXXXXXXXXXXXXX
   Global intArray()     'THIS ARRAY IS SHARED WITH ALL FORMS/MODULES
   DIM STATEMENTS NEEDED ???   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If you enter code in module, that is not inside a sub or function (between the Sub.../End Sub lines), it is called declaration code.  You will see, in the drop-downs at the top of the code entry window, "(general)" and "(declarations)".  This code is called immediately, when the program is run, and is used to declare Global variables and external functions.  In the above example, a DIM statement is not required;  I am making a global array of unknown size.

The problem, I can see, is you are dimensioning an array based on the value of another variable (in this case, k).  Therefore, until you know the value of k, you cannot know the actual size of your array.  This makes creating an array, that can be used by all forms/modules/functions/subs very difficult because:  

  -  You cannot dimension an array (k, 2) until you know what 'k' is.  
  -  You cannot know what 'k' is, until you have calculated it within a function
  -  You cannot calculate 'k' from the (declarations) section of the module
  -  You must declare variables in the (declarations) section

To resolve this, I would declare the global array at the beginning of a module to be of unknown size, like so:

    Global arrMyArray()   'THE () SAYS "UNKNOWN SIZE". GLOBAL MEANS IT CAN BE USED ANYWHERE

Now, I would create a sub, in my module, to read data from a file:

    Public Sub ReadDataFromFile(strFileName as String)   'CREATE A SUB THAT CAN BE USED ANYWHERE

      Dim k as Long    
      Dim strBuffer as String

      'A SECTION OF CODE TO DETERMINE HOW BIG MY ARRAY SHOULD BE

      k = 0                  'RESET K (A COUNTER FOR MY ARRAY SIZE)

      Open strFileName for Input as #1       'OPEN THE FILE
         Do until EOF(1)   'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Line Input #1, strBuffer   'READ AN ENTIRE LINE FROM THE FILE
            k = k + 1                  'INCREMENT THE COUNTER

         Loop
      Close #1     'CLOSE THE FILE

      'NOW k CONTAINS THE NUMBER OF LINES IN MY FILE/HOW BIG MY ARRAY SHOULD BE I CAN MAKE
      'A TEMPORARY ARRAY TO STORE THE FILE IN.

      Dim arrTempArray(k, 2)     'MAKE TEMPORARY ARRAY BIG ENOUGH TO HOLD FILE

      k = 0    'RESET k (NOW USED AS AN ARRAY MARKER)

      Open strFileName for Input as #1      'OPEN THE FILE AGAIN, THIS TIME TO READ
         Do until EOF(1)        'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Input #1, arrTempArray(k, 0), arrTempArray(k, 1), arrTempArray(k, 2)
                                                                   'READ THE INDIVIDUAL VALUES ON THE LINE IN THE ARRAY
            k = k + 1   'INCREMENT THE MARKER

         Loop
      Close #1            'CLOSE THE FILE

      'NOW WE HAVE A COMPLETE ARRAY, WE CAN COPY IT TO THE GLOBAL ARRAY.  THE GLOBAL ARRAY
      'WILL THEN RESIZE ITSELF TO FIT THE TEMPORARY ARRAY

      arrMyArray() = arrTempArray()

    End Sub

If I have a form, with a button on it, that I click to load the data, my form's code will look like this:

    Private Sub CommandButton1_Click()          'THE CLICK EVENT FOR A TYPICAL BUTTON

       Call ReadDataFromArray("x:\mypath\myfile.dat")      'EXECUTE THE SUB I WROTE ABOVE TO READ ALL
                                                                                   'VALUES IN myfile.dat INTO THE GLOBAL ARRAY

    End Sub

Now, clicking CommandButton1 will enter all the values in myfile.dat into the global array, arrMyArray.

Now, I know that my global array contains all the data I need it to, I can create code to manipulate that array.  Because arrTempArray() was only referenced inside the ReadDataFromFile sub, it will be cleared when I finish using that sub.  But because I copied the contents of arrTempArray() into arrMyArray() (the global array), I can use the arrMyArray() when I want to access the data.

When it is time to save the data in the array, you would write a sub (in the module) to write the contents of the global array, into a file, like so:

   Public Sub WriteDataToFile(strFileName as String)
     
     Dim i as Long    'COUNTER FOR FOR/NEXT LOOP

     Open strFileName for Output as #1      'CREATE A NEW FILE

        For i = Lbound(arrMyArray(), 1) to Ubound(arrMyArray(), 1)    'LOOP THROUGH THE GLOBAL ARRAY

             Print #1, arrMyArray(i, 0) & "," & _
                          arrMyArray(i, 1) & "," & _
                          arrMyArray(i, 2)

        Next i

     Close #1        'CLOSE AND SAVE THE FILE

Now I would make another button on my form, for saving and use the following code in its Click event:

     Private Sub CommandButton2_Click

        Call WriteDataToFile("x:\mypath\myfile2.dat")
                                       'CALL THE SUB I JUST WROTE ABOVE, TO WRITE MY DATA TO myfile2.dat

     End Sub

I hope this helps you to understand the use of Global arrays, dynamic (unknown/changeable size) arrays and the relationships between public subs in a module...

Regards,
J
>> I do not know why the second sample I gave at the beginning would not work
>> Dim Var1
       
>>         Var1 = ABC(K, 2)
       
>>      Open "c:\program files\Microsoft Visual Studio\database\????? _
>>        For Output As #1
>>       GET#1, ABC(K, 2)
>>       Close

I think this does not work because you are opening a file for sequential text output (...for Output...).  Get # (I think) is only used in Random Access and Binary files (...for Random... or ...for Binary...).  Get # is designed to read data from a Random Access/Binary file, into a variable.  Sequential text streams use Input #, or Line Input # to read data into a variable.  Use the following table to determine what you should use for your file.

         Open "file.dat" for...  |   Output             |   Input     |  Random   |  Binary
        ================================================
         To read data in         |     (n/a)             |   Input #  |     Get #    |   Get #
        --------------------------------------------------------------------------------------
         To write data to        | Print #, Write #  |    (n/a)    |     Put #    |   Put #
       
HTH

J.
Thanks to all of you for the detailed explanations. It is very much appreciated. Now it is my turn to make the codes work.


gonzal13(joe)
Here is a (confusing) example of putting the code in a module:

'~~~~Form Code~~~~

Option Explicit

Private Sub Command1_Click()
'
'Save array via a call to a function in a module
'
    'We can redimension the array during runtime if needed:
    ReDim ABC(9, 9) 'Make a 10x10 array
   
    Dim i As Long, j As Long 'Just some variable we use for indexing
    Dim lCount As Long 'just a counter
   
    'The following code only fills the array with sequential numbers
    For i = 0 To UBound(ABC, 1) 'Upper bound of 1st Dimension
        For j = 0 To UBound(ABC, 2) 'Upper bound of 2nd dimension
            ABC(i, j) = lCount 'save the number in the current location
            lCount = lCount + 1 'increment the count
        Next
    Next
   
    'Call the function and check the return value
    If SaveArray(ABC) Then
        MsgBox "Array saved"
    Else
        MsgBox "Oops, array Not Saved"
    End If
   
End Sub

Private Sub Command2_Click()
'
'Read a 2 dimensional array
'
    Dim i As Long, j As Long 'Just some variable we use for indexing
    Dim sText As String
    Dim aNewArray() As Integer
   
    Call ReadArray(aNewArray)
    'The following code only displays the contents of the array
    For i = 0 To UBound(aNewArray, 1) 'Upper bound of 1st Dimension
        For j = 0 To UBound(aNewArray, 2) 'Upper bound of 2nd dimension
            sText = sText & CStr(aNewArray(i, j)) & " " 'convert to string and concatenate to sText
        Next
        sText = sText & vbCrLf 'Next line
    Next
   
    MsgBox sText, vbExclamation, "The Array Contents"
   
End Sub


'~~~~~Module Code~~!~~~~

Option Explicit

'Declare a module level array which can be used by any function in the project
Public ABC() As Integer

Public Function SaveArray(inABC() As Integer) As Boolean
'
'Save a 2 dimensional array
'
'
    Dim ff As Long 'Used in the file write routine
   
    'Go to this label if a runtime error occurs
    On Error GoTo SAVEERR
    'The next part writes the array to a binary file
    ff = FreeFile 'Get the first available file number (probably #1)
    Open "c:\test.dat" For Binary As #ff
    'Data written with Put is usually read from a file with Get
    Put #ff, , inABC
    'Close the file
    Close #ff
   
    'if we are here, it's success: Return true
    SaveArray = True
    Exit Function
SAVEERR:
    MsgBox "Error Writing Data. #" & Err.Number & " " & Err.Description, vbCritical, App.Title
    SaveArray = False
End Function

Public Function ReadArray(outABC() As Integer) As Boolean
'
'
'
    Dim ff As Long 'Used in the file write routine
   
    On Error GoTo READERR
   
    'redim to fit our global array
    ReDim outABC(UBound(ABC, 1), UBound(ABC, 2))
   
    'The next part reads the array from a binary file
    ff = FreeFile 'Get the first available file number (probably #1)
    Open "c:\test.dat" For Binary As #ff
    'Data read with Get is usually written to a file with Put
    'The array outABC is passed to this function ByRef, therefore the source array
    'in the calling procedure will be modified.
    Get #ff, , outABC
    'Close the file
    Close #ff
    ReadArray = True
    Exit Function
READERR:
    MsgBox "Error Reading Data. #" & Err.Number & " " & Err.Description, vbCritical, App.Title
    ReadArray = False

End Function


'Good Luck!
Erick:

What I need is to learn VB. To me it is as facinating as chess used to be. Thanks for your help. I shall today start studying all the responses not only to find the best but to learn the different approaches. With the comments that all provided, makes it easier. That which I do not understand, I can refer to the books with an easier approach.

Joe
bingie:
I took your code tried it and then went to the *.txt file but it was blank. I inputted the data, but only 10 lines long and it was cd(10, 2) but it did not work. The array was in memory. Now really the second column in this array is not needed, and do not know how to change it.

   i = 0
 
      Open "c:\program files\Microsoft Visual Studio\binary.txt" _
        For Input As #1
      Do Until EOF(1)
          Line Input #1, cd(i, 2) 'Save the current line at the current index i
    i = i + 1   'increment i
Loop   'Go to the next line in the file
Close

End Sub

If it can be this simple, wonderful. Due to the expected length of the program, I would like to call the function from a module.
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ERICK37:

I tried the following code, using the two dimentional array, and the *.Dat file was empty

 SAVE ARRAY

For i = 0 To 99
    cd(i, 2) = i
Next
 Open "c:\program files\Microsoft Visual Studio\binary.dat" _
        For Binary As #1

'write the array
Put #1, , cd
'point back to the head


Close #1

End Sub
'The next part writes the array to a binary file
    ff = FreeFile 'Get the first available file number (probably #1)
    Open "c:\program files\Microsoft Visual Studio\binary.dat" _
        For Binary As #ff
    'Data written with Put is usually read from a file with Get
    Put #ff, , cd   'cd(400,2)xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    'Close the file
    Close #ff

End Sub

*.Dat file empty
Is there any way that one can supply the exact code?

Use Dim ABC(400, 2)

MODULE FUNCTION EXACT CODE

CLICK EVENT

 cd(400,2) generated. 400 is a variable

Call function to save array

Click event

Call function to recall data

gonzal13(joe)
JimboMcGee

First of all the array ABC(k,2) can be as large as 400 rows

Ok, I opened a module and placed the following into it:

'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Global ABC() 'The () says 'Unknown Size' Global can be used anywhere

Public Sub ReadDataFromFile(ABC As Integer)

      Dim k As Long
      Dim Buffer As Integer ' ????
      DIM arrTempArray(400, 0) AS INTEGER
      DIM ABC(400,2) AS INTEGER

      'A SECTION OF CODE TO DETERMINE HOW BIG MY ARRAY SHOULD BE

      k = 0                  'RESET K (A COUNTER FOR MY ARRAY SIZE)

      Open Cards For Input As #1       'OPEN THE FILE
         Do Until EOF(1)   'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Line Input #1, Buffer   'READ AN ENTIRE LINE FROM THE FILE
            k = k + 1                  'INCREMENT THE COUNTER

         Loop
      Close #1     'CLOSE THE FILE


End Sub

      'A TEMPORARY ARRAY TO STORE THE FILE IN.

      Dim arrTempArray(k, 2)     'MAKE TEMPORARY ARRAY BIG ENOUGH TO HOLD FILE

      k = 0    'RESET k (NOW USED AS AN ARRAY MARKER)

      Open ABC For Input As #1      'OPEN THE FILE AGAIN, THIS TIME TO READ
         Do Until EOF(1)        'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Input #1, arrTempArray(k, 0), arrTempArray(k, 1), arrTempArray(k, 2)
                                                                   'READ THE INDIVIDUAL VALUES ON THE LINE IN THE ARRAY
            k = k + 1   'INCREMENT THE MARKER

         Loop
      Close #1            'CLOSE THE FILE

      'NOW WE HAVE A COMPLETE ARRAY, WE CAN COPY IT TO THE GLOBAL ARRAY.  THE GLOBAL ARRAY
      'WILL THEN RESIZE ITSELF TO FIT THE TEMPORARY ARRAY

      ABC() = arrTempArray()




CALL STATEMENT IN CLICK EVENT:




            Call ReadDataFromArray("C:\binary.dat")  'Not accepted



gonzal13(joe)

>> Public Sub ReadDataFromFile(ABC As Integer)

1.   You should not change the definition of the function, unless you know what you are changing:  by writing it
      so that ABC is passed to the function, instead of strFileName, you are now no longer dealing with a file
      and VB does not know what to do.  The function I wrote is designed to read data from a file, whose name
      you supply.


>>      Dim k As Long
>>      Dim Buffer As Integer ' ????
>>      DIM arrTempArray(400, 0) AS INTEGER
>>      DIM ABC(400,2) AS INTEGER

2.   You should not need to define ABC, or redefine arrTempArray, or change the type of Buffer.  This will
      affect (read: completely ruin) the action of the function;  I fear you are confusing yourself by making
      unnecessary changes.  arrTempArray sizes itself, based on the size of the file and copies it to arrMyArray
      when it is finished.  arrMyArray is the equivalent of your ABC so, if you must use ABC, you should rename
      all references to arrMyArray to ABC.  You _must_ do some reading on global variables and subs/functions
      -- it's an absolute minimum to understand why/how any of this code works.


>>            Call ReadDataFromArray("C:\binary.dat")  'Not accepted

3.   This is entirely my error.  It should read

            Call ReadDataFromFile("C:\binary.dat")

      as that is the name of the subroutine I wrote.  It should be noted that it still will not work, due to the
      changes you have made to my subroutine.


>> Is there any way that one can supply the exact code?

4.   I am afraid I am not goin to do that.  There is too much about the language you need to understand before
      you will be ready to implement this process.  Some of it is basic stuff that I fear you have skipped in favour
      of the more complex procedures.  You need to have a handle on the basics and, for that, you need to try to
      adapt the examples we have given to you, to match your own requirements.  I will, of course, help you
      with any questions you have, while doing this...

Avatar of brianb99999
brianb99999

hmmm,

Not much time at the moment.  Could you e-mail me with what you have at the moment (forms and modules).

Thanks,
Brian.
Brian:

All I want is to be able to save a generated variable array from a click event, then call to save it using a module. The call would be in any click event. The array would be variable integer up t 400 rows. The array is CD(q, 2). I checked it through a print routine and it contains all the numbers it is supposed to generate.

This is what was given to me and I do not understand it evidently:

In the Module:

Global Cards() 'The () says 'Unknown Size' Global can be used anywhere

Public Sub ReadDataFromArray(Cards As Integer)

      Dim k As Long
      Dim Buffer As Integer
      Dim arrTempArray(400, 2) As Integer
      Dim Cards(400, 2) As Integer
      'A SECTION OF CODE TO DETERMINE HOW BIG MY ARRAY SHOULD BE

      k = 0                  'RESET K (A COUNTER FOR MY ARRAY SIZE)

      Open Cards For Input As #1       'OPEN THE FILE
         Do Until EOF(1)   'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Line Input #1, Buffer   'READ AN ENTIRE LINE FROM THE FILE
            k = k + 1                  'INCREMENT THE COUNTER

         Loop
      Close #1     'CLOSE THE FILE


          'A TEMPORARY ARRAY TO STORE THE FILE IN.

           'MAKE TEMPORARY ARRAY BIG ENOUGH TO HOLD FILE

      k = 0    'RESET k (NOW USED AS AN ARRAY MARKER)

      Open ABC For Input As #1      'OPEN THE FILE AGAIN, THIS TIME TO READ
         Do Until EOF(1)        'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Input #1, arrTempArray(k, 0), arrTempArray(k, 1), arrTempArray(k, 2)
                                                                   'READ THE INDIVIDUAL VALUES ON THE LINE IN THE ARRAY
            k = k + 1   'INCREMENT THE MARKER

         Loop
      Close #1            'CLOSE THE FILE

      'NOW WE HAVE A COMPLETE ARRAY, WE CAN COPY IT TO THE GLOBAL ARRAY.  THE GLOBAL ARRAY
      'WILL THEN RESIZE ITSELF TO FIT THE TEMPORARY ARRAY

      arrABC() = arrTempArray()




End Sub


Now I do not know how to configure the Call statement for Save the file and then to read the file. I have the books,but I still do not understand. This was supplied bu jimbobmcgee, but I think he became frustrated with me which I can understand. It takes great patience to teach someone. What I do in Win98, I assume that the person does not have allot of knowledge and then I lead him through the complete process to correct his problem.

 An example was one that had his C:\ drive complete invisibe by one of Symantec products. He thought that it was a 'C:' drive it could mot be made a 'D' drive. He was trying everything possible, until I suggested the simple solution of purchasing a new HD, load the symanted product, and make his old drive a D drive. He thought one could not do such a thing.

gonzal13(joe)
Just to confirm (I might not be able to have much of a look till Thursday as I have been put on 3 projects at the moment),

What you want is:
1. Click a button on a form
1.1 Generate the array (you already have this part working?)
1.2 Save the array to a file
2. Click a different button on a form
2.1 Open the file created in step 1.2 and... what do you want to do with the retrieved data?  Put it back into the array?

Brian.
Hi:
Click a button on a form to generate the integer numbers, already coded
Generate an array of numbers that can vary from 52 to 400 in the click event
Call in the same click  event to save the array
Call to read the array into memory in another click event
After the list is read into the memory it will be massaged by other coding which I can take care of

gonzal13(je)
1. Click a button on a form to generate the integer numbers, already coded.
2. Generate an array of numbers that can vary from 52 to 400 in the click event.
You will need to code this, unless you tell me what the numbers that are generated are or are based on.
3. Some code that will help you:
NOTES:
Command1 is the name of your first command button, command2 is the second command button.
Fillarray you will need to code as I am unsure what numbers you want in your array.

Private Sub Command1_Click()
    Dim AnArray() As Integer    'The Array
    Call FillArray(AnArray)        'Call to the procedure that fills the array
    Call SaveArray(AnArray)     'Call to the procedure that saves the array to a file
End Sub

Private Sub FillArray(AnArray() As Integer)
    'You will need to write this procedure.  What I have in here will allow you to test
    Dim lngCounter As Long

    ReDim Preserve AnArray(lngCounter)
    AnArray(lngCounter) = 1
    lngCounter = 1
    ReDim Preserve AnArray(lngCounter)
    AnArray(lngCounter) = 2
    lngCounter = 2
    ReDim Preserve AnArray(lngCounter)
    AnArray(lngCounter) = 3
    lngCounter = 3
    ReDim Preserve AnArray(lngCounter)
    AnArray(lngCounter) = 4
    lngCounter = 4
    ReDim Preserve AnArray(lngCounter)
    AnArray(lngCounter) = 5
End Sub

Private Sub SaveArray(AnArray() As Integer)
    'Procedure saves the array to a flat file called C:\text.dat
    Dim intFileHandle As Integer
    Dim lngCounter As Long

    intFileHandle = FreeFile
    Open "c:\text.dat" For Output As #intFileHandle
    For lngCounter = LBound(AnArray) To UBound(AnArray)
        Print #intFileHandle, AnArray(lngCounter)
    Next lngCounter
    Close #intFileHandle
End Sub

Private Sub GetArray(AnArray() As Integer)
    'Returns an array which is loaded with the input from the flat file called C:\text.dat
    Dim intFileHandle As Integer
    Dim lngCounter As Long
    Dim strRETP As String

    intFileHandle = FreeFile
    Open "C:\text.dat" For Input As #intFileHandle
    Do While Not EOF(intFileHandle)
        ReDim Preserve AnArray(lngCounter)
        Line Input #intFileHandle, strRETP
        AnArray(lngCounter) = CInt(strRETP)
        lngCounter = lngCounter + 1
    Loop
    Close #intFileHandle
End Sub

Private Sub Command2_Click()
    Dim AnArray() As Integer

    Call GetArray(AnArray())    'Calls the procedure that loads the array from the flat file
End Sub

Hope that helps,

Brian.
>> This was supplied bu jimbobmcgee, but I think he became frustrated with me which I can understand.

I wasn't frustrated with you, Gonzal13, I was just concerned that I was bombarding you with stuff you weren't understanding and confusing you further.

Let's see if I can put a couple of pointers for the code I wrote:

    1.  Dim Cards(400, 2) As Integer
         You don't need this line because you already have  Global Cards()  at the top of your module.  If you are
         absolutely sure you will never need more than 400 rows, you can use Global Card(400, 2), but I was writing
         it as if you could never be entirely sure how many rows you are going to need.

    2.  The following section:
   
    'A SECTION OF CODE TO DETERMINE HOW BIG MY ARRAY SHOULD BE

      k = 0                  'RESET K (A COUNTER FOR MY ARRAY SIZE)

      Open strFileName for Input as #1       'OPEN THE FILE
         Do until EOF(1)   'KEEP READING FROM FILE UNTIL THE END OF THE FILE

            Line Input #1, strBuffer   'READ AN ENTIRE LINE FROM THE FILE
            k = k + 1                  'INCREMENT THE COUNTER

         Loop
      Close #1     'CLOSE THE FILE

      'NOW k CONTAINS THE NUMBER OF LINES IN MY FILE/HOW BIG MY ARRAY SHOULD BE I CAN MAKE
      'A TEMPORARY ARRAY TO STORE THE FILE IN.

      Dim arrTempArray(k, 2)     'MAKE TEMPORARY ARRAY BIG ENOUGH TO HOLD FILE

        creates an temporary array that only this function can see.  The routine loops through the file, getting the
        number of rows and uses that number to determine the size of the temporary array.

    3.  This line:

        arrMyArray() = arrTempArray()

         copies the temporary array to the global one, so other routines can see the data you load.

    4.   Putting this line:
 
        Call ReadDataFromFile("x:\mypath\myfile.dat")

          in your click event reads all rows from x:\mypath\myfile.dat to the global array

    5.   This routine:

           Public Sub WriteDataToFile(strFileName as String)
     
     Dim i as Long    'COUNTER FOR FOR/NEXT LOOP

     Open strFileName for Output as #1      'CREATE A NEW FILE

        For i = Lbound(arrMyArray(), 1) to Ubound(arrMyArray(), 1)    'LOOP THROUGH THE GLOBAL ARRAY

             Print #1, arrMyArray(i, 0) & "," & _
                          arrMyArray(i, 1) & "," & _
                          arrMyArray(i, 2)

        Next i

     Close #1        'CLOSE AND SAVE THE FILE

   End Sub

      would write the contents of the global array to the file.


    6.   By placing the following line in the click event of your Save button:

          Call WriteDataToFile("x:\mypath\myfile2.dat")

          you write the data in the global array to x:\mypath\myfile2.dat


HTH

J.
Do not worry about bombarding me with code I do not know. Having been an engineer I am used to jumping in with my two feet and blindfoled into projects. Besides as I said I have 4 reference books and the MSDN to help me, but they do not have actual code to accomplish a task as a sample in the area that I am interested. What I need to do is to understand how to code into a module a procedure that I can later call from a click event. If you use the nomenclature I use, then everything should go smoothly.

Will get back to you.

Joe
What I need to do is to understand how to code into a module a procedure that I can later call from a click event.

1.
Function = Will (should) always return a value
eg - the following will make the variable strReturnName equal to Joe,
dim strReturnName as String
strReturnName = GetName

private Function GetName() as string
    GetName = "Joe"
End Function

2.
Sub = Procedure that has no intrinsic return
eg,
Call DoSomething

Private Sub DoSomething()
    'Do some calculations or something else
End Sub

3. If you want it in a module instead of a form - remove the private clause at the start of the function or sub

4. To pass variables that you are using to a sub or function:
eg sub (similar with functions)
call MySub(myVariable1, myVariable2, "Hey")

Sub MySub(MyVariable1 as string, MyVariable2 as Integer, optional MyOptional1 as String = "Hi", MyOptional2 as string = "There")
    msgbox MyOptional1 & " " & myoptional2  'Will display 'Hey There' not 'Hi There'
End Sub

Optional means that you don't have to pass it a value.
There are a lot of other options in this, ie byval or byref that you should read up on.

Brian.
Thanks to both of you. Now it is my turn.

Joe
Out of curiosity would anyone be willing to write the specific code to save and read the array in a module and then the exact code for calling the read and save in click events?

 This way it would be easier for me to continue developing the program. It will serve as another guide on how to format the code in a module and click event. This somehow has hindered my progress. I have a long way to leering VB6 to be considered a fair programmer.

What data would be needed?

Normally what BillDll and I assume is assume a minimal level of expertise when commenting on a program. I copied this idea from BillDll.


Do I need to award the points and then open up another question on the same subject?


gonzal13(joe)
Hi Gonzal13,

Without knowing exactly what you are doing/having a copy of your existing code that would be difficult.
For a good general example of how to call a procedure from the click events see the post from 10/20/2004 03:49PM PDT

Brian.
Hi:

Ok, it seems that I am confusing every one, thus the poor response. I will open another question so that no one will have to read the various comments on this site.  Actually the end result will be an array that is populated'


I gues I frustrated every one. I did not mean to do it. All I need is the specific code, so thatI can add it to my 'tools' inventory .

Joe