galvanize
galvanize

Reputation: 547

Use a dynamic array in multiple forms using Access

With single variable everything works fine, problems appear with dynamic array.

There is a module with global variables:

Option Compare Database
Option Explicit

Global gbl_numberOfPositions As Integer
Global gbl_numberOfDisciplines As Integer
Global gbl_mv_clsJobPostions() As clsJobPostion

Public Sub Init_Globals()
    gbl_numberOfPositions = 0
    gbl_numberOfDisciplines = 0
    ReDim gbl_mv_clsJobPostions(0)
End Sub

In the first form dynamic array is defined

Private Sub Form_Load()

    Call Init_Globals
End Sub

Private Sub InitCBox()
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset(strSQL)
rs.MoveLast
ReDim gbl_mv_clsJobPositions(rs.RecordCount)
rs.MoveFirst
i = 1
Do While (Not rs.EOF)
    Set gbl_mv_clsJobPositions(i) = New clsJobPostion
    gbl_mv_clsJobPositions(i).InitializeMe _
        rs![ID Job Position], rs![ID Position], rs![ID Discipline]
    i = i + 1
    rs.MoveNext
Loop
Debug.Print UBound(gbl_mv_clsJobPositions)
End Sub

Then second one is loaded:

Private Sub Form_Load()
    Debug.Print UBound(gbl_mv_clsJobPostions)
End Sub

However two different are returned. In the second case it is zero.

So my question is how to pass dynamic arrays between forms?

Upvotes: 1

Views: 1128

Answers (3)

Smandoli
Smandoli

Reputation: 7019

You seem to have two different spellings, and that may be causing the problem:

  • gbl_mv_clsJobPostions
  • gbl_mv_clsJobPositions

Also, I'm wondering why you use this:

Global gbl_mv_clsJobPostions() As clsJobPostion
                                  -------------

Why not use As String, or variant, or some other standard variable type?

Upvotes: 0

Smandoli
Smandoli

Reputation: 7019

Instead of:

ReDim gbl_mv_clsJobPositions(rs.RecordCount)

Use:

ReDim Preserve gbl_mv_clsJobPositions(rs.RecordCount)

Upvotes: 1

Smandoli
Smandoli

Reputation: 7019

From another SO post:

Public and Global are nearly identical in their function, however Global can only be used in standard modules, whereas Public can be used in all contexts (modules, classes, controls, forms etc.) Global comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public.

Upvotes: 0

Related Questions