Results 1 to 13 of 13
  1. #1
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126

    Opening form B from form A does NOT close form A - why ?

    I have a form showing a textbox with different names of persons - 2-clicking on one of the names shold:

    1) CLOSE THE CURRENT FORM
    2) Open another form showing that person from the 2-click

    I use this code in the 2-click event of the textbox

    Dim f As Form, rst As DAO.Recordset
    DoCmd.Close acForm, Me.Name
    If CurrentProject.AllForms("PERSON Vedligehold af").IsLoaded Then
    Set f = Forms("PERSON Vedligehold af")
    Set rst = f.Recordset.Clone
    rst.FindFirst "[PersonID] = " & Me.PersonID
    f.Bookmark = rst.Bookmark
    Set f = Nothing
    Set rst = Nothing
    Else
    DoCmd.OpenForm "PERSON Vedligehold af", , , "PersonID = " & PersonID
    End If

    And it works nicely EXCEPT the first form is still open !



    What do I do wrong ?

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Why do you want to close the form? It adds complexity to your process. Otherwise, close the form as the last thing, not the first.

  3. #3
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    You need to close the first form in the OnOpen event of the 2nd form.

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    it doesnt have to.

  5. #5
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    It doesn't work - the form that SHOULD BE close remains open - argh !

  6. #6
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I tried something similar to what you have and can't replicate the issue even without moving the close method to the end of the code. Putting it early is a mistake AFAIC, because it will cause execution to branch off the current procedure into the close event for the very form you want to continue running code on. I figure it's cancelling itself, but that doesn't make sense. But "it doesn't work" doesn't help much. What "doesn't work"? Putting the call to close in the second form? Maybe post a zipped copy of your db?

  7. #7
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    702
    In my experience with Access since 2.0, if a form opens a recordset then it must remain open until the recordset is closed.

    I use some very similar code but I have it in a standard VBA Module. Not in code behind the form.

    Here are the basic steps I use:

    1) hide the current form
    2) call the function to open the next form
    3) close the form


    This may help:

    MS Access – VBA – Requery a Form While Remaining on the Same Record

  8. #8
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    Yeah, I too have experienced that hide/show is a better method.

    My situation is, that I want to open forms in 3 levels on top of the main menu and then go back and forth between these levels.

    Hide/show seem to be a much easier and better method to use then.

    I'm now working on some generalized method for this hiding/showing different forms.

  9. #9
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    702
    Quote Originally Posted by ksor View Post
    My situation is, that I want to open forms in 3 levels on top of the main menu and then go back and forth between these levels.

    Hide/show seem to be a much easier and better method to use then.
    By hiding/showing a form, you also save the overhead of load time.

    Another approach I use is a tab control. You navigate between the pages of the tab control. Check out this example: Wizard Form template

  10. #10
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,471
    Couple ideas. Does the 2nd form have to remain open? Once you select a name and 2nd form opens, can they do whatever they need and then close that 2nd form(model/popup)? Or if not, can you add 2nd form as a subform to your first form? Just seems overly complicated to have to know which forms are open and such.

    And an alternate way, Open Form 2 (or if it is open already) and using code on Form2 and the PersonID field from Form1, set the RecordSource as needed in Form2, then close Form 1. So do it all in Form2, not Form1. Only thing in Form1 is Opening Form2.

  11. #11
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    Thx to all of you ... I'm working on the project ;-))

  12. #12
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    I now made a solution where I from the MAIN MENU with buttens can OPEN my forms and then they STAY open but will be hidden when "close".

    It works very nice and have the side effect that data in the forms stay the same while using other forms - that's a great increase in functionality in my case.

    In some remote corner of the main menu I have a counter showing the number of opened (and now hidden) forms - just for checking - nice !

    I can recommend that "Open and then hide" idea - works very nice !

  13. #13
    HiTechCoach's Avatar
    HiTechCoach is offline MS MVP - Access Expert
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2010
    Location
    Oklahoma, USA
    Posts
    702
    Glad to hear that it worked for you. It is a trick I have been using for over 15 years in Access.

    Thanks for paying it forward by sharing your solution.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 03-20-2017, 07:22 PM
  2. Close the Switchboard when opening a form
    By Silvera in forum Forms
    Replies: 3
    Last Post: 08-11-2015, 03:02 PM
  3. Replies: 4
    Last Post: 10-09-2014, 07:09 PM
  4. Replies: 2
    Last Post: 05-11-2012, 11:52 AM
  5. Close Form A After Opening Form B
    By M4ttyC88 in forum Programming
    Replies: 6
    Last Post: 01-18-2012, 02:06 AM

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