Calling Public Sub from an Array (1 Viewer)

KevinBaker

New member
Local time
Today, 01:52
Joined
Aug 23, 2020
Messages
22
In a module I have:

Code:
Public FormArray(9) As String

Public Sub modContinue()
  If FormArray(0) <> "" Then
    Select Case left$(FormArray(0), 2)
      Case "fr": DoCmd.OpenForm FormArray(0)
      Case "cm": Call FormArray (0)
    End Select
    
    'Shift the remaining elements up one
    For i = 1 To UBound(FormArray)
      FormArray(i - 1) = FormArray(i)
    Next
  Else
    If Not IsNull(TempVars!frmReport) Then
      Forms(TempVars!frmReport).Visible = True
    End If

    'Open the report
    On Error Resume Next
    DoCmd.OpenReport TempVars!txtRptName, View:=acViewPreview
            
    If Err = 2501 Then
      Err.Clear
      Exit Sub
    End If
    DoCmd.Maximize
  End If
End Sub

On a FormA if I have a Public Sub called cmHelloWorld like this:
Code:
Public Sub cmHelloWorld()
  MsgBox "Hello World"
End Sub

Using a button on the form I run this code:
Code:
FormArray(0) = "frmTest2"
FormArray(1) = "cmHelloWorld"
Call modContinue

When I press the button frmTest2 opens correctly, however when I click the Continue button on frmTest2, which calls modContinue, trying to run cmHelloWorld fails with "Expected procedure, not variable
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:52
Joined
Sep 21, 2011
Messages
14,528
Walk though your code line by line with F8.
You shift the array values up one, but how do you test it again?

Why is there a space between FormArray and (0) for cm ? and not elsewhere?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:52
Joined
May 7, 2009
Messages
19,248
when the Form is not open, how can the Sub be accessed?
also using Call is not the appropriate action.
try googling CallByName.
 

KevinBaker

New member
Local time
Today, 01:52
Joined
Aug 23, 2020
Messages
22
Gasman - it's test each time the "Continue" button is clicked, which just runs modContinue again

arnelgp - CallByName did the trick! Thanks!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:52
Joined
Feb 19, 2002
Messages
43,587
Put your common code in a standard code module. It does not belong in a form's class module. The only time I would put a shared module in a form's class module is if the procedure is only called from the form.

Good organization is good practice.
 

Users who are viewing this thread

Top Bottom