Array prepared within a VBA event. How to extract its contents from inside a different event?

浪尽此生 提交于 2020-01-07 02:16:08

问题


This question should apply to any VBA situ with events:

I've filled an array within a Form_Load() event in MS Access. Now I would like to extract, dereference, the contents of that array from within a ListBox_DblClick() event.

I Dim'd the array at the top of the form module (if it was Excel, it would be a sheet module) they both share; no luck in having the ListBox_DblClick event recognize that there is an array anywhere.

Thanks for any help:

Dim ArrWhatever() As String

Function ThisArr(tmpVal1, tmpVal2, tmpVal3)
 Dim numOfCols As Long
 Dim I, J, x As Long

 If Len(tmpVal1) > 0 Then
  ReDim Preserve ArrWhatever(numOfCols, 1 To J)
  Arr(1, J) = tmpVal1
  Arr(1, J) = tmpVal2
  Arr(1, J) = tmpVal3
  J = J + 1
 End If
End Function

Form_Load()
 ...
 retVal = ThisArr(val1, val2, val3)
End Sub

If the contents are subsequently extracted by using

For x = LBound(Arr, 2) To UBound(Arr, 2)
 Debug.Print ArrWhatever(1, x) & "  " & ArrWhatever(2, x) & "  " & ArrWhatever(3, x)
Next

from inside the Form_Load event, then everything is found.

But so far no luck in getting a different event adjacent on the same form to recognize the array.


回答1:


Let's start with a simple single Dimension example. Tested in access 2010.

Option Compare Database
Option Explicit

Dim singleArray() As String 'start with one element
Dim currentLength As Integer 'variable to keep track of number of elements


Private Sub Command0_Click()
    Debug.Print "Accessed from Event " & singleArray(0) 'access from event

    Call PrintArray 'calling function to print array
End Sub

Private Sub Form_Load()

    'make 3 calls to my function to populate array
    Call PopulateArray("val1")
    Call PopulateArray("val2")
    Call PopulateArray("val3")

    'print it
    Call PrintArray

End Sub

Sub PopulateArray(value As String)
   'all arrays are 0 based by default, so the first time it is called, it will create 1 element at position 0
    ReDim Preserve singleArray(currentLength)

    'put the value in the array
    singleArray(currentLength) = value

    'increment variable so the next time the function is called, the value will be placed one position higher
    currentLength = currentLength + 1

End Sub

Sub PrintArray()
    Dim x As Integer
    'loop through array and print values
    For x = LBound(singleArray) To UBound(singleArray)
        Debug.Print singleArray(x)
    Next
End Sub

Edit: double array sample, knowing the bounds up front

Option Explicit


Dim doubleArray() As String 'array declaration that will become two element array

Dim currentLength As Integer 'variable to keep track of number of elements for array 1


Private Sub Command0_Click()
    Debug.Print "Accessed from Event " & doubleArray(0, 0) 'access from event

    Call PrintArray 'calling function to print array
End Sub

Private Sub Form_Load()

    Dim arrayLength As Integer, arrayWidth As Integer

    'determine bounds of array
    arrayLength = 2 'can you write code to determine the length before you start adding records?
    arrayWidth = 1 'if you have a fixed number of elements on each "record", this can stay the same


    ReDim doubleArray(0 To arrayLength, 0 To arrayWidth)

    Call PopulateDoubleArray("val11", "val12")
    Call PopulateDoubleArray("val21", "val22")
    Call PopulateDoubleArray("val31", "val32")

    Call PrintArray


End Sub


Sub PopulateDoubleArray(value As String, value2 As String)

    'put the value in the array
    doubleArray(currentLength, 0) = value
    doubleArray(currentLength, 1) = value2

    'increment variable so the next time the function is called, the value will be placed one position higher
    currentLength = currentLength + 1

End Sub

Sub PrintArray()
    Dim x As Integer

    'loop through array and print values
    For x = LBound(doubleArray) To UBound(doubleArray)
        Debug.Print doubleArray(x, 0) & " " & doubleArray(x, 1)
    Next

End Sub



回答2:


If you had Option Explicit in your module, it would have directly given you a compiler error about the ambiguous variable name Arr.

You can't have the same name for a variable and a function.

Additionally, this line enforces variable declaration and reports undeclared variables/constants already at compile time.

To have this automatically in new modules, set the Require Variable Declaration option in the VBA Editor. This is really a must have for VBA development.



来源:https://stackoverflow.com/questions/37791608/array-prepared-within-a-vba-event-how-to-extract-its-contents-from-inside-a-dif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!