Is it possible to identify from which form another form is opened?

cigarprofiler

Registered User.
Local time
Yesterday, 16:31
Joined
Mar 25, 2017
Messages
32
I have a form2 that opens by clicking a button on form1. On loading form2, some VBA code is executed.

I would like to create an option to open form2 from another form, say form3, and perform a different bit of code on loading.

Is it possible to identify whether form2 is opened from form1 or form3?

What I'm looking for is something like this:

If form2 is opened from form1: do this
If form2 is opened from form3: do that

I haven't been able to find anything on this forum or Google, because I don't know what to look for. Maybe something like passing the name of the originating form to form2?
 
Last edited:
you could pass the form name in the OpenArgs argument of the docmd.openform method. Then in the opened form use a select case in the form load event.
 
The answer to this question is embodied in the Old Programmer's Rule #2: Access won't tell you anything you didn't tell it first.

If you want to know how a form was opened, find a way to tell the targeted form that it was launched by API, not by GUI. Moke's OpenArgs suggestion is the easiest answer, but not necessarily the only way.
 
As Doc said, there may be other ways. One thing I often do for testing and debugging is to put conditional debug.print statement(s) in various events and/or function/sub calls. You can set the Debug condition to True, and write simple statements to the immediate window to show logic flow which you can review. You can always shut condition off, or turn those statements into comments.

Good luck.
 
I have created a class module for this purpose. Attached is a sample database which implements the class module. The class module can also return other information from the calling form including the actual "Call Form" itself. You can then code against the "Call Form".

In the example is an instance of the Class module named CallCalled you immediately have available the following information from the Calling Form:-



Code:
    CallCalled.prpCallingCtrlName             'The name of the Calling Control
    CallCalled.prpCallingForm                 'The Calling form as an Object
    CallCalled.prpPrincipleCtrl               'the Principal Control, this is the Control linked to the Calling Control
    CallCalled.prpPrincipleCtrlCaption        'the Caption of the Principal Control

{AVAILABLE from the CALLING Form; in the CALLED Form}

Also any valid operation you perform on the Principal Control Object:- "CallCalled.prpPrincipleCtrl" (Linked to the Control on the Calling Form ByRef {I think} ) is reflected in that "originating" Control.... Yes, I know, I don't understand it either!

I did a YouTube Play list about it HERE:- Object Oriented - MS Access - VBA
 

Attachments

Last edited:
Thanks for the replies, everyone. I'm going to go with Moke's suggestion first, mostly because I don't fully understand what the others are saying, being an unexperienced VBA-user.

:o

ETA: I didn't notice Uncle Gizmo's file at first. Thanks, that helps a lot!
 
