[AccessD] Doing some thing the class way... was Toolbar

John Colby jwcolby at gmail.com
Sat Jan 29 08:38:22 CST 2022


LOL, I do something on my keyboard that presses "send" in Gmail!  Then I
couldn't see that to continue the thread.  Plus I got something from the
moderator saying something was too long.  At any rate, the form's OnOpen
sets the pointer to my class and then calls the fInit(me) which passes in a
pointer to itself (the form itself).

Setting a pointer to my clsFrm prevents it from unloading until the form
closes. Passing in the pointer to me allows the clsFrom to sink events.

http://jwcolby.blogspot.com/2013/02/a-form-wrapper-to-control-them-all-in.html?view=sidebar



On Sat, Jan 29, 2022 at 9:10 AM Bill Benson <bensonforums at gmail.com> wrote:

> John what is the
>    function form_OnOpen
>
> and where is that? It appears to be hanging out below a sentence that says
> what you are doing in the onOpen event of your form.
>
> I have more response prepared but wanted to ask this separately.
>
> On Sat, Jan 29, 2022 at 8:57 AM John Colby <jwcolby at gmail.com> wrote:
>
> > Bill,
> >
> > If I create my own class to "wrap" an object, I have to dimension a
> > variable to hold a pointer to my class after I open it.  Let's assume I
> > want to create a clsFrm to wrap my form and sink events from my form.
> >
> > 1) I create my clsFrm.
> > 2) In the header of my clsFrm I create a variable
> >
> > dim frm as form
> >
> > 3) I create an Init function
> >
> > function finit(lfrm as form)
> >    set frm = lfrm
> > end function
> >
> > Back in the header of my form's cbf module I dim a pointer to my class
> >
> > dim gclsFrm as clsFrm
> >
> > In The OnOpen of my form I instantiate my class
> >
> > function form_OnOpen()
> >
> >
> >
> >
> > On Sat, Jan 29, 2022 at 7:38 AM Bill Benson <bensonforums at gmail.com>
> > wrote:
> >
> > > John - I need to ask for clarification on a few definitions and
> > procedures
> > > in your explanation.
> > >
> > > 1. What does CBF stand for?
> > > 2. Where and how do you create instances of the class module you are
> > > assigning forms to?
> > >
> > > I have a new database with only one form named Form1, which has no code
> > in
> > > its code module, and which has had added to it, in design view before I
> > > closed and saved it, a single control (command button) named Command0.
> I
> > > will next write some code in a class module I named clsForm, and a
> > > procedure named LoadForms in a standard module named Module1; I will
> > single
> > > step through LoadForms and report on intermediary results. As I will
> hit
> > a
> > > runtime error very quickly, I will ask you some questions.
> > >
> > > 'clsForm
> > > Option Compare Database
> > > Option Explicit
> > > Public WithEvents MyForm As Form
> > >    Private Sub MyForm_Load()
> > >        g_i_IncrementForms = g_i_IncrementForms + 1
> > >        MsgBox "Instance " & g_i_IncrementForms & " of Form_" &
> > > Me.MyForm.Name & " was unloaded"
> > >    End Sub
> > >
> > >    Private Sub MyForm_Unload(Cancel As Integer)
> > >        MsgBox "Instance " & g_i_IncrementForms & " of Form_" &
> > > Me.MyForm.Name & " was unloaded"
> > >    End Sub
> > >
> > > 'Module1
> > > Option Compare Database
> > > Option Explicit
> > > Public g_i_IncrementForms   As Long
> > > Public g_col_Forms          As Collection
> > > Sub LoadForms()
> > >    Dim clsForm     As clsForm
> > >    Dim Form        As Object
> > >    Dim Forms       As Object
> > >
> > >    Set Forms = Application.CurrentProject.AllForms
> > >    For Each Form In Forms
> > >       Set clsForm = New clsForm
> > >       g_i_IncrementForms = 0
> > >       Set clsForm.MyForm = Form
> > >       If g_col_Forms Is Nothing Then
> > >         Set g_col_Forms = New Collection
> > >       End If
> > >       g_col_Forms.Add clsForm
> > >    Next
> > > End Sub
> > >
> > > Single-Stepping through Module1.LoadForms: Before the line Set
> > > clsForm.MyForm = Form, the immediate window can give me these results:
> > >    ?TypeName(Forms)
> > >      AllObjects
> > >    ?TypeOf Forms is AllForms
> > >      True
> > >    ?TypeOf Form is Object
> > >      True
> > >    ?TypeOf Form is Form
> > >      False
> > >    ?Forms(0).name
> > >      Form1
> > >
> > > Some things to note:
> > >     In the construction above, Form is declared as Object.
> > >     The line Set clsForm.MyForm = Form, will throw a runtime error.
> > >
> > > If instead this declaration had been used
> > >    Dim Form        As Form
> > >
> > > then this line would have produced a runtime error:
> > >    For Each Form In Forms
> > >
> > > Therefore, in order to have some kind of collection to loop *prior* to
> > > opening any forms, I have to deal with the AllObjects class with
> members
> > of
> > > type Object. Declaring the MyForm object in the clsForm class module is
> > > what causes the assignment  Set clsForm.MyForm = Form to result in a
> type
> > > mismatch error.
> > >
> > > So given my results, I concluded - you will apparently tell me WRONGLY
> -
> > > that using class modules to house a Form's events was not possible
> until
> > > the Form itself has been opened.
> > >
> > > So in order for me to understand what you are implying by stating the
> > class
> > > module approach can be used to hold the Form's events, I need you to
> show
> > > me some code that will create an instance of clsForm for each Form type
> > > access object. That will enable me to apply your methods. Your
> > explanation
> > > so far confuses me, since you talk about declaring things "in the Form
> > > header". What Form header, in the code module of the Form? If that is
> so,
> > > then the Form has to be opened in order for those objects to get
> assigned
> > > something in memory. Until then, I assume they are either allocated
> some
> > > memory, or are in fact nothing at all, prior to a particular Form
> > opening.
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list