Results 1 to 7 of 7

Thread: Generic Close

  1. #1
    Join Date
    Aug 2006
    Posts
    126

    Unanswered: Generic Close

    Quick question. From a dashboard, I open one of a number of forms. On each form is a "Return to Main Menu" button. The button runs a macro which is simply a CloseWindow (ObjectType=Form). I want to use one macro regardless of which form is open. In earlier versions of Access I thought I could get away with leaving the objectname blank, but I'm now in 2007/10 and that doesn't seem to work. I tried setting the ObjectName "=[screen.activeform.name]" but that doesn't work. What is the correct/best way to create a generic close?
    Pete
    PGT

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Use a VBA procedure instead of a macro:
    Code:
    Sub CloseForm(Byval FomName As String)
    
        Docmd.Close acForm, FormName
    
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Aug 2006
    Posts
    126
    Thanks Sinndho,
    I knew I could close the form by passing the explicit FormName (either as Macro or a VBA). I was trying to avoid having to code each button with the name of the form it's on. Essentially I was trying to do the same as clicking the "X" in the upper-right corner of the form. I know it's probably better to use the explicit method, but in this case it would cleaner to be completely generic. I thought I could just enumerate the FormName with "screen.activeform.name" or something similar, but that syntax doesn't work.
    Pete
    PGT

  4. #4
    Join Date
    Apr 2004
    Location
    metro Detroit
    Posts
    634
    the arguments are optional

    docmd.close will close the active form or report

  5. #5
    Join Date
    Aug 2006
    Posts
    126
    Thanks Rogue,
    I've got a module
    Public Function CloseCurrentForm()
    DoCmd.Close acForm, Me.Name, acSaveNo
    End Function
    and I could drop everything except the DoCmd.Close.
    But now my problem is that when I go to the button OnClick event, I can't find the module or event. Double clicking the [Event Procedure] option merely creates a new button specific function.
    Pete
    PGT

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    When the form is design mode, select the button, then in the Property windows, replace "[Event Procedure]" by "=CloseCurrentForm()" (without the quotes) for the OnClick event.
    Have a nice day!

  7. #7
    Join Date
    Aug 2006
    Posts
    126
    Sinndho, my problem was forgetting the "=".

    For everyone, access didn't like this use of Me.Name.
    I tried DoCmd.Close acForm,, acSaveNo and it didn't like that either.
    DoCmd.Close by itself did work. So I was officially done.

    But just out of curiosity, I also went back to the original non-working macro's that used CloseWindow with no arguments; deleted them and then recreated them; and voila they now work. Must have been something in the moving of the db from 2003 to 2010.
    Thanks to all.
    PGT

Tags for this Thread

Posting Permissions

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