locked
Which form passed a value to the main form RRS feed

  • Question

  • Hello,

    There are 3 Forms (Form1, Form2 and Form3). On the Form1 there is a TextBox(txtID), when Form1's subform gridView row is clicked, txtID displays grid row id. When grid double clicked, Form3 opens and Form1's txtID text is passed to Form3.

    On the gridview double click event:

    private sub Row_Id_DblClick(Cancel As Integer)
       DoCmd.OpenForm "Form3", acNormal
    End Sub

    In the Form3 Form Load event:

    Private Sub Form_Load()
       dim myID As String
       myID = Form_Form1.txtID 
    End Sub

    On the Form 2 a button is clicked and Form3 opens and the id form a TextBox(txtID) on the Form2 is passed to Form3. On Form3 Load event I tried: 

    myID = Form_Form2.txtID

    This way I cannot call Form3 from both Form1 and Form2. It only works one at a time. What is the correct way to implement this.

    Any help is appreciated.

    Savaln

    Monday, July 16, 2018 10:24 PM

Answers

  • The version in which the application was originally developed shouldn't make any difference.

    1.  What is the exact code you have used in the first form's module to open the other form?

    2.  Where have you put the code in the other form's module?  It should be in the form's Load event procedure.

    3.  Are the other forms ever opened independently, other than from the first form?  If so you should make the assignment of the value to the variable conditional on the OpenArgs property not being Null.

    Of course, you could cut out the middleman and simply pass the value of the txtID control to the other form as its OpenArgs property:

        DoCmd.OpenForm "Form3", View:=acNormal, OpenArgs:=Me.txtID

    and assign it to the variable in the other form's Load event procedure:

        Dim myID As String
        
        If Not IsNull(Me.OpenArgs) Then
            myID = Me.OpenArgs
        End If

    Ken Sheridan, Stafford, England

    • Proposed as answer by Terry Xu - MSFT Wednesday, July 18, 2018 9:17 AM
    • Marked as answer by Savalan Wednesday, July 18, 2018 11:02 AM
    Tuesday, July 17, 2018 5:53 PM

All replies

  • Hello Savalan,

    If you checked the Form3 is opened, then you could try to set value to the text box directly.

    such as 

    If Application.CurrentProject.AllForms("Form2").IsLoaded Then
    Forms!Form2.Controls("Text26").Value = "BBB"
    End If

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 17, 2018 1:16 AM
  • I'd suggest that you pass the form name as the OpenArgs argument of the OpenForm method:

       DoCmd.OpenForm "Form3", View:=acNormal, OpenArgs:=Me.Name

    You can then reference the OpenArgs property in Form3's module to determine the calling form:

       Dim myID As String
       myID = Forms(Me.OpenArgs).txtID

    Ken Sheridan, Stafford, England

    Tuesday, July 17, 2018 11:16 AM
  • Hello Terry,

    Thank you for the reply.

    Is Text26 on the Form2? If so, Form2 already has a TextBox with the value ID and when I click the button on Form2 to open Form3, I want to pass the TextBox's value to Form3. If it is on Form3, maybe I could drop a TextBox on From3 with visibility none, and pass the ID from Form2 to Form3, then use the ID in this TextBox to populate other controls on Form3. 

    I am Sorry if I wasn't clear enough. 

    Best regards,

    Savalan

    Tuesday, July 17, 2018 4:33 PM
  • Hello Ken,

    Thank you for the reply. You approach is what I was looking for and had a lot of hope that it would solve my problem. When I tried, I got the following error on the line "myID = Forms(Me.OpenArgs).txtID":

    The application had been created long time ago (with Access 2007 or 2010). I am using Access 365. Could it be the reason?

    Best regards,

    Savalan

    Tuesday, July 17, 2018 4:42 PM
  • The version in which the application was originally developed shouldn't make any difference.

    1.  What is the exact code you have used in the first form's module to open the other form?

    2.  Where have you put the code in the other form's module?  It should be in the form's Load event procedure.

    3.  Are the other forms ever opened independently, other than from the first form?  If so you should make the assignment of the value to the variable conditional on the OpenArgs property not being Null.

    Of course, you could cut out the middleman and simply pass the value of the txtID control to the other form as its OpenArgs property:

        DoCmd.OpenForm "Form3", View:=acNormal, OpenArgs:=Me.txtID

    and assign it to the variable in the other form's Load event procedure:

        Dim myID As String
        
        If Not IsNull(Me.OpenArgs) Then
            myID = Me.OpenArgs
        End If

    Ken Sheridan, Stafford, England

    • Proposed as answer by Terry Xu - MSFT Wednesday, July 18, 2018 9:17 AM
    • Marked as answer by Savalan Wednesday, July 18, 2018 11:02 AM
    Tuesday, July 17, 2018 5:53 PM
  • Hi Ken,

    Thank you again. I will try as you instructed and update you.

    Regards,

    Savalan

    Tuesday, July 17, 2018 9:50 PM
  • Hello Savalan,

    I agree with Ken that OpenForm with OpenArgs property could translate ID from source form.

    In addition, since you have two forms which would open the form3, please note that if the Form3 is opened by form1, the load event will not trigger again even you call OpenForm form from Form2.

    In this situation, you need consider about closing form3 first or write the value to the form3 directly.

    If Application.CurrentProject.AllForms("Form3").IsLoaded Then
    Forms!Form3.Controls("myID").Value = Me.txtID
    'OR
    'DoCmd.Close acForm, "Form3"
    Else
    DoCmd.OpenForm "Form3", View:=acNormal, OpenArgs:=Me.txtID
    End If

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 18, 2018 9:34 AM
  • Hi Ken,

    Thank you so much. This code worked as I hoped. Sorry it took so long to reply. The application has a lot of stuff in it and I had to go through every line of code to see what was going on.

    Best regards,

    Savalan

    Wednesday, July 18, 2018 11:06 AM
  • Hello Terry,

    Thank you for the reply. I just tested what was your concern. I definitely have to use your code in addition to Ken's code. 

    Best regards,

    Savalan

    Wednesday, July 18, 2018 11:18 AM