Solved

preserve an array to call back later

Posted on 2013-11-06
4
211 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 81

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

861 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

20 Experts available now in Live!

Get 1:1 Help Now