Solved How to get the Form name that triggered an event. (1 Viewer)

raziel3

Registered User.
Local time
Today, 06:31
Joined
Oct 5, 2017
Messages
275
I have 3 forms that basically does the same data entry one for Inventory (deInventory), Expenses (deExpenses) and Foreign Purchases (deForeign). I have an event set up on each of them to detect available vendor credits.

Code:
Private Sub SUBTOTAL_AfterUpdate()
Dim rs As DAO.Recordset
Dim CreditCheck As String
Dim MsgBoxAns As String

CreditCheck = "SELECT VENCR.VENDISP, SUM(VENCR.AMOUNT) " & _
                "FROM VENCR " & _
                "WHERE VENCR.VENDISP = " & SQLQuote(txtVENDISP) & _
                "GROUP BY VENCR.VENDISP " & _
                "HAVING Sum(VENCR.AMOUNT)<> 0"
              
Set rs = CurrentDb.OpenRecordset(CreditCheck, dbOpenSnapshot)

If rs.RecordCount > 0 Then
    If MsgBox("Vendor Credits Available", vbYesNo, "Apply Credits?") = vbYes Then
        DoCmd.OpenForm "popVENDCR", acNormal
        Forms!popVENDCR.Form.txtVENDISP = VENDISP
    End If
  
End If

rs.Close
End Sub

After the user selects whatever I have another event on the popVENDCR to transfer the credit amount being claimed to the data entry forms.

Code:
Private Sub btnAPPCR_Click()
If txtCRUSED > txtCRTTL Then
    MsgBox "Cannot Apply Credits More Than What Is Available", vbCritical, "Credit Application Error"
    txtCRUSED = ""
    txtCRUSED.SetFocus
Else
    Forms![deInventory].Form.txtCMAMT = txtCRUSED
    DoCmd.Close acForm, "popVENDCR", acSaveYes
End If
End Sub

The question. On this line Forms![deInventory].Form.txtCMAMT = txtCRUSED, instead of doing 3 different pop up forms, how can I get Forms![deInventory] to be dynamic so whatever form that triggered the pop up form, txtCRUSED will be transferred to that form's field?
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 11:31
Joined
Sep 12, 2006
Messages
15,656
I think you will have to pass the form name into the sub, or have the same sub in each form, and then you can just use me.name to get the name of the form.

Maybe screen.activeform.name might give you the name of the current form.

You might want to use a numeric value instead of the form name, to avoid wasting space in the database.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:31
Joined
Feb 19, 2002
Messages
43,275
Pass in a reference to the Calling Form using the OpenArgs.
 

MarkK

bit cruncher
Local time
Today, 03:31
Joined
Mar 17, 2004
Messages
8,181
I typically solve this problem as demonstrated. This ensures better separation of concerns. Note that clicking OK in the popup only hides the form, doesn't close it, so the function can return a value from the popup.
 

Attachments

  • Dialog.accdb
    412 KB · Views: 48

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:31
Joined
May 21, 2018
Messages
8,529
At the top of the Called form put the following
Code:
Public ReturnControl as Access.control

In the calling form pass a reference to the control to get the return value
Code:
DoCmd.OpenForm "popVENDCR", acNormal
Forms!popVENDCR.Form.txtVENDISP = VENDISP
set Forms!popVENDCR.ReturnControl = me.txtCMAMT


Then in the popup
Code:
Forms![deInventory].Form.txtCMAMT = txtCRUSED
becomes
ReturnControl = txtCrused
 

raziel3

Registered User.
Local time
Today, 06:31
Joined
Oct 5, 2017
Messages
275
Once again @MajP for the win. Your solutions are so simple and easy to implement. Much thanks.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:31
Joined
May 21, 2018
Messages
8,529
If you open the form ACDIALOG you will have to pass the name of the control because code execution stops and you would not be able to modify the ReturnControl. That line of code would not execute until after you close the pop up. Unfortunately you cannot pass Object references in open args.
 

Users who are viewing this thread

Top Bottom