Public Call statement in a form to Click a save and close Command button in a different form

Bob Aubry 41 Reputation points
2021-11-27T17:55:19.02+00:00

Module1

Public Sub Command25_Click()
Call Command25_Click

End Sub

In frmScreenOptions the following Call statement does not work.

DoCmd.OpenForm "frmScheduledTransactionsPaid", acNormal, "", "", , acNormal
If (Forms!frmScheduledTransactionsPaid!DueDate <= Date And Forms!frmScheduledTransactionsPaid!AutoEntry = -1) Then
Call Forms("frmScheduledTransactionsPaid").Command25_Click
End If

If you could tell me what is wrong, it would appreciate it. Bob

Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
898 questions
0 comments No comments
{count} votes

Accepted answer
  1. Albert Kallal 5,496 Reputation points
    2021-11-28T00:06:58.193+00:00

    First, you are CORRECT to change Private Sub to Public sub for the click event.

    Then in code you can do say this:

    dim strF      as String  
    dim F         as Form  
    strF = "frmScheduledTransactionsPaid"  
      
    DoCmd.OpenForm strF, acNormal, "", "", , acNormal  
    Set F = Forms(strF)  
    If (F!DueDate <= Date And F!AutoEntry = -1) Then  
        Call F.Command25_Click  
    End If  
      
    ' even F.Command25_Click will also work (with OUT the word call in front)  
    '  
    ' and if you want, full type out would be this  
      
    Call Forms("frmScheduledTransactionsPaid").Command25_Click  
    

    so say we have formA with this:

    153171-image.png

    And we add a code event to the button (right click) choose this:

    153142-image.png

    We choose code builder, and now we have this code stub:

    Private Sub Command5_Click()  
      
        ' code here to walk the dog  
        ' code here to clean the house  
          
        MsgBox "The dog has been walked, and the house is clean"  
          
          
    End Sub  
      
    

    As noted, we don't have to write/type in the Sub/End sub part - that automatic done for you.

    Now, the goal here is to call that click button code OUTSIDE of the form - from another form, or even from a standard plane jane code module. So, as noted (and you done this!!!), change the "private" to public. (it defaults to private, since mode code in a form is JUST FOR the one form.

    So we now have this:

    Public Sub Command5_Click()  
      
        ' code here to walk the dog  
        ' code here to clean the house  
          
        MsgBox "The dog has been walked, and the house is clean"  
          
          
    End Sub  
      
    

    Ok, so now from a code module, or even say formB, we want to call the above button click "code" stub.

    so, AS LONG as formA is open, we can do this. If formA is closed, then we can NOT call the code stub.

    So, now say we create formB, and have this button code:

    Private Sub Command8_Click()  
      
        Call Forms("FormA").Command5_Click  
          
      
    End Sub  
      
    

    So, use the above syntax to call the click button code stub inside of that form.

    Just remember, if you do plan or want to call that code inside of that formA in this example, FormA MUST be open for the call to work.

    If formA is open then ANY other form, or ANY other code, in other forms, or even code in a standard code module is free to call that FormA routine (but FormA must be open).

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.