Results 1 to 4 of 4
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250

    Best way to deal with data entry forms buttons (Save / Close without changes / Delete)

    Hello,

    I'm trying to optimize my data entry forms. My application works like this:
    • Main menu from which users can access all the "list" continuous forms (for example orders list, customers list, products list...)


    Code:
    Private Sub cmdCustomers_Click()
    
        DoCmd.OpenForm "frm_CustomersList", , , , acFormReadOnly
        
    End Sub

    • Each record on the "list form" has a "Details" button which opens a "details" form (read only)


    Code:
    Private Sub cmdDetails_Click()
    
        DoCmd.OpenForm "frm_CustomersDetails", , , "CustomerID=" & Me.CustomerID, acFormReadOnly
    
    End Sub

    • In the "list form" header, there's a "Create New Customer" button which opens a data entry form like this:


    Code:
    Private Sub cmdAddNew_Click()
    
        DoCmd.OpenForm "frmDE_Customers", , , , acFormAdd
    
    End Sub

    • Finally, on the customer details form, there is an "Edit" button which does this:


    Code:
    Private Sub cmdEdit_Click()
    
        DoCmd.OpenForm "frmDE_Customers", , , "CustomerID=" & Me.CustomerID, acFormEdit
    
    End Sub
    Everything except the Data Entry form is Read Only. I want to fool proof the data entry forms so there are no accidental record changes and so on. I have 3 buttons at the moment: Save, Cancel, Delete:

    Save button:
    Code:
    DoCmd.Close acForm, Me.Name, acSaveYes
    Cancel (close the form and keep the record in the state in which the form was opened):
    Code:
    Me.Undo
    DoCmd.Close acForm, Me.Name, acSaveYes
    Delete (if creating a new record, then cancel, if the record already exists, then delete):
    Note: Connect, Exec and Disconnect are my custom subroutines that work with Microsoft SQL Server
    Code:
        If Not IsNull(Me.PurchaseOrderID) Then
        
            ' Editing an existing record, delete it
            If MsgBox("Záznam bude smazán. Chcete pokračovat?", vbYesNoCancel + vbCritical, "Upozornění") <> vbYes Then Exit Sub
            
            Connect
            Exec "DELETE FROM tbl1PurchaseOrders WHERE PurchaseOrderID=" & Me.PurchaseOrderID
            Disconnect
    
            DoCmd.Close acForm, Me.Name, acSaveYes
            RequeryAllOpenForms
            
        Else
        
            ' Creating a new record, cancel
            If MsgBox("Vytvářený záznam bude smazán. Chcete pokračovat?", vbYesNoCancel + vbExclamation, "Upozornění") <> vbYes Then Exit Sub
            
            Me.Undo
            DoCmd.Close acForm, Me.Name, acSaveYes
            RequeryAllOpenForms
            
        End If
    I am not sure if this is the correct approach, and I do have some problems with data entry - for example pressing F5 refreshes the form and saves the record no matter what, skipping the validation checks.

    Can you please advise if the approach above is a good practice, or what's the better way to do it?

    Thanks, Tomas

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The BeforeUpdate event is the place for validation. You can always prevent a save by cancelling in that event.
    Pat Hartman has written up an excellent piece about it here. She has also made an accompanying video and a sample database you can download.
    https://www.access-programmers.co.uk...-and-2.324342/

    There is also a AUG Session that I think was recorded, more info here:
    https://accessusergroups.org/pacific...h-pat-hartman/
    Last edited by Minty; 06-07-2023 at 07:37 AM.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Attached an example of how you might do this:
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by Bob Fitz View Post
    Attached an example of how you might do this:
    Small addition to code:
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Replies: 4
    Last Post: 08-10-2017, 12:56 AM
  2. Replies: 7
    Last Post: 02-19-2016, 11:14 PM
  3. Replies: 2
    Last Post: 11-05-2014, 09:16 AM
  4. Replies: 2
    Last Post: 09-02-2014, 06:21 AM
  5. Replies: 10
    Last Post: 09-18-2012, 02:00 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