Results 1 to 3 of 3
  1. #1
    mreniff is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    41

    Call Same Sub (same code) from different forms in DB

    The following code is customized to be triggered on multiple different forms. The various different criteria for the forms are in RED text. Can someone point me in the directions videos that will teach me how to reuse (call a procedure, function or module?) this code, as needed based on the "active" form? I want to eliminate the need of adding the button and customized code to each form. I want know what form is open and active and which code is needs to process.

    cmdUpdate button:
    Private SubcmdUpdate_Click()

    Dim strSQL As String

    strSQL ="UPDATE Students SET Students.UpdateWorkshop = -1 "


    strSQL = strSQL& "WHERE Students.[ID] IN "
    strSQL = strSQL& "(SELECT TOP " & CLng(Me.Textbox1) & " [ID] FROMStudents As S "
    strSQL = strSQL& "WHERE (NZ(S.UpdateWorkshop, 0) = 0) AND (S.Major In ('BADM','PBA')) "
    strSQL = strSQL& "AND (S.Session = " &Val([Forms]![WorkshopsSession]![SessionCombo] & "") &"));"
    'DoCmd.Requery

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Pass parameters to procedure with arguments and concatenate query string.

    The generic code showing only one argument would be like:
    Code:
    Private SubcmdUpdate(strField As String)
    strSQL = "UPDATE Students SET [" & strField & "]= True "
    …
    CurrentDb.Execute strSQL
    End Sub
    Still need a button Click or some other event behind form to call procedure:
    Code:
    Private SubcmdUpdate_Click()
    SubcmdUpdate("UpdateWorkshop")
    End Sub
    Hard-coded literal can be a reference to form control to get user input.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mreniff is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2020
    Posts
    41
    Thank you. It will a couple of days before I can work on this.

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

Similar Threads

  1. Code Help: Using Field Value to Call Column Name
    By Legend1673 in forum Access
    Replies: 1
    Last Post: 02-20-2016, 07:10 PM
  2. Replies: 3
    Last Post: 06-13-2014, 05:57 PM
  3. Pause code until return from call command
    By trevor40 in forum Programming
    Replies: 1
    Last Post: 03-08-2014, 05:08 AM
  4. Replies: 3
    Last Post: 08-30-2013, 01:01 PM
  5. Call .dll Code On BeforeUpdate?
    By phi11yguy19 in forum Programming
    Replies: 0
    Last Post: 07-02-2011, 10:20 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