WithEvents & Form Class For All Forms

Mile-O

Back once again...
Local time
Today, 05:38
Joined
Dec 10, 2002
Messages
11,316
Okay, I'm sick of designing forms whereby I have to go through their individual settings and set things like PopUp to True or RecordSelectors to False.

Therefore I want to create a form class that, in the Form_Open event will do all that form me for every form.

So, I've got a class called AppForm.

Code:
Option Compare Database
Option Explicit

Private Const cstrEventProc As String = "[Event Procedure]"

Private WithEvents frm As Access.Form

Public Property Get Form() As Access.Form
    Set Form = frm
End Property ' Form

Public Property Set Form(param_Form As Access.Form)
    Set frm = param_Form
    frm.OnOpen = cstrEventProc
End Property ' Form

Private Sub frm_Open(Cancel As Integer)

    With frm

        ' some general form attributes
        .Caption = ThisApp.Name
        .PopUp = True
        .AutoCenter = True
        .NavigationButtons = False
        .ScrollBars = 0
        .BorderStyle = 1
        .Moveable = True
        .ControlBox = False
        .MinMaxButtons = False
        .RecordSelectors = False
        
    End With
    
End Sub ' frm_Open

Now, that doesn't work, and I don't expect it to work because on each form I need to do some work with regards to instantiating the class.

So, on the form's class module

Code:
    Private ThisForm As New AppForm


    Private Sub Form_Open(Cancel As Integer)
    
    End Sub

ThisApp is just another class, dealing with database application stuff, like Name, version, etc. Just in case anyone was wondering.

Now, how do we go about assigning the form to the class as the first event triggered for a form is OnOpen, which is what I want to replace with the OnOpen in the AppForm class?

I guess there must be a way by adding all the database's form collection to a collection object at start-up so that the class will work. But I'm stumped. :(
 
Answering my own questions now.

Basically, here's my new AppForm class.

Code:
Option Compare Database
Option Explicit

Private WithEvents frm As Access.Form

Public Sub SetAttributes(ByRef param_frm As Form)

    Set frm = param_frm

    With frm

        ' some general form attributes
        .Caption = ThisApp.Name
        .NavigationButtons = False
        .ScrollBars = 0
        .Moveable = True
        .ControlBox = False
        .RecordSelectors = False
        
    End With
    
    Set frm = Nothing
    
End Sub ' frm_Open

And in every form:

Code:
    Option Compare Database
    Option Explicit

    Private ThisForm As New AppForm

    Private Sub Form_Open(Cancel As Integer)
    
        ThisForm.SetAttributes Me
        
    End Sub

Although it turns out some form properties can't be changed at runtime. Oh well.
 

Users who are viewing this thread

Back
Top Bottom