Results 1 to 11 of 11
  1. #1
    vikasbhandari2 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    8

    Form not getting closed at first time

    Hello All,



    I have a form, in which I have a subform showing some data and a Button with following code:

    Code:
    Private Sub Command14_Click()
        ModContext.Status = True
        Me.SetFocus
        DoCmd.Close acForm, Me.Name
    End Sub
    The problem is, whenever I change any record in the Sub Form, and click on the button, it is supposed to close the form and return the control to the function which called this form. But it never closes at the first click. I put a debug and it is being called but just not closing for the first time. Example, I selected a record in sub form and clicked button, the code mentioned above is being called every time but it doesn't close. Now I click the button again, and the same code is called again but this time the form is closed. Ahhh, I am getting mad

    Please Help!
    Vikas B

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    The button is on the main form? What is ModContext? Why the SetFocus?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    vikasbhandari2 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    8
    ModContext is just a Module and the Status is a boolean variable which suggests if I had clicked ok or Cancel. I tried to use SetFocus when it was not closing. Whether I include SetFocus or not, it is not closing the form.

    Please Help!
    Vikas B

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    I don't understand the ModContext and Status, never used this. A code module has a Status property? Why do you need this?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Try the following VBA code and see if either of the MsgBox's pop up:
    Code:
    Private Sub Command14_Click()
        MsgBox "Before close!"
    
        DoCmd.Close
    
        MsgBox "After close!"
    End Sub

    • If neither of the MsgBox's appear, then it is most likely interference from an Event on the Form Control you were in IMMEDIATELY BEFORE clicking the button. This usually happens with "On Key Press" or "On Key Down" Events in my experience. You'll need to change (or remove) the code on the other Form Control before the button will work on the first click.
    • If only the "Before close!" MsgBox appears, then most likely there is some sort of error condition being triggered that's canceling the rest of the Sub.
    • If both of the MsgBox's appear, then there is probably a problem with the Form itself. I would suggest creating a new Form and copying all the Controls and Objects onto it then trying again.

  6. #6
    vikasbhandari2 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    8
    @June7, it doesn't matter if I include the
    Status
    in the code or not. I have removed it and it still not closing it.

    @Rawb, thanks for the comment and it was a nice first attempt. However, as I said in previous post as well, the whole code, is called (I confirmed it in debug mode by putting a break point and then pressing f8.) but the form is not closed. But If I click again, and then the same code is being called but it is closing the form second time I call it.

    So in short, the following line is only closing the form if I call it second time.

    Code:
    DoCmd.Close acForm, Me.Name
    I cannot create new form because it has been happening with almost every form I have for Search. I have like 15+ plus forms and I cannot go back and convert new

    Thanks.
    Vikas B

  7. #7
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    OK, I've done a little Google-Fu and it appears that lots of people are actually having this problem. While none of them seem to know WHY it's happening, many seem to find that one of the following will work:

    Solution 1: Remove all the Arguments
    Code:
      DoCmd.Close
    Solution 2: Get the Form name from the Screen.ActiveForm.Name Instead of Me.Name
    Code:
      DoCmd.Close acForm, Screen.ActiveForm.Name

  8. #8
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Code:
      DoCmd.Close acForm, "frmMain"
      DoCmd.OpenForm "frmCred"
    I had to include the form name.. seems to work without issue.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    Never had issue with Me.Name. Odd.

    What is the code that opens the form/subform? Are you using the OpenForm window mode acDialog parameter?

    If you want to provide project for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    vikasbhandari2 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2012
    Posts
    8
    Damn it!

    I am not sure why Access behaves such a way. If anyone has any explanation, please do let me know.

    it worked fine when I used

    "frmSearchStates" instead of formObject.Name. Working fine right now.

    Thanks so much for your help guys. FYI, I am an Excel Expert so still learning the Access way of coding, so bear with me in coming future as well

    Thanks,
    Vikas B

  11. #11
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    I had the same problem with .me awhile back.. someone told me to put the "formname" in... and its worked ever since... I was unable to get an answer why....

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

Similar Threads

  1. Replies: 1
    Last Post: 03-30-2012, 11:57 PM
  2. A2010 compacting every time the DB is closed.
    By irish634 in forum Access
    Replies: 5
    Last Post: 03-02-2012, 04:28 PM
  3. Replies: 4
    Last Post: 08-17-2011, 05:30 AM
  4. Print a Closed case
    By lyndon.pace in forum Reports
    Replies: 1
    Last Post: 04-11-2011, 11:35 AM
  5. Get column headings from closed Workbook
    By Deutz in forum Access
    Replies: 1
    Last Post: 12-10-2010, 09: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