Results 1 to 19 of 19

Thread: Docmd.close

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2

    Docmd.close

    Using DoCmd.Close to close a form other than the active form does not work e.g.

    Active form: frmUserForm1
    Form to be closed: frmUserForm2

    Code: DoCmd.Close, acForm,"frmUserForm2"

    Unfortunately, frmUserForm2 just sits there looking at me. Why will it not close?

  2. #2
    New Member
    Join Date
    Aug 2003
    Posts
    5
    The only thing that I could see that may cause the command to fail is the comma (,) right after the DoCmd.Close...

    VB Code:
    1. DoCmd.Close[COLOR=red],[/COLOR] acForm,"frmUserForm2"

    Try it this way and see what happens:

    VB Code:
    1. DoCmd.Close acForm, "frmUserForm2"

    Hope this helps!

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Posts
    2
    Thanks for that draybu. Unfortunately, the way you have suggested is actually the way I have the code written; I made a mistake when typing the code into my post. So, the code you have suggested doesn't work for me. Any other ideas anyone....please!!!!

  4. #4
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    I'll sort it...

    VB Code:
    1. For Each frm In Forms
    2.   If frm.name <> "frmUserForm1"
    3. Then
    4.      DoCmd.Close acForm, frm.name
    5.      
    6.       End If
    7.       Next frm

    or

    VB Code:
    1. For Each frm In Forms
    2.   If frm.name = "frmUserForm2"
    3. Then
    4. DoCmd.Close acForm, frm.name
    5.      
    6.       End If
    7.  
    8. Next frm
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  5. #5
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    Try

    formname1.hide
    this hides the forms and make them load faster when you use them again

  6. #6
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    If you hide forms with data in them which is meant to go into a database, it won't enter UNTIL the form is closed, meaning if you need the data on another form, you're screwed. Also hiding forms means they still use memory, leading to slower performance.
    Stick with closing them.
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  7. #7
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    A common cause of form fails to close is mis-spelled form names... for me at any rate.

    VB Code:
    1. Public Function closeform(NameOfForm, OpenThisForm As Variant)
    2. 'This function closes the current form and loads another
    3.  
    4. 'You can define "code" words and add specialist actions in the case select
    5. 'other than that the Form named to be opened (if it is a real form name)
    6. 'will result in that form opened.
    7. Dim CF_Result As Boolean
    8.  
    9. CF_Result = False
    10. On Error GoTo Err
    11.  
    12.   Select Case OpenThisForm    'select the open via nickname
    13.    
    14.     Case "CJE"
    15.         DoCmd.OpenForm "Plus Clients and estimates"
    16.         If NameOfForm <> "Plus Clients and estimates" Then CF_Result = True
    17.        
    18.     Case "Y"                              'Add popups
    19.         DoCmd.OpenForm "Start-Up"
    20.        
    21.     Case "CAL"                         'Add other one off special actions
    22.         DoCmd.OpenReport "Calender01"
    23.    
    24.     Case Else               'select to open via true name (better)
    25.         If NameOfForm <> OpenThisForm Then
    26.           DoCmd.OpenForm OpenThisForm
    27.           CF_Result = True
    28.         Else
    29.           CF_Result = False
    30.         End If
    31.   End Select
    32.    
    33. GoTo No_Error   'skip this old fasioned "goto" jumping
    34. err2:
    35. CF_Result = False
    36. MsgBox "Error SB42.1: form can not be closed. " & Err.Number & " - " & Err.Description
    37. GoTo Still_no_error 'get out of this and end the function
    38.  
    39. Err:
    40.   MsgBox "Failed to find " & OpenThisForm
    41.   CF_Result = False
    42.  
    43. No_Error:
    44.   On Error GoTo err2
    45.   If CF_Result <> False Then DoCmd.Close acForm, NameOfForm
    46.  
    47. Still_no_error:
    48.   closeform = CF_Result
    49. End Function

    Recently I developed a block of code for dealing with the old chestnut of close me and open him in the world of form control. This is quite well tested and I hope might help.

    Feel free to use the code (The case select allows you to set up nick-names for long winded form names).

    Another thought is: what is you non-closing form doing? By that I mean when you try to close it is it being opened still and are you getting any error messages? if you've used on error resume next then REM it out and read the error reports.

    Are you useing many ActiveX componants on the forms?

    If I think of any other causes I'll post them here as well.
    Last edited by Matt_T_hat; Sep 18th, 2003 at 04:02 AM.
    ?
    'What's this bit for anyway?
    For Jono

  8. #8
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    Bloody hell Mat. That's some extreme code for closing a form....
    Impressive, but a little overkill don't you think?
    :S
    I had the same problem in Access VBA I found closing a form from another using docmd.close impossible, hence the code I wrote earlier.......
    If anyone can get the docmd.close command to work JUST on it's on, I'd be impressed....
    then again, I'm impressed at juggling.
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  9. #9
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    I have no problem with this.. I just created two forms.. form5 and form6.. the command button on 5 closes 6 with a click.. there was no problem..

    VB Code:
    1. Private Sub Command0_Click()
    2. DoCmd.Close acForm, "form6"
    3. End Sub

  10. #10
    New Member
    Join Date
    Sep 2003
    Posts
    4
    I really don't know what the problem is

    I tried it my self and it works with this code

    DoCmd.Close acForm, "frmUserform2"

    If you make a button and you make those 2 forms it just has 2 work.

  11. #11
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    maybe you're using a different VBA version, cause it sure as hell doesnt work here.
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  12. #12
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    I'm using Access 97 sr-2

  13. #13
    New Member
    Join Date
    Sep 2003
    Posts
    4
    I'm using Office XP

  14. #14
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    I'm using 2k.
    It's just a bug, like how you can't select .text in your code and you have to use .value
    Or is that 2k only too....
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  15. #15
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Bazzlad
    I'm using 2k.
    It's just a bug, like how you can't select .text in your code and you have to use .value
    Or is that 2k only too....
    So is this thread resolved? Is that the final answer? Sorry it's a bug... does MacroSoft not have the umption to release a fix?... Or does anyone know of a patch activeX or otherwise? Or perhaps there is a definitive way to do it? I know my "overkill" code does the job and can cope with seriouse abuse without crashing anything. Perhaps we need to work on a fairly deffinitive solution.

    One day some newbie is going to search VBforums.com and find this thread only to go away without an answer to post the question again and again...

    I think we should perhaps put our heads togeather and find a set of answers.
    ?
    'What's this bit for anyway?
    For Jono

  16. #16
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Bazzlad
    Bloody hell Mat. That's some extreme code for closing a form....
    Impressive, but a little overkill don't you think?
    :S
    I had the same problem in Access VBA I found closing a form from another using docmd.close impossible, hence the code I wrote earlier.......
    If anyone can get the docmd.close command to work JUST on it's on, I'd be impressed....
    then again, I'm impressed at juggling.
    For the record the code given sits in a module to be called by code when needed. I wrote it for openiong and closeing forms in access 2000 as I got fed up with getting the docmd.open and the docmd.close in the wrong order. Plus this gives less "ficker" as the form that is closed is closed whent the other form is covering it. The result was a happy pro looking transition.

    Over kill - maybe, usefull - for sure!

    perhaps a stripped down version of the code posted would be the answer. What do you think?
    ?
    'What's this bit for anyway?
    For Jono

  17. #17
    Addicted Member Bazzlad's Avatar
    Join Date
    Jun 2003
    Posts
    227
    In fairness I very much like your code, it's well written and smart, the over kill comment was made because it appears we have to write a page of code to replace this:
    "Docmd.close"
    Lol.
    I think between our two solutions no1 should have a problem....
    there are two ways of shutting down a form here, so it's got to be solved hasn't it?

    Cheers
    *And you'll see, Everything you stand for is fake*
    http://www.rhesusrock.com

  18. #18
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    Originally posted by Bazzlad
    I think between our two solutions no1 should have a problem....
    there are two ways of shutting down a form here, so it's got to be solved hasn't it?
    I think perhaps you could be right.
    ?
    'What's this bit for anyway?
    For Jono

  19. #19
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83
    Originally posted by Bazzlad
    If you hide forms with data in them which is meant to go into a database, it won't enter UNTIL the form is closed, meaning if you need the data on another form, you're screwed.
    You could use the access command constant (SaveRecord) to remedy this situation.

    Code:
    DoCmd.RunCommand acCmdSaveRecord
    Best to check if the Form is Dirty first otherwise there's no point in running it.

    Code:
    If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord

    As for the rest of the problem.

    Have you compacted and repaired the database on the chance it may just be one of Access's niggles?

    Have you exported all objects into a new database?

    The only other thing I can think of is if there is any condition in the Form's Unload event whereby you are setting Cancel to True.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width