Results 1 to 15 of 15

Thread: Passing Data Between two forms

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    98

    Exclamation

    I have all the values of my text boxes stored in variables.
    This is on form1.

    I am trying to pass the variables with the values to the second form form2.....

    Does anyone know how I can do this?


    Help is apprecaited!

    Steve

  2. #2
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    Ummm...you mean something like
    Code:
    Public Sub Command1_Click()
         Form2.Text1.Text = Form1.Text1.Text
    End Sub
    Dim

  3. #3
    Addicted Member
    Join Date
    Oct 2000
    Location
    Vienna/Austria
    Posts
    132
    Hi Dim !!!

    Another solutions,

    1) declare in Form2 public variables then zou can set them
    like
    Code:
       forms2.Myvariable = me.textbox1.txt
    2) or you make properties in Forms2 and set them
    advantage of this is you can check the values

    Code:
       forms2.Mypropertie = me.textbox1.txt
    if you need any example - pls reply to this thread

    -cu TheOnly

  4. #4
    Lively Member flint's Avatar
    Join Date
    Oct 2000
    Posts
    67

    Just some quick notes

    Here's some things to keep in mind when passing varibles form one form to another:

    If you unload the form with the initial values you will lose any values that you may have defined on that form which will affect other forms that make references to the varibles on the form

    For Example:
    Code:
     unload frmCalculate  
    'this will erase the varibles that you have 
    'defined from memory
    
    'Use the hide method to keep the varible values 
    'in memory for further calculations on different forms
    frmCalculate.Hide
    You need to identify what the value is going to be. If it is Text then use something like:
    Code:
    Dim myText As String  'Of course this is used by VB as default
    myFirstForm.mytxtField.Text = myText
    
    'Let's say that you wanted a label control on your 
    'second form to display the text string identified 
    'on the first form. Here's the approach:
    mySecondForm.myLabel.Caption = myText
    
    'Of course this could be narrowed down by using this code:
    mySecondForm.myLabel.Caption = myFirstForm.mytxtField.Text
    When transfering values or implementing formulas try this:
    Code:
    'This example assumes that you plug in the Price and
    'Quantity on the first form in seperate text fields and 
    'then click the Next button on form1 to show form2 that 
    'has the Total amount of the purchase.
    
    'Needed:  2 forms(named form1 and form2), two Text fields 
    'on form1(named txtPrice and txtQuantity) and One Text 
    'field on form2(named txtTotal), on form1 place a command
    'button named cmdNext, you can also apply labels as you 
    'see fit(perhaps to identify which is Price and which is 
    'Quantity on form1 and Total Price on form2)
    
    
    'Now....Put this in the code module for form1
    Private Sub txtPrice_LostFocus()
    txtPrice = Format(txtPrice, "Currency")
    End Sub
    
    Private Sub txtQuantity_LostFocus()
      txtQuantity = Format(txtQuantity, "#,##0")
    End Sub
    
    Private Sub cmdNext_Click()
     form1.Hide
     form2.Show 
    End Sub
    
    'Now put this onto the form2 code module
    Private Sub Form_Load()
    
    'this multiplies Price and Quantity on form1
    Val(txtTotal) = Val(form1.txtPrice) * Val(form2.txtQuantity)
    txtTotal = Format(txtTotal, "$#,##0")
    
    End Sub
    Play around with this and you start seeing how everything correlates...look at other examples posted throughout the web and break them down to see what makes them function...this is certainly an efficient way to learn how objects are structured.

    Hope this helps.
    Feel free to email me [email protected] or post here on the VB Forum, I'm sure someone will help you.

    Regards,
    Flint

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    98

    Reply

    Here is my code that I have in form1 in my cmdPrint Click event

    airfare_total = Val(airfare_day1) + Val(AirFare_Day2) + Val(AirFare_Day3) + Val(AirFare_Day4) + Val(AirFare_Day5) + Val(AirFare_Day6) + Val(AirFare_Day7)

    amount_total = Val(Amount_Day1) + Val(Amount_Day2) + Val(Amount_Day3) + Val(Amount_Day4) + Val(Amount_Day5) + Val(Amount_Day6) + Val(Amount_Day7)

    meals_total = Val(meals_day1) + Val(Meals_Day2) + Val(Meals_Day3) + Val(Meals_Day4) + Val(Meals_Day5) + Val(Meals_Day6) + Val(Meals_Day7)

    lodging_total = Val(lodging_day1) + Val(Lodging_Day2) + Val(Lodging_Day3) + Val(Lodging_Day4) + Val(Lodging_Day5) + Val(Lodging_Day6) + Val(Lodging_Day7)

    auto_total = Val(auto_day1) + Val(Auto_Day2) + Val(Auto_Day3) + Val(Auto_Day4) + Val(Auto_Day5) + Val(Auto_Day6) + Val(Auto_Day7)

    Rental_Total = Val(rental_day1) + Val(Rental_Day2) + Val(Rental_Day3) + Val(Rental_Day4) + Val(Rental_Day5) + Val(Rental_Day6) + Val(Rental_Day7)

    mtravel_total = Val(Mtravel_day1) + Val(MTravel_Day2) + Val(MTravel_Day3) + Val(MTravel_Day4) + Val(MTravel_Day5) + Val(MTravel_Day6) + Val(MTravel_Day7)


    ********These variables I want to pass to form2******
    This is where i am having problems



    Here is my code in form2.. in the cmdsubmit click event


    rsnames.AddNew

    rsnames("StartDate").Value = frmExpense2.datStartDate.Value
    rsnames("EndDate").Value = frmExpense2.datenddate.Value

    rsnames("FirstName").Value = frmExpense.lblEmpNumsub.Caption
    rsnames("LastName").Value = frmExpense.lblEmpNamesub.Caption



    rsnames("Location").Value = frmExpense2.txtLocation.Text
    rsnames("AirFare").Value = airfare_total
    rsnames("Automobile").Value = auto_total
    rsnames("Lodging").Value = lodging_total
    rsnames("Meals").Value = meals_total
    rsnames("MTravel").Value = mtravel_total
    rsnames("OTravel").Value = amount_total
    'rsnames("Rental").Value = rental_total
    rsnames("Purpose").Value = frmExpense2.txtPTrip.Text

    rsnames.Update

    Unload Me
    Unload frmExpense2




    The textbox values make it to the db but the values of the variables!!!!



    Desperatly seeking help...
    Steve

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    To pass the variables from form to form declare them as public in a bas module.

    In a bas module.

    Public myVar1 as string
    Public myVar 2 as string
    etc.

    In form1
    myVar1 = text1.text

    In form2
    text1.text = myVar1

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Hyperactive Member D12Bit's Avatar
    Join Date
    Oct 2000
    Location
    Guatemala
    Posts
    373
    Just a little note.

    If a project doesn´t have a Bas module (simple project?),

    In Declarations of the form holding the value:
    Public Variable

    and then you´ll be able to choose from the objects of the form...

    Text1.Text = form.variable

    Saludos...


    [Edited by D12Bit on 10-31-2000 at 11:59 AM]
    "Who Dares Wins" - "Quien se Arriesga Gana"
    Mail me at:

  8. #8
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Question

    Why not qualify the variables?

    e.g. frmExpense2.Airfare_Total

    Maybe you are creating local variables - have you got Option Explicit in the Form's code header?

    Cheers,

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    This isn't really necessary for what you are doing but I figured I'd throw it out there because I just learned it. You can add properties to forms just as you would an active x control.

    Code:
    Private m_Total As String
    
    Public Property Let MyTotal(newValue As String)
        m_Total = newValue
    End Property
    
    Public Property Get MyTotal() As String
        MyTotal = m_Total
    End Property

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946
    D12Bit

    If you declare a variable public at form level it is not accessable from other forms. The question was how to make a variable accessable and the way to do it is in a .bas module.

    Procedure/Add Module

    And yes..do use Option Explict at the head or your stdcode
    and your bas because without it and using global variables you are heading for trouble.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11
    Lively Member flint's Avatar
    Join Date
    Oct 2000
    Posts
    67
    That's what I was thinking paulw. Just Qualify your variables.
    HeSaidJoe's advice is great to. You can make them Public so that all forms can access them.

    Code:
    'Instead of this:
    
    rsnames("Location").Value = frmExpense2.txtLocation.Text 
    rsnames("AirFare").Value = airfare_total 
    rsnames("Automobile").Value = auto_total 
    rsnames("Lodging").Value = lodging_total 
    rsnames("Meals").Value = meals_total 
    rsnames("MTravel").Value = mtravel_total 
    rsnames("OTravel").Value = amount_total 
    'rsnames("Rental").Value = rental_total  'You may want to remove the single quotation to make this line of code work
    rsnames("Purpose").Value = frmExpense2.txtPTrip.Text 
    
    'Try this:
    
    rsnames("Location").Value = frmExpense1.txtLocation.Text 
    rsnames("AirFare").Value = frmExpense1.airfare_total.Value 
    rsnames("Automobile").Value = frmExpense1.auto_total.Value 
    rsnames("Lodging").Value = frmExpense1.lodging_total.Value 
    rsnames("Meals").Value = frmExpense1.meals_total.Value 
    rsnames("MTravel").Value = frmExpense1.mtravel_total.Value 
    rsnames("OTravel").Value = frmExpense1.amount_total.Value 
    rsnames("Rental").Value = frmExpense1.rental_total.Value 
    rsnames("Purpose").Value = frmExpense1.txtPTrip.Text 
    
    'I am assuming that frmExpense1 is the name of form one where the variables are.
    Just another quick note:

    You may want to consider making your variables like airfare_total into AirfareTotal (taking the underscore out). I'm not saying what you have is wrong. The underscore connects an event to an object. This will simplify things a little and eliminate any ambiguious calculations made by an underscore misinterpretation. The underscore connects an event to an object.

    Hope this helps.

    Flint

    [Edited by flint on 10-31-2000 at 12:29 PM]

  12. #12
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    HeSaidJoe:

    If you declare a variable public at form level it is not accessable from other forms
    Yes it's possible, i'm not saying it's the best way but:

    Form1.PublicVariable

  13. #13
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    by the way

    Why i'm not able to post a new thread,a
    blank screen come up and it said done in the bottom left.

    But i can reply to threads, what's the problem!

  14. #14
    Lively Member flint's Avatar
    Join Date
    Oct 2000
    Posts
    67
    I see your thread sebs. There's np. I don't know about the new threads thing though.

    You took the words out of my mouth sebs. As long as you qualify it first you can access it.(Or at least I can)

    Flint

    [Edited by flint on 10-31-2000 at 12:36 PM]

  15. #15
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Sebs:

    I stand corrected. It does work and I should have known it would because it uses the same method as any other object...like a textbox....form2.text1 = form1.text1, a form is an object as well and behaves in a similar manner.

    Duh...dumb dumb me...it could lead to a lot of extra code but it would work.

    Later
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

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



Click Here to Expand Forum to Full Width