Results 1 to 10 of 10
  1. #1
    MTM is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    20

    Reference Form 1 from Form 2 button in Public Sub

    I have a series of Forms to change Label captions (language) that are stored in tables.

    From an administration form with buttons I want to call a Sub to write the desired table captions to each label on the form. The problem I am having is getting the function to use the desired form's reference so I can read the controls on the form and then replace the caption. I know my code works using with a Call Write_LablTx("Chinese", Me) if I make the call from a button on Target_form with labels to change. However, I want to make the call from another form with buttons (Language_Admin). So far, I have not found a way to pass the reference to the Public Sub LablTx and reference on Target_Form. I see several posts about referencing other forms, but no code I have tried seems to work. Any help appreciated.

    Sample of my code:
    Language_Admin form on click event:

    ********************
    Private Sub Command0_Click()
    Call Write_LablTx("Chinese", Me) 'This works when I make the call from the Target_Form with labels to change.
    End Sub


    *********************
    Public Sub Write_LablTx(Language As String, F As Form)
    Dim db As Database
    Dim rs As Recordset
    Dim Ctl As Control
    Dim LabelTxt As String
    Dim Txt As String

    Set db = CurrentDb
    Set rs = db.OpenRecordset("Language_Test")

    Txt = F.Name
    Debug.Print Txt

    'Write to Label Text Table
    For Each Ctl In F.Controls
    rs.Edit
    Txt = Ctl.Name
    LabelTxt = rs.Fields(Language)
    F.Controls(Txt).Caption = LabelTxt
    Debug.Print Ctl.Name & ":" & F.Controls(Txt).Caption
    rs.MoveNext
    Next



    rs.Close
    Set rs = Nothing
    db.Close

    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Untested, but does this work?

    Call Write_LablTx("Chinese", Forms!Target_Form)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    MTM is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    20

    Reference Form 1 from Form 2 button in Public Sub

    Quote Originally Posted by pbaldy View Post
    Untested, but does this work?

    Call Write_LablTx("Chinese", Forms!Target_Form)
    in


    Thank you for the suggestion, but did not work.

    When I use "Call Write_LablTx("Chinese", Forms!BaseCoat_Front_2_Test)" from Administration form I

    get message: does not support this property method at line: "F.Controls(Txt).Caption = LabelTxt" in my code.

    Mike

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Can you try to change the sub to accept a form name (string) instead of a form object as argument and see what happens:
    Code:
    Public Sub Write_LablTx(Language As String, sFormName As String)
    Dim db As Database
    Dim rs As Recordset
    Dim Ctl As Control
    Dim LabelTxt As String
    Dim Txt As String
    Dim F as Form
    
    
    Set db = CurrentDb
    Set rs = db.OpenRecordset("Language_Test")
    
    Set F=CurrentProject.Allforms(sFormName)
    
    
    Txt = F.Name
    Debug.Print Txt
    
    
    'Write to Label Text Table
    For Each Ctl In F.Controls
    rs.Edit
    Txt = Ctl.Name
    LabelTxt = rs.Fields(Language)
    F.Controls(Txt).Caption = LabelTxt
    Debug.Print Ctl.Name & ":" & F.Controls(Txt).Caption
    rs.MoveNext
    Next
    
    
    rs.Close
    Set rs = Nothing
    db.Close
    
    
    End Sub
    To use it:
    Code:
    Call Write_LablTx("Chinese", "BaseCoat_Front_2_Test")
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It sounds like it's already using the form and into the control loop. When it errors, hit debug and see what control it's on (your debug should tell you). Make sure that control has a caption property on the target form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    MTM is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    20
    Hi Paul,
    I think I have not made myself clear. When I call the public sub from the Target Form (using a button event) my called sub evaluates the form that the call was made from (Target Form). However, what I want to do is make this call from a another form (Administrator Form) and reference the Target Form. So, my issue is how to reference the Target Form when I make the call from the Administrator form. Hope that is clear.
    Mike

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Have you tried to modified code I've posted in post #4?
    Also< I believe that the target form need to be open for any changes to work.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by MTM View Post
    Hi Paul,
    I think I have not made myself clear. When I call the public sub from the Target Form (using a button event) my called sub evaluates the form that the call was made from (Target Form). However, what I want to do is make this call from a another form (Administrator Form) and reference the Target Form. So, my issue is how to reference the Target Form when I make the call from the Administrator form. Hope that is clear.
    Mike
    I understood. Based on the error you got in post 3, it sounds like it is evaluating controls on the desired form, but running into a control that doesn't have a caption property. Can you attach the db here that hits the error in post 3?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    MTM is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2020
    Posts
    20
    I tried the suggestion of post 3 again and this time it worked perfectly.

    Thanks to all of you who contributed to this solved problem!

    Referencing in Form can be confusing, especially when I make a mistake and I am not certain a technique should work.

    Mike

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Glad you got it working.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. public function values from form
    By ShostyFan in forum Programming
    Replies: 4
    Last Post: 10-02-2017, 02:11 PM
  2. Displaying a Public Variable on a form
    By swenger in forum Programming
    Replies: 1
    Last Post: 06-23-2016, 01:56 PM
  3. Replies: 6
    Last Post: 04-07-2015, 03:41 PM
  4. Replies: 1
    Last Post: 03-28-2013, 07:54 AM
  5. VBA public procedure in a form
    By gg80 in forum Programming
    Replies: 3
    Last Post: 09-12-2010, 04:55 AM

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