Results 1 to 10 of 10
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176

    Question VBA code after DoCmd.Close acForm

    If have a need to reopen a form with a new dataset and then adjust items on the form when a certain button is clicked.



    The code in the button event handler is :-

    DoCmd.Close acForm, "Visual"
    DoCmd.OpenForm "Visual", , , strFilter

    This seems to work correctly, although probably not best practice.

    My question is why does this work. You might have thought that after the Close line that is it, and the OpenForm line can't be executed as the form is closed.

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    But the Form was closed before you opened it the first time, so the close is one event and a subsequent open is a separate event.
    What exactly is the confusing part?

  3. #3
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by orange View Post
    But the Form was closed before you opened it the first time, so the close is one event and a subsequent open is a separate event.
    What exactly is the confusing part?
    I forgot to mention that the code is within the VBA of the Visual form. So, I would have thought that when the Visual form is closed (from within itself), all VBA associated with the form is also closed and can't run anymore. Maybe that is not the case.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    so the close is one event and a subsequent open is a separate event.
    That would not be my interpretation. The event is the button click. The commands are to close the form and reopen it and while those are events, they are not written as such. I would have thought this would not work since no further code ought to run when the form is closed.

    I agree with Miles R; it is not good practice. Better to re-assign the form recordsource and requery, or since you seem to have a filter property value at that point, just apply it. That will automatically requery the form.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by Micron View Post
    That would not be my interpretation. The event is the button click. The commands are to close the form and reopen it and while those are events, they are not written as such. I would have thought this would not work since no further code ought to run when the form is closed.

    I agree with Miles R; it is not good practice. Better to re-assign the form record-source and re-query, or since you seem to have a filter property value at that point, just apply it. That will automatically re-query the form.
    Thanks for your replies everyone. Even though it probably should not work it does perfectly. Maybe it is a timing issue and before the form has closed itself, it has already started the second open.
    I did try re-querying with the new filter, which also worked (a bit more slowly). However due to an Access 2007 bug there was a problem with this. The form is a single form with a vertical scroll bar. If I am at the bottom of the scroll bar when I click the button to reload, the Form Header is slightly corrupted - looks like the Form Header bottom edge has been overwritten with what was on the previous page. This won't go away until the form is closed and re-opened from scratch. Very unsightly.
    So I am stuck with using an unorthodox method that does work cleanly.

    I was just curious if anyone knew why this works.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    I agree
    -it's probably not good practice,
    -I probably should have thought more about calling the Form close as an Event vs a button click etc.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    If I am at the bottom of the scroll bar when I click the button to reload, the Form Header is slightly corrupted
    If you want to trouble shoot that, post a compacted and zipped db copy or maybe pics. Forms do have a page header IIRC but I've never used them and I've heard they can be wonky, and I've never treated a form as a multi page window or document for that reason. Perhaps you are using a page header. You could also try Me.Repaint. Since it's a single form, I think there's no need to worry about losing your spot like you would on a datasheet or continuous view.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Quote Originally Posted by Micron View Post
    If you want to trouble shoot that, post a compacted and zipped db copy or maybe pics. Forms do have a page header IIRC but I've never used them and I've heard they can be wonky, and I've never treated a form as a multi page window or document for that reason. Perhaps you are using a page header. You could also try Me.Repaint. Since it's a single form, I think there's no need to worry about losing your spot like you would on a datasheet or continuous view.
    Thanks for your reply. I tried Me.Repaint but that does not clear the error. It's not worth trying to fix this as I have a work around, so have abandoned the version of code that does a re-query. Still not had an answer as to why the form open works after the form that has issued the request has supposedly been closed.

    Just for your amusement, I have attached a picture showing the error - very minor but unacceptable in a working application. The top image is how the top of the form should look with the black line being the border of the Form Header (blue area). Below the black line is the Form Detail. On the lower image, the header border has gone and is replaced with part of the Form Detail - clearly a bug as the Form Header should never be overwritten by the Form Detail, but not worth worrying about.
    Click image for larger version. 

Name:	Bug.jpg 
Views:	17 
Size:	9.1 KB 
ID:	41521

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,991
    Coming back to the original question, all the code in the button click event will run even if the form has closed.
    I have used similar code on numerous occasions to reload a form and see no reason why it should be considered as bad practice.

    I haven't looked at your later 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

  10. #10
    Miles R is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Mar 2019
    Posts
    176
    Thanks islagdogs. I guess that wraps things up. Looks like Access 2007 does not fully close the forms VBA whilst it is still executing code in an event. I like to see under the bonnet of Access to confirm that, just out of curiosity.
    Don't worry about the other issue - its once of those flaky Access things like flickering forms that you can't get rid of sometimes.

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

Similar Threads

  1. DoCmd.Close not working as expected
    By GraeagleBill in forum Programming
    Replies: 7
    Last Post: 12-07-2016, 12:31 AM
  2. Where do I put DoCmd.Close acForm, "Email_AORB"?
    By Thompyt in forum Programming
    Replies: 3
    Last Post: 11-12-2014, 05:17 PM
  3. DoCmd.Close Not Working
    By kdbailey in forum Access
    Replies: 6
    Last Post: 12-11-2013, 07:35 PM
  4. DoCmd.Close OpenArgs NOT WORKING
    By clchris_80 in forum Access
    Replies: 1
    Last Post: 01-19-2013, 09:07 PM
  5. Replies: 19
    Last Post: 12-02-2012, 07:01 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