Solved

Write array of Long to a file, and read it back to array Long VBA

Posted on 2014-10-15
3
153 Views
Last Modified: 2014-10-16
Hi experts,

A VBA problem here:

I have declared and initialised the array below:

Dim arr1() As Long

I'd like to do two things:
1. Write it to a file
2. Initialise the array below with the file contents.

Dim arr2() As Long

I will accept solution / provide max points if:
1. Code can be copy/paste to any VBA macro and will work
2. arr2 is an array of Long
3. arr1 and arr2 have exactly the same data

Best,
Blueoris
0
Comment
Question by:Juan Villanueva
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40383393
Here are two macros. The first which writes the data and the second that reads it.

Sub WriteArrayToDisk()
Dim FF As Integer
Dim arr1() As Long
Dim lngIndex As Long
Dim strValues As String

ReDim arr1(4) As Long

arr1(0) = 12345
arr1(1) = 54321
arr1(2) = 999999
arr1(3) = 88888
arr1(4) = 7777

For lngIndex = LBound(arr1) To UBound(arr1)
    If lngIndex < UBound(arr1) Then
        strValues = strValues & arr1(lngIndex) & ","
    Else
        strValues = strValues & arr1(lngIndex)
    End If
Next

FF = FreeFile

Open "C:\temp\MyFile.txt" For Output As #FF
Print #FF, strValues
Close

End Sub
Sub ReadArrayFromDisk()
Dim FF As Integer
Dim arr2() As Long
Dim strParts() As String
Dim strValues As String
Dim lngIndex As Long

FF = FreeFile

Open "C:\temp\MyFile.txt" For Input As #FF
Line Input #1, strValues
Close

strParts = Split(strValues, ",")
ReDim arr2(UBound(strParts))

For lngIndex = 0 To UBound(strParts)
    arr2(lngIndex) = strParts(lngIndex)
Next

' Show the results
For lngIndex = 0 To UBound(arr2)
    Debug.Print arr2(lngIndex)
Next

End Sub

Open in new window

0
 

Author Closing Comment

by:Juan Villanueva
ID: 40383620
You are a legend. Thanks!
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40384306
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now