saving a array to disk

C

cliff

i am a old bloke trying to teach myself to program.
i sish to save a 3 demensional aray (structur and data) to disk and return
it at a later date from a diferent module usingvba in access 2003.
i have read of openfile,put and get but i am having truble puting them
together can sombody give me a example
 
J

John W. Vinson

i am a old bloke trying to teach myself to program.
i sish to save a 3 demensional aray (structur and data) to disk and return
it at a later date from a diferent module usingvba in access 2003.
i have read of openfile,put and get but i am having truble puting them
together can sombody give me a example

Given that you're using a database, why not store the data in a Table in your
database? A three dimensional array maps precisely to a three-field table. By
writing to an external text file you're forcing yourself to reinvent... a
database!

It can be done, and it might be a useful learning experience, but if the
purpose is in fact to save the data, why not use the tools to hand?
 
C

Clifford Bass via AccessMonster.com

Hi Cliff,

Is there a specific need to save it to a separate file? If so: To read
and write from/to disk you can use the TextStream object and the ReadLine,
Read, Write, or Writeline methods. Do a search for those words in the VBA
help. Or you can use the Input #, Print #, Write # with a file opened with
the Open statement. These would probably be easier to use than the Put # and
Get # statements which operate at more detailed level than you probably need.

If you do not need to save it to a separate file, you could just save it
in a table that might look something like this:

tblMyArray
X_Coordinate
Y_Coordinate
Z_Coordinate
The_Value

If it is a sparse array, you could save only those items that contain
values. Then when you need to reload it, initialize your array variable
first, and then load the values from the table. Air code, not complete.

Public Sub CreateAndSaveArray()

Dim rs As DAO.Recordset
Dim intarrValues(20, 30, 40) As Integer
Dim intX As Integer
Dim intY As Integer
Dim intZ As Integer

' Stuff that populates the array

' Save the array
' Open the table as a recordset
' Use three nested For...Next loops to go through the array and save
' each value as a record in the table
For intX = 1 To 20
For intY = 1 To 30
For intZ = 1 To 40
rs.Add
rs!X_Coordinate = intX
rs!Y_Coordinate = intY
rs!Z_Coordinate = intZ
rs!The_Value = intarrValues(intX, intY, intZ)
rs.Update
Next intZ
Next intY
Next intX
' Close the recordset

End Sub

Public Sub LoadAndUseArray()

Dim rs As DAO.Recordset
Dim intarrValues(20, 30, 40) As Integer
Dim intX As Integer
Dim intY As Integer
Dim intZ As Integer

' Load the array
' Use three nested For...Next loops to go through the array and clear all
values
' Open the table as a recordset (i.e. named rs)
' Read each record and place value in array
Do While Not rs.EOF
intarrValues(rs!X_Coordinate, rs!Y_Coordinate, rs!Z_Coordinate) = rs!
The_Value
rs.MoveNext
Loop
' Close the recordset

' Stuff that uses the array

End Sub

Hope this helps,

Clifford Bass
 
C

Clifford Bass via AccessMonster.com

Hi Cliff,

There is also the Line Input # statement that may be of use to you.

Clifford Bass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top