Reference to a form

dsmaj

Registered User.
Local time
Today, 14:59
Joined
Apr 28, 2004
Messages
96
Hey folks, I've got a multi-purpose form that gets called from a bunch of other different forms, and it deals with data on those forms depending on which one called it. I currently have all of the calling forms calling this multi-purpose form by passing it Me.Name, and then in the multi-purpose form I use Select Case's to get data from the calling form. In the interest of code-reuse, and as compact a front-end to this database as possible, I would like to somehow set a reference to the calling form within ONE select-case in the form_load event...so I'd like to do something like this:

dim ParentForm as Form

Select Case OpenArgs
Case "Form1"
set ParentForm = Forms!Form1.Reference
Case "Form2"
set ParentForm = Forms!Form2.Reference
end select

Is there a way to get a reference from a form like that?

Thanks,
Sam.
 
G’day Sam.

Give this a shot: -

Code:
Private Sub Form_Open(ByRef intCancel As Integer)
    Dim frmParentForm As Form
    
    If Nz(Me.OpenArgs, "") <> "" Then
        Set frmParentForm = Forms(Me.OpenArgs)
    End If

End Sub
[color=green]'   Let's have a closer look at the reasons why this should be done.
'
'   In the first example below the variable Cancel should have some
'   naming convention applied and it would be better to state how
'   it is being passed. Sure, Micro$oft doesn't do it but we are not them.
'
'   Same goes for the variable ParentForm, would be better as frmParentForm.
'   As we go through our code we should be able to see the data type
'   without going back to the place it was defined.
'
'   Select Case OpenArgs is not protected against OpenArgs being a zero
'   length string or a Null if it could be. It probably won't be once
'   the database is up and running because all Forms that open this Form
'   would pass their name. But during development this Form may be opened
'   from the database window and under that circumstance OpenArgs would
'   not be defined. Therefore the Nz(Me.OpenArgs, "") <> "" check.
'   If Me.OpenArgs is Null then it is converted to a ZLS and if the result
'   is a ZLS then Me.OpenArgs is not used.
'
'   The code in red is not only repetitive and hard coded but also unnecessary.
'   By repetitive I mean that it appears in both the Case test and the Case statement.
'   By hard coded I mean that the absolute name of the calling Form is stated. It
'   would require adjusting the code, in two places, if the names changed or the list was
'   added to. Too much work, being lazy sometimes is good.
'
'   If OpenArgs has some value it should be the name of the Form that opened it.
'   The Forms collection can therefore be referenced with the string OpenArgs directly.
'[/color]
Private Sub Form_Open(Cancel As Integer)
    Dim ParentForm As Form
    
    Select Case OpenArgs
        Case "[color=red]Form1[/color]"
            Set ParentForm = Forms![color=red]Form1[/color].Reference
            
        Case "[color=red]Form2[/color]"
            Set ParentForm = Forms![color=red]Form2[/color].Reference
    End Select

End Sub
[color=green]'
'   And so...
'[/color]
Private Sub Form_Open(ByRef intCancel As Integer)
    Dim frmParentForm As Form
    
    If Nz(Me.OpenArgs, "") <> "" Then
        Set frmParentForm = Forms(Me.OpenArgs)
    End If

End Sub


Hope that helps.


Regards,
Chris.
 
Last edited:
Point taken Pat, and well understood and agree it could lead to confusion.

However, don’t you just love that word, the reason on my part is to educate when I can.

Therefore for all, (not you Pat you know what you are doing) there is a difference.

Know what is going on; don’t for one second think that source code that one may get from any other source is correct. Understand just what is correct.
 
While still agreeing totally with Pat, here is something else to think about while using default Micro$oft code.

1.**********
The name of the received argument, passed by the system, to a user Sub is very restricted in scope.

The name can be changed to almost anything you like (Please don’t use a reserved word.)
Cancel is not a reserved word in Access, it is simply the name of a passed variable.
The data type of Cancel can’t be changed but would have been better typecast as a Boolean.
(IMHO Cancel = True XOR False and can’t be ‘maybe’.)
Cancel must be received by reference ByRef, the system needs to know what is sent back.
The Sub can be defined as Public. (Never tried it but may have its usage.)
It can’t be redefined as a Function. (There is no return value, except by reference.)
(I guess system event procedures have been defined as Subs because some of the Subs can return multiple values.)

And all that comes from one line of Micro$oft code: -

Private Sub Form_Open(Cancel As Integer)
**********


2.**********
How about this line of Micro$oft code: -

Option Compare Database

I won’t even try…
**********

Only two examples of what Pat and I are talking about, and there are many others.

Everything that you see needs to be questioned, needs to be tested…needs to be taken with a grain of salt.

Can’t speak for Pat but I can for me.

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom