Solved

preserve an array to call back later

Posted on 2013-11-06
4
202 Views
Last Modified: 2013-11-06
Excel vba 2010


After filling a listbox with the  "Arr" array.
Is there a way to preserver it in memory and call it back if needed ?



Dim Arr As Variant
''code.....

With objMyRecordset
                 .ActiveConnection = objMyConn
                 .Open strsql
    
Arr = objMyRecordset.GetRows
       objMyRecordset.MoveFirst
' i need to preserve this Arr and call it back later if needed ?

Open in new window




Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 400 total points
ID: 39628834
Assign Dim Arr as Variant globally, as in, OUTSIDE the sub or function,

Global Arr as Variant

Be sure you're not changing it (Arr) often in other subs or functions, you might get confused about the results (being overwritten somewhere else at unknown times)
0
 
LVL 80

Assisted Solution

by:byundt
byundt earned 100 total points
ID: 39629041
From past experience, you will lose the values in your global array from time to time (such as after a fatal error). It is therefore good practice to test whether those values exist, and to renew them if not.

The code below creates an array arr (and displays a message) the first time GlobalArrayTester is run. If that sub is run again, it displays a message saying that the values already exist. If sub MakeAnError is run, then the values stored in memory are lost--and GlobalArrayTester will have to renew them the next time it runs.
Dim arr As Variant

Sub GlobalArrayTester()
If IsEmpty(arr) Then
    arr = Worksheets(1).Range("A1:A5").Value
    MsgBox "arr must be created"
Else
    MsgBox "arr still exists " & arr(4, 1)
End If
End Sub

Sub MakeAnError()
Dim ws As Worksheet
Set ws = Worksheets("abc")  'This worksheet doesn't exist, and so will cause run-time error
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:fordraiders
ID: 39629299
so, just by
Global Arr as Variant

This will keep it in memeory to call again later if need be ?
Thanks
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39629331
worked great Thanks !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

707 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

18 Experts available now in Live!

Get 1:1 Help Now