Search code examples
databasems-access

Access form cannot access previous form variable


I have a form which have referenced to previous form variable in the following code. I have change the scope to Public for the variable WorkflowName, however, error still happened.

Form 1: 

Option Compare Database
Option Explicit
Public WorkflowName As String

Private Sub btn_CreditControlEmail_Click()
    WorkflowName = "Email to Credit Control"
    DoCmd.OpenForm "WorkFlow Email Preview"
End Sub
  
Form 2: 
Option Compare Database
Dim frmPrevious As Form
Dim WorkflowName As String

Private Sub btn_ToOutlook_Click()

    'update workflow status to Renewal Table
    MsgBox frmPrevious![WorkflowName]
    
End Sub

Private Sub Form_Load()
    Set frmPrevious = Screen.ActiveForm
End Sub

However, when run to second form

MsgBox frmPrevious![WorkflowName]

The following error happened.

The error message

In watching the value of frmPrevious in second form, I can see the "WorkflowName" value of frmPrevious.

enter image description here

enter image description here


Solution

  • If you want to refere to a variable in another form you can't use frmPrevious![WorkflowName], because that refers to a field in the first form that does not exist.

    But use this: a dot and without brackets:

    MsgBox frmPrevious.WorkflowName
    

    Edit:

    Another way to do this, and get the Intelisense to work, is by using the Form_ syntax. Lets say Form 1 is named "WorkflowStart"

    MsgBox Form_WorkflowStart.WorkflowName