Results 1 to 3 of 3
  1. #1
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,699

    Reusable code for forms

    Programmer's 'DRY' rule (Don't Repeat Yourself). If your DB has multiple forms here's a way to implement DRY for some common form procedures.


    The below 2 procedures are in a standard module. They can be called by any form's button click event like this:


    Call CCAddNewRecord(Me)
    or
    Call CCDeleteRecord(Me)




    Code:
    Public Function CCAddNewRecord(frm As Form)
        With frm
                DoCmd.GoToRecord , , acNewRec
        End With
    End Function
    or


    Code:
    Public Function CCDeleteRecord(frm As Form)
        With frm
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdDeleteRecord
            DoCmd.SetWarnings True
        End With
    End Function
    Above code is bare bones to show concept. With additional lines of code for error handling and such, it can save dozens or hundreds of lines by not having the code in each and every form's code module.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,858
    The nice thing about having those is you can have them all in one module, add it to your application parts, and then just drop the module into any project in one shot with the click of a button.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,699
    In post#1, I mentioned that the supplied code was bare bones. Here's a more comprehensive real world capable version of the two functions.

    Code:
    Public Function CCAddNewRecord(frm As Form)
        On Error GoTo Error_Handler
        With frm
            'if currently working on an unsaved new record
            'and this function is called again, ignore.
            If .NewRecord Then
            Else
                DoCmd.GoToRecord , , acNewRec
            End If
        End With
    Error_Handler_Exit:
        On Error Resume Next
        Exit Function
    Error_Handler:
        Select Case Err
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in function CCAddNewRecord, called by " & frm.Name
        End Select
        Resume Error_Handler_Exit
        Resume
    End Function
    
    
    '---------------------------------------------------------------------------------------
    ' Method : CCDeleteRecord
    ' Author : davegri
    ' Date    : 03/16/22
    ' Purpose: Delete record. This can be called from either a main form or a child form.
    '   Note that attempting to delete a main form record that has dependent child records
    '   will result in a runtime error. To avoid this, programmer is responsible to determine if
    '   that error will occur and not call this function at all, or handle the runtime error
    '   in this function's error handler.
    '   The runtime error can also be avoided by instituting cascade deletes via the 
    '   relationships if appropriate.
    '---------------------------------------------------------------------------------------
    Public Function CCDeleteRecord(frm As Form)
        On Error GoTo Error_Handler
        With frm
            'handle situation that user is trying to delete an
            'unsaved new record.
            If .NewRecord Then
                frm.Undo
                DoCmd.RunCommand acCmdRecordsGoToLast
                Exit Function
            End If
            If MsgBox("Do you want to completely remove the displayed record?", vbInformation + vbYesNo) = vbNo Then
                Exit Function
            End If
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdDeleteRecord
            DoCmd.SetWarnings True
            DoCmd.RunCommand acCmdRecordsGoToFirst
        End With
    Error_Handler_Exit:
        On Error Resume Next
        Exit Function
    Error_Handler:
        Select Case Err
            Case Else
                MsgBox "Error " & Err.Number & " (" & Err.Description & ") in function CCDeleteRecord, called by " & frm.Name
        End Select
        Resume Error_Handler_Exit
        Resume
    End Function
    Last edited by davegri; 06-17-2024 at 07:26 AM. Reason: clarity

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

Similar Threads

  1. Replies: 18
    Last Post: 03-22-2022, 04:09 PM
  2. Replies: 2
    Last Post: 04-10-2020, 05:32 PM
  3. Use of code to close forms
    By Roncc in forum Access
    Replies: 2
    Last Post: 01-21-2018, 08:24 PM
  4. update VBA 2.0 code in forms into access 7.0 code
    By toughwg in forum Programming
    Replies: 1
    Last Post: 12-08-2015, 11:40 AM
  5. Intellisence within code behind forms
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 07-24-2014, 02:28 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