Calling Public Sub from an Array (1 Viewer)

KevinBaker

New member
Local time
Today, 16:16
Joined
Aug 23, 2020
Messages
25
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
 
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?
 
when the Form is not open, how can the Sub be accessed?
also using Call is not the appropriate action.
try googling CallByName.
 
Gasman - it's test each time the "Continue" button is clicked, which just runs modContinue again

arnelgp - CallByName did the trick! Thanks!
 
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

Back
Top Bottom