Home > Blockchain >  MS Access: instantiating a form within a custom class
MS Access: instantiating a form within a custom class

Time:01-04

I am trying to instantiate an existing form (frmVisibleForm) in my project from within a custom class module (clsMBox) and manipulate its properties from there too. I want to be able to use events from the form.

What I expect to happen:

  1. The Form frmVisibleForm is instantiated but invisible
  2. The Form gets set to Modal
  3. The Form gets set to Visible
  4. The Form gets Focus

What happens:

Nothing. No form shows up, no error messages, no prompts, nothing happens at all when running the test module´s function. Its my first time trying out custom classes in access so maybe I made some fundamental error but I can't figure out why it doesn´t work the way I thought it would. Appreciate any help.

Here is the code I have so far:

The Form (frmVisibleForm):

Option Compare Database
Option Explicit

Public Event DataInput(InputValue As String)

(No actual events thus far)

The Custom Class Module (clsMBox):

Option Compare Database
Option Explicit

Dim WithEvents cls_frmVisibleForm As Form_frmVisibleForm

Private Sub InstantiateForm()

    Set cls_frmVisibleForm = New Form_frmVisibleForm

    With cls_frmVisibleForm
    
        .Modal = True
        .Visible = True
        .SetFocus
        
    End With

End Sub

The Module I try to test it from (mdlTestMBox):

Option Compare Database
Option Explicit

Public Function ClassTest()

    Dim mbox As clsMBox
    
    Set mbox = New clsMBox
    
End Function

CodePudding user response:

I guess you need to either make InstantiateForm a public method, and then call that, or rename it to initialise:

Private Sub Class_Initialize()

    Static cls_frmVisibleForm As Access.Form

    Set cls_frmVisibleForm = New Form_frmVisibleForm

    With cls_frmVisibleForm
    
        .Modal = True
        .Visible = True
        .Move 0, 0
        
    End With

End Sub

To open and close an instance of the form:

Public Function ClassTest()

    Static mbox As clsMBox
    
    Set mbox = New clsMBox
    
    Stop
    DoCmd.Close acForm, Forms(0).Name
  
End Function
  • Related