Results 1 to 10 of 10
  1. #1
    ajudante is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2020
    Posts
    4

    Use form values in another form

    I have two forms. form1 has textbox1 and textbox2 asking user input. It also has a button that the user clicks after filling the boxes. The button closes form1 and opens form2. form2 has textbox3 with has a simple formula adding the values inserted by the use in textbox1 and textbox2. This needs to work even after form1 is closed. How to do this? I gather that there are two possible solutions, but I can't make them work.



    Solution 1 is with Global Variables, with code below.

    Code:
    In a VBA Module:
    
    Option Compare Database
    Global Var1 As Single
    Global Var2 As Single
    
    In form1 button:
    
    Private Sub Continue_button_Click()
    Var1 = Val(Me.textbox1 & "")
    Var2 = Val(Me.textbox2 & "")
    DoCmd.OpenForm "form2"
    DoCmd.Close acForm, Me.Name
    End Sub
    
    In form2 textbox3:
    =[Var1]+[Var2]
    In form2 textbox3 I get the #Name? error


    Solution 2 is OpenArgs, with code below

    Code:
    In the form1 button
    
    Private Sub Continue_button_Click()
    docmd.openform "form2",,,,,textbox1 & ":" & textbox2
    doCmd.Close acForm, Me.Name
    End Sub
    
    In the form2 load event
    
    Var1=split(openargs,":")(0)
    Var2=split(openargs,":")(1)
    
    and in form2 textbox3
    =[Var1]+[Var2]
    This gives me a Run-time error 13 Type mismatch error

    In both solutions, there is something line of code that is wrong or some code missing, but I can't figure out what. Any clues?

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    Try it without the brackets around var1 and var2.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can't reference a VBA variable from a textbox control source, only from VBA. Your first method could place the values in the textbox in code in the load event:

    me.textbox3 = Var1 + Var2
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    FYI for solution #2 I would think it would be more like

    Code:
    Dim argArray() as Variant
    argArray = Split(Me.OpenArgs, ":")
    Me.Textbox3 = argArray(0) + argArray(1)
    Also openargs is a string type so you may or may not have to do some type conversions....?

  5. #5
    ajudante is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2020
    Posts
    4
    Thanks. But var1 and var 2 are automatically added brackets

  6. #6
    ajudante is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2020
    Posts
    4
    Thanks. Where do I write that? In form1 button code or in form2 load code?

  7. #7
    ajudante is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2020
    Posts
    4
    Quote Originally Posted by pbaldy View Post
    You can't reference a VBA variable from a textbox control source, only from VBA. Your first method could place the values in the textbox in code in the load event:

    me.textbox3 = Var1 + Var2
    Thanks. I tried that and I got the same #Name? error

  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 ajudante View Post
    Thanks. I tried that and I got the same #Name? error
    What I posted was VBA that would go in the form's load event. You'd first have to remove the existing formula.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,784

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,411
    Code:
    debug.print split("ABC:DEF",":")(0)
    will return ABC

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

Similar Threads

  1. Replies: 5
    Last Post: 12-07-2019, 01:32 AM
  2. Replies: 9
    Last Post: 04-21-2017, 11:23 AM
  3. Replies: 8
    Last Post: 03-20-2017, 07:22 PM
  4. Replies: 6
    Last Post: 10-17-2014, 09:38 AM
  5. Replies: 19
    Last Post: 09-09-2014, 01:36 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