Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Unanswered: Passing variables between forms

    How do I pass a variable from one form to another?

    I posted earlier about having trouble working with nested forms, and it looks like creating a global variable is the answer, but I can't figure out how. In this case, I want to pass two values from a subform to another form.

  2. #2
    Join Date
    Sep 2003
    Location
    MI
    Posts
    3,713
    Try using the OpenArgs property ...

    Ex:

    Private Sub EditButton_Click()
    DoCmd.OpenForm "Departmental SI Update", , , , , , DeptSIReviewTbl & "|" & Me.CurrentRecord
    End Sub
    Back to Access ... ADO is not the way to go for speed ...

  3. #3
    Join Date
    Aug 2003
    Location
    Cleveland USA
    Posts
    184

    Angry

    The OpenArgs didn't work for me. I want to carry two values into unbound text boxes. I got it to work for existing records, but if the main form is in Add mode, it won't carry any values over, even if I try saving the record first.

  4. #4
    Join Date
    Nov 2003
    Location
    Sussex, England
    Posts
    404

    Global Variables

    One method I find quite flexible:

    Create a new module, call it modGlobals (or similar)

    Declare private variables, e.g.
    Private myText1 as string
    Private myText2 as string

    Create Public Subs, e.g.
    Public Set_Value1(TxtIN As String)
    myText1 = TxtIn
    End Sub

    Public Set_Value2(TxtIN As String)
    myText2 = TxtIn
    End Sub

    Now Public functions
    Public Function Get_Value1() As String
    Get_Value1=myText1
    End Function

    Public Function Get_Value2() As String
    Get_Value2=myText2
    End Function


    On the form (or sub form) raise an event to capture the values in the text boxes (or whatever) on the form, as easy way is to use a button, e.g.
    Sub Command1_Click()
    Set_Value1 me.textBox1
    Set_Value2 me.TextBox2
    End Sub

    On the next form, use the on_Load event to recover the values, e.g.

    Sub On_Load()
    msgbox "Value1 = " & Get_Value1()
    msgbox "Value2 = " & Get_Value2()
    'Other code
    End Sub

    The OpenArgs method can be adapted to do this, and uses less memory, but I find that using modules and global code is moe flexible.

    I wouldn't recommend using actual global variables throughout the database as some books recommend. When you can get them to work they cause more confusion than anything else, stick with the Set and Get stuff in a module.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •