Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176

    Two instances of same form showing on screen


    Prior to opening the form "Design Form", I am referencing some of the elements of "Design Form", so the Load event of the form is triggered (I have set a Me.Visible = True line of code in the Load event to make the form visible.

    I then open the form with DoCmd.OpenForm "Design Form", with all the proper parameters and filters etc.

    However, I now see two copies of the "Design Form" on the screen (with different hWnd). If I close the one with proper data, they both close, but closing the first does not close the second.

    Can't see how there can be two instances of the same form visible (I am not using the New keyword).
    Must be missing something obvious.

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Hello again Miles

    It is certainly possible to have multiple instances of the same form open.
    However, I don't think you haven't provided enough information for anyone to give a definitive explanation of why its happening in your case.

    At the very least, please provide the relevant code.
    Better still, please supply a cut down version of your app that clearly shows the issue but with any confidential data removed
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I think the reason why is there - in either the load or open event of a form, if you're going to have DoCmd.OpenForm "Design Form" you'll end up with a copy. If you close the "parent" form, all other copies (instances) will close.
    Also agree that there isn't enough info to figure out what's needed. Why open a second instance of the same form and then do data entry or edit in one of them?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by Micron View Post
    I think the reason why is there - in either the load or open event of a form, if you're going to have DoCmd.OpenForm "Design Form" you'll end up with a copy. If you close the "parent" form, all other copies (instances) will close.
    Also agree that there isn't enough info to figure out what's needed. Why open a second instance of the same form and then do data entry or edit in one of them?
    Thanks for your replies.
    I was missing the knowledge that DoCmd.OpenForm could actually create another instance of a form. I just assumed it would reopen the same form.
    The reason I need this is because I am actually creating multiple instances of the same form later using the New command (which I know does create multiple instances of the same form). I have to interrogate the generic form instance first so that I can alter some things on the newly created copies.

    Why this issue arose is because when I later close the (lets say 4 copies) of the form created with New [Form_Design Form], by calling
    DoCmd.Close acForm, "Design Form" 4 times, I get one form still visible and have to actually call DoCmd.Close acForm, "Design Form" 5 times.

    This is not an issue of course, but I like to understand things rather than just do a bodge job that works.

    For Colin, I created a simple access database showing what I meant about two forms appearing.
    The code is simple - two forms, Main and F1 :-

    Main
    Private Sub Form_Load()
    Dim x As Integer
    DoCmd.Maximize
    first = True
    x = [Form_F1].WindowHeight
    first = False
    DoCmd.OpenForm "F1"
    End Sub
    F1
    Private Sub Form_Load()
    Me.Visible = True
    Me.Caption = hWnd
    If Not first Then
    Me.Move 3000, 0, , 2500
    End If
    End Sub

    Module 1
    Public first As Boolean


    Result on opening Main form is :-
    Click image for larger version. 

Name:	Capture.JPG 
Views:	17 
Size:	16.8 KB 
ID:	45282

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    You've lost me. I don't see what that code for opening 2 different forms has to do with your original question about opening the same form multiple times.
    EDIT - I get it, I think. You were not trying to have multiple instances of a form; you were describing a result.
    - end of edit

    Boolean default is False so I'd use something else (integer, long, text value) or reverse the logic because if you simply open the 2nd form and the first is not open, the value is False which to me, implies that the main form is open when it isn't.
    Nor is it necessary to set it True then do something and then set it back to what it would already be at the beginning.
    Nor should it be necessary to set a different form opening to be visible.
    I just assumed it would reopen the same form.
    Why would you want to try to open a form that is already open in form view? You'd only do that if it was in some other view IMO.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Sorry but I don't get it at all. Is that two forms Main & Design and what about form F1?
    Can you post your example app explaining exactly what steps are needed to replicate your original issue
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    I don't think you are getting the point here.
    Forget about all the boolean stuff - that was just bodged together to move the second form into a different place than the first so you can see them both.

    The point was that in my actual application, the instance of the form opened by getting its WindowHeight was invisible, so I could not understand why closing 4 visible forms that I had created, only closed 3 of them. There was another invisible version lurking in the background that I did not know about until I investigated what was going on.

    I think the issue here for Beginners to Access is that they may not realise that doing something like reading a forms properties e.g. WindowHeight actually triggers the Open Event of that form, although the form will be invisible at that stage.

  8. #8
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Colin, sorry made a mistake, Design Form is the form name on my actual application.
    On the example, the forms are just Main and F1. Code for F1 is what I put under Design Form.

    As you can see, the code is very simple, but sorry to confuse you.
    The first instance of F1 is opened by x = [Form_F1].WindowHeight
    and the second by
    DoCmd.OpenForm "F1"

    This was the bit that puzzled me - I would have thought that the
    DoCmd.OpenForm "F1" just re-opened the same F1

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Yes - you completely confused me.
    I had to create a copy of your example database to understand what on earth you were talking about - it would have been easier if you had uploaded yours.
    I minimized the main form so I could see the other form(s)!

    I'm still not clear what the purpose is of determining the value x as you don't use it.
    However you do need to close the instance that you have opened (invisibly)
    To only end up with one copy of form F1 open, change the main form code to:

    Code:
    Private Sub Form_Load()Dim x As Integer
    DoCmd.Minimize
    First = True
    x = Form_F1.WindowHeight
    DoCmd.Close acForm, "F1"
    First = False
    DoCmd.OpenForm "F1"
    End Sub
    OR to this ....
    Code:
    Private Sub Form_Load()Dim x As Integer
    DoCmd.Minimize
    First = True
    DoCmd.OpenForm "F1", , , , , acHidden
    x = Forms!F1.WindowHeight
    DoCmd.Close acForm, "F1"
    First = False
    DoCmd.OpenForm "F1"
    End Sub
    Next time you have a problem with forms, can you please explain the context more clearly and upload an example database. Thanks
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  10. #10
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Colin,

    Thanks for your input and patience. In the example database, I don't use x - it was just to trigger the Open event for demonstration purposes. In the real application I do make use of the WindowHeight and other measurements so I can adjust the dimensions and text box sizes in each version of Design Form I create (could have used hard coded values, but that would be a bit naff).

    Yes, I will now close the version of the form that is triggered, before I create new copies.

    What I am left with though is the underlying mechanics of how and why Access creates copies of forms. I can understand that using the NEW keyword, creates a new copy of a form that has been created in Design Mode. What is not clear is why it is creating a new copy when you use Docmd.Open
    I would have though that it just opens the form that has already been created in Design Mode, not create an additional one.
    So, as you can see from the simple code, there is only one Docmd.Open so a novice might expect there to be only one version of the form - namely the one that they actually created in Design Mode. Would like to know what is going on under the bonnet here.

    Thanks anyway,

    Miles

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Or you can just do something like this in your main form ...

    Code:
    Private Sub Form_Load()'Dim x As Integer
    DoCmd.Minimize
    DoCmd.OpenForm "F1"
    Debug.Print Forms!F1.WindowHeight
    Debug.Print Forms!F1.Hwnd
    
    
    End Sub
    Only one instance of form F1 is ever opened
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by isladogs View Post
    Or you can just do something like this in your main form ...

    Code:
    Private Sub Form_Load()'Dim x As Integer
    DoCmd.Minimize
    DoCmd.OpenForm "F1"
    Debug.Print Forms!F1.WindowHeight
    Debug.Print Forms!F1.Hwnd
    
    
    End Sub
    Only one instance of form F1 is ever opened
    Yes, but that's no good for my purposes. I need to know the WindowHeight BEFORE I open the form, so I can adjust the window height and other things on the form (actually in the main application, I don't use DoCmd.OpenForm, I create new copies using the New keyword).
    Perhaps its not clear why I would want to do that, but I do and it works. In my application I sometimes have 8 instances of the Design Form open, all with different heights and different detail heights etc.

    I am not sure anyone reading this thread knows why DoCMD.OpenForm is actually creating a copy of the form rather than just opening the existing one. Must be a feature, probably introduced with Access 1997 when multiple instances of the same form were allowed.

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    You're right. Its not clear why you would want to have multiple instances of the same form open at once, all with different heights etc.
    Its certainly not a typical way of working in Access and something that in 20+ years I can't ever remember needing to do.
    But its your app and, if it works for you, so be it.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by isladogs View Post
    You're right. Its not clear why you would want to have multiple instances of the same form open at once, all with different heights etc.
    Its certainly not a typical way of working in Access and something that in 20+ years I can't ever remember needing to do.
    But its your app and, if it works for you, so be it.
    Hum,
    I'm slightly surprised at your comment. I can think of many reasons to have multiple instances of the same form open at the same time. In my case, I have a collection of items and the Design Forms show details about the items. I often want to compare several items side by side, so create multiple instances of the same form, each with details specific to the item (hence different sizes, text etc etc). Without the ability to create multiple instances of the same form this would not be possible (unless I created numerous versions of the form in design mode with different names (e.g. D1, D2, D3 etc).

    Another example is showing Personnel records side by side, or pictures of different things. Many other examples.

    However, we all have our own way of doing things and I like to experiment with different ideas and options.

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Opening multiple instances of the same form certainly has its uses
    My actual comment was:
    Its not clear why you would want to have multiple instances of the same form open at once, all with different heights etc.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 01-31-2018, 01:43 PM
  2. Login in form - not showing on screen
    By Sheba in forum Forms
    Replies: 2
    Last Post: 10-08-2014, 09:57 AM
  3. Replies: 2
    Last Post: 08-19-2013, 09:40 AM
  4. Replies: 3
    Last Post: 10-17-2012, 03:16 PM
  5. Replies: 5
    Last Post: 07-31-2012, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums