Instance

clsInstance helps to create and manage form instances. Using Form instances is an alternate technique for opening forms. Using a form instance makes it possible to open a second copy of a form that is already open in Form View so the application can show two records from the same table simultaneously for example.

Enumerated Variables: ddForms

Properties: Visible FormType Form

Methods: fCreateInstance CreateForm Move

Events Completed

Notes about implementing this class:

Declare a global constant in the application

Global Const gcEVNTPROC = “[Event Procedure]”

Create properties in each form to be instance UserChoice – long This property is used to pass a value back to the calling form Caller – used to identify the form that created the instance

enum ddForms should be modified so that there is one member for each form that will be instanced in the application.

objects should be declared a the module level of the form that will be using it: dim WithEvents frm as clsInstance

Qualifying the declaration with WithEvents allows the calling form to handle the close event of the form instance.

A sample application using clsInstance can be download from here: Media:Demo_EventsAndCollections.zip.

Synopsis

Help with creating form instances

' Instance
' http://www.utteraccess.com/wiki/Instance
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  {{{YYYY-MM-DD}}}
'
' Module    : clsInstance
' DateTime  : 3/6/2014 11:47
' Author    : UA Contributor
' Purpose   : manage form instances
'
' The enums in this class are application dependant
' modify fcreateinstance and enum definition to reflect forms in the application
' to be instanced using this class
'
' additional properties and methods added by UA Contributor
'
' move - call form's move method
'---------------------------------------------------------------------------------------
Option Compare Database
Option Explicit


Private Const cstrModule As String = "clsInstance"

Public Enum ddForms
'declare enum variables here to correspond to the name of each form that will be instanced
'replace these identifiers with names that correspond to table names in the application
'where you are deploying clsInstance:

frmNewPeople = 1
frmDemoEvents
frmDemoCollections

End Enum

Public Event Completed(Result As Long)


Private WithEvents mfrm As Form

Private mFormType As ddForms
Private mlngstrUserChoice As Long
Private Property Get strUserChoice() As Long
On Error GoTo HandleExit

strUserChoice = mlngstrUserChoice
HandleExit:
End Property
Private Property Let strUserChoice(rData As Long)
On Error GoTo HandleExit

mlngstrUserChoice = rData
HandleExit:
End Property


Public Sub Move(Left As Long, Optional Top As Variant, Optional Width As Variant, Optional Height As Variant)
' Procedure: move
' DateTime: 3/7/2014 9:51:46 AM
' Author: UA Contributor
' Description: call the form's move method to position it
'--


Const cstrProcedure = "move"
Dim lngTop As Long
Dim lngWidth As Long
Dim lngHeight As Long

On Error GoTo HandleError

If IsMissing(Top) Then
lngTop = Form.WindowTop
Else
lngTop = Top

End If

If IsMissing(Width) Then
lngWidth = Form.Width
Else
lngWidth = Width
End If

If IsMissing(Height) Then
lngHeight = Form.InsideHeight
Else
lngHeight = Height
End If


mfrm.Move Left, Top, Width, Height

HandleExit:

Exit Sub

HandleError:
End Sub


Public Property Get Visible() As Boolean
If Not mfrm Is Nothing Then
Visible = mfrm.Visible
End If
End Property

Public Property Let Visible(pSetting As Boolean)
If Not mfrm Is Nothing Then
mfrm.Visible = pSetting
End If
End Property

Public Property Get Form() As Form
Set Form = mfrm
End Property

Public Property Let FormType(pForm As ddForms)
mFormType = pForm
End Property

Public Property Get FormType() As ddForms
FormType = mFormType
End Property

Public Sub CreateForm(Optional pForm As ddForms)

If pForm > 0 Then
mFormType = pForm
End If

Set mfrm = fCreateInstance(mFormType)
mfrm.OnClose = gcEVNTPROC

End Sub

Private Sub mfrm_Close()

RaiseEvent Completed(Nz(mfrm.UserChoice, 0))
'Set mfrm = Nothing

End Sub

Private Function fCreateInstance(pForm As ddForms) As Form
Const cstrProcedure = "fCreateInstance"


Select Case pForm

'use enums from ddForms to control which form gets instanced
'use the Class Objects for each form to instance
Case frmNewPeople
Set fCreateInstance = New Form_frmNewPeople
Case frmDemoCollections
Set fCreateInstance = New Form_frmDemoCollections
Case frmDemoEvents
Set fCreateInstance = New Form_frmDemoEvents

End Select

End Function
'
' Demonstration Function
'
{{{FunctionBody}}}