Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

preserve an array to call back later

Posted on 2013-11-06
4
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 1600 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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

704 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