Last edited:
[Edit: After rereading, I guess this doesn't really answer the question, but may be useful in cases when just need to know if a form is opened]

I've used this code to test if a form is open.

If funIsLoadedForm(strFormName) = True Then...

This goes in a standard code module
Code:
'---------------------------------------------------------------------------------------
' Procedure : funIsLoadedForm
' Author    : Neville Turbit
' Date      : 04/06/09
' Purpose   : Checks if a form is loaded
'             Returns True if the specified form is open in Form view or Datasheet view.
'---------------------------------------------------------------------------------------
'
Public Function funIsLoadedForm(ByVal strFormName As String) As Boolean
    On Error GoTo Error_funIsLoadedForm

    Const conObjStateClosed = 0
    Const conDesignView = 0

    If SysCmd(acSysCmdGetObjectState, acForm, strFormName) <> conObjStateClosed Then
        If Forms(strFormName).CurrentView <> conDesignView Then
           funIsLoadedForm = True
        End If
    End If
    
Exit_funIsLoadedForm:
    On Error GoTo 0
    Exit Function

Error_funIsLoadedForm:

    MsgBox "An unexpected situation arose in your program." & funCrLf & _
           "Please write down the following details:" & funCrLf & funCrLf & _
           "Module Name: modGeneric" & funCrLf & _
           "Type: Module" & funCrLf & _
           "Calling Procedure: funIsLoadedForm" & funCrLf & _
           "Error Number: " & Err.Number & funCrLf & _
           "Error Descritption: " & Err.Description
           
    Resume Exit_funIsLoadedForm
    
End Function
He has some other useful functions too.
http://www.projectperfect.com.au/microsoft-access-sample-10.htm
 
Last edited:
Hi

Further to the last post, here is an alternative procedure for determining if a form or report is loaded:

Code:
Public Function IsLoaded(MyFormName)

On Error Resume Next

' Accepts: a form/report name
' Purpose: determines if a form/report is loaded
' Returns: True if specified the form/report is loaded;
'          False if the specified form/report is not loaded.
' Example: IsLoaded("frmMainMenu")

    Dim I As Integer

    IsLoaded = False
    For I = 0 To Forms.Count - 1
        If Forms(I).FormName = MyFormName Then
            IsLoaded = True
            Exit Function
        End If
    Next
    
    For I = 0 To Reports.Count - 1
        If Reports(I).Name = MyFormName Then
            IsLoaded = True
            Exit Function
        End If
    Next

End Function

And on the same theme, this is used for tables:

Code:
Public Function IsTableOpen(strName As String) As Boolean
IsTableOpen = SysCmd(acSysCmdGetObjectState, acTable, strName)
'Debug.Print IsTableOpen
End Function
 
This is what I wanted to post but couldn't remember where I had it, this is much simpler and will work for tables, queries, forms, reports and modules to determine if it is open or not.

Code:
Function IsOpen(strname As String, strtype As String) As Boolean
'http://www.techrepublic.com/blog/microsoft-office/how-to-check-for-open-access-objects/
'strTypes: Table=0, Query=1, Form=2, Report=3, Module=5
    If SysCmd(acSysCmdGetObjectState, strtype, strname) <> 0 Then
        IsOpen = True
    End If
End Function
 
what is wrong with using the isloaded property of the allforms collection?

CurrentProject.AllForms("myform").IsLoaded
 
what is wrong with using the isloaded property of the allforms collection?

CurrentProject.AllForms("myform").IsLoaded

Absolutely nothing.
This only goes to show once again that there are many ways to skin a cat ....

And, of course that method can also be adapted for reports, modules & macros.
However, I believe it doesn't work for tables or queries

One advantage of the code posted by @sxschech is that it will work for all object types.
In fact for completeness, I've added macros below (even though I don't use them ... other than Autoexec / Autokeys).

Code:
Function IsOpen(strname As String, strtype As String) As Boolean
'http://www.techrepublic.com/blog/microsoft-office/how-to-check-for-open-access-objects/
'strTypes: acTable=0, acQuery=1, acForm=2, acReport=3, acMacro=4,  acModule=5
    If SysCmd(acSysCmdGetObjectState, strtype, strname) <> 0 Then
        IsOpen = True
    End If
End Function

However, I can never remember the object type numbers so instead I'd use: acTable, acForm etc

I've not tested whether this can be used with other object types such as:
acServerView=7; acDiagram=8 ; acStoredProcedure=9;acFunction=10; acTableDataMacro=12
 
However, I can never remember the object type numbers so instead I'd use: acTable, acForm etc

If you use the proper type you don't need to remember, it'll give you intellisense to pick from.

Pick a constant, hit f2, search for a member and use the class.

Function IsOpen(strname As String, strtype As AcObjectType) As Boolean
 
If you use the proper type you don't need to remember, it'll give you intellisense to pick from.

Pick a constant, hit f2, search for a member and use the class.

I realise that - in fact that's how I got the list in my previous post:
acServerView=7; acDiagram=8 ; acStoredProcedure=9;acFunction=10; acTableDataMacro=12

My point is that in my view its not worth the time doing the lookup as I would have typed acForm or whatever in a fraction of that time

Function IsOpen(strname As String, strtype As AcObjectType) As Boolean

However I agree using strType As acObjectType does make this better
 
I only made one point. Not only is better for you it's better for anybody else that might read/use your code in the future.
 

Users who are viewing this thread

Back
Top Bottom