ACC2: How to Close Datasheet Form Without Saving Design Changes

ID: Q173517


The information in this article applies to:


SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

When you open a form in datasheet view in Microsoft Access version 2.0, and then make any modifications to the layout of the form, such as increasing the width of a column, Microsoft Access prompts you to save the changes to the form when you close it.

If you do not have design rights to the datasheet form, Microsoft Access cannot save the changes you made while viewing the form. When you try to save the form, you receive the following error message:

You don't have permissions to modify form '<name of form>'


MORE INFORMATION

To prevent Microsoft Access from prompting you to save the form, use one of the following methods. The first method involves embedding your datasheet form as a subform within an unbound main form. The second method uses application programming interface (API) function calls in an Access Basic procedure.

Method 1

  1. Create a new blank form and set the following properties:


  2. 
       Form: frmTest
       ----------------------------
       Caption: &lt;same as subform's&gt;
       ControlSource: &lt;none&gt;
       Default View: Single Form
       Scroll Bars: Neither
       Record Selectors: No
       Navigation Buttons: No 
  3. Add your datasheet form as a subform to the new blank form.


  4. Resize and reposition the subform to remove all the unused space in the master form.


  5. Save the master form. When you open the master form, it looks like your datasheet form; however, changing the column size or font doesn't affect the datasheet form's design. Therefore, closing the master form doesn't prompt you to save any changes.


Method 2

Advanced: Requires expert coding, interoperability, and multi-user skills.

This method assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Access Basic, please refer to the "Building Applications" manual.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code.

You may have some Microsoft Windows API functions defined in an existing Microsoft Access library; therefore, your declarations may be duplicates. If you receive a duplicate procedure name error message, remove or comment out the declarations statement in your code.

  1. Create a new form to prompt the user. This is necessary to prevent the existing datasheet form from calling the API function.


  2. 
    Form: frmCloseDialog
          -----------------------------
          Name: frmCloseDialog
          Caption: Close Datasheet form
          Recordsource: &lt;none&gt;
          Default View: Single Form
          Views Allowed: Form
          Scroll Bars: Neither
          Record Selector: No
          Navigation Buttons: No
          Border Style: Dialog
    
          Label:
             Name: lblPrompt
             Caption: Are you sure you want to close the form?
          Command Button:
             Name: cmdCloseForm
             Caption: OK
             On Click: [Event Procedure]
          Command Button:
             Name: cmdCancel
             Caption: Cancel
             On Click: [Event Procedure] 
  3. On the View menu, click Code to display the form's module. Type the following lines of code within the form's module:


  4. 
          Option Explicit
    
          Declare Function KillForm Lib "User" _
            Alias "DestroyWindow" (ByVal intWinHandle As Integer) As Integer
    
          Sub cmdCancel_Click ()
             On Error GoTo ErrCmdCancel
             DoCmd Close
    
          ExitCmdCancel:
             Exit Sub
    
          ErrCmdCancel:
             MsgBox Error$
             Resume ExitCmdCancel
          End Sub
    
          Sub cmdCloseForm_Click ()
             Dim intResult As Integer, intHnd As Integer
             On Error GoTo errCloseForm
    
             ' Set the focus back to the datasheet form prior to saving the
             ' record.
    
             Forms![frmDatasheet].SetFocus
             DoCmd DoMenuItem A_FORMBAR, A_FILE, A_SAVERECORD, ,A_MENU_VER20
    
             ' Grab the window handle of the datasheet form, and
             ' Invoke the DestroyWindow API call to close the datasheet form.
    
             intHnd = forms!frmDatasheet.hwnd
             intResult = KillForm(intHnd)
    
             ' Close the cancel dialog form frmCloseDialog.
             DoCmd Close A_FORM, Me.Name
          ExitCloseForm:
             Exit Sub
          ErrCloseForm:                 ' In case the datasheet form wasn't
                                        ' open.
             MsgBox Error$
             Resume ExitCloseForm
          End Sub 
  5. Close and save the form created in step 3 as frmCloseDialog.


  6. Open the datasheet form (frmDatasheet) in Design view and view its properties.


  7. Set the form's OnUnload property to the following event procedure:


  8. 
          Sub Form_Unload (Cancel As Integer)
             ' Cancel the unload event of the Datasheet form, and open
             ' the dialog form created within Step 1.
    
             Cancel = True
             DoCmd OpenForm "frmCloseDialog"
          End Sub 
  9. Close and save the form.


  10. Open the datasheet form, and modify the width of the first column. Note that when you try to close the datasheet form, the dialog form prompts you to select either OK or Cancel. When you click OK, the datasheet form closes without prompting you to save the changes to the form.



REFERENCES

For more information about declaring Windows APIs, search the Help Index for "Declare external procedures."

Additional query words: inf


Keywords          : FmrHowto 
Version           : WINDOWS:2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 6, 1999