Results 1 to 10 of 10

Thread: VB Multiple Form Passing ?

  1. #1
    Senior Member
    Join Date
    Jun 2002
    Posts
    102

    VB Multiple Form Passing ?

    I'm just learning Visual Basic. It's kind of fun but my VB book really sucks. I've been doing a little searching. And Thought I might ask the great communtity of Antionline. So here goes. I've got this program I'm supposed to do as an assignment.

    The main form is supposed to calculate which i got ok. The calculations is done. Then once it outputs to the label captions. I'm supposed to move the answers from the captions to a summary form. The whole point of the summary form is to display today's totals. So everytime there is a calculation on the main form it's supposed to print out to the calculations on the labels, then move to the summary form. Now how do I move the output from the captions on the main form to the seperate summary form? And then how do I keep on adding to the summary everytime there is a calculation?


    Main Form
    Private Sub cmdCalculate_Click()
    '
    ''Setup Const Variables
    Const curTaxRate As Currency = 0.08 'The tax Rate 8%
    Const curHourly As Currency = 30# 'The hourly wage is $30.00
    '
    ''Setup Variables
    Dim mcurTotal As Currency
    Dim mcurSalesTax As Currency
    Dim mcurHours As Currency
    Dim mcurParts As Currency
    Dim intHours As Integer
    Dim strName As String
    Dim intNumber As Long

    '
    ''Setup Strings to go into Variables
    curParts = Val(txtAmountParts.Text)
    intHours = Val(txtHours.Text)
    strName = Val(txtCustName.Text)
    intNumber = Val(txtJobNumber.Text)
    '
    ''Calculate Price for Parts
    mcurParts = curParts
    lblParts.Caption = FormatCurrency(mcurParts)
    '
    ''Calculate labor price
    mcurHours = intHours * curHourly
    lblLabor.Caption = FormatCurrency(mcurHours)
    '
    ''Calculate SubTotal
    lblSubTotal.Caption = FormatCurrency(mcurParts + mcurHours)
    '
    ''Calculate Just the Sales Tax
    mcurSalesTax = curParts * curTaxRate
    lblSalesTax.Caption = FormatCurrency(mcurSalesTax)
    '
    ''Calculate The Total with Tax
    mcurTotal = mcurSalesTax + mcurHours + mcurParts
    lblTotal.Caption = FormatCurrency(mcurTotal)
    End Sub

    Private Sub cmdClear_Click()
    'Clear All Boxes and Labels
    txtAmountParts.Text = ""
    txtHourst.Text = ""
    txtCustName.Text = ""
    txtJobNumber.Text = ""
    lblParts.Caption = ""
    lblLabor.Caption = ""
    lblSubTotal.Caption = ""
    lblSalesTax.Caption = ""
    lblTotal.Caption = ""
    End Sub

    Private Sub cmdOk_Click()
    Unload Me
    End Sub

    Private Sub cmdPrint_Click()
    PrintForm
    End Sub
    Summary Form
    Option Explicit
    'This is were I get stuck, I put in the labels for the output but then what? And how do I keep
    ' adding totals from the main form everytime there is a calculation?

    Private Sub Form_Load()
    lblSumParts.Caption = "<insert somthing here>" 'The total amount of parts for the day
    lblSumHours.Caption = "<insert somthing here>" ' The total number of hours for the day
    lblSumTotal.Caption = "<insert somthing here>" ' The main total from the main form
    End Sub
    Does antionline have syntax highlighting for posting code? I didn't see anything so I just used the quote thingy.
    Good Grief

  2. #2
    Junior Member
    Join Date
    Jun 2002
    Posts
    21
    Global variables, look 'em up in your book.
    you want the values that need to be passed between forms to be global rather than form level variables.
    \"The fifth horseman of the apocalyse?\"
    \"Yeah, he left the group before they hit it big.\"
    T. Pratchett

  3. #3
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Its been a very long time since I programmed in VB, but global variables are one option, the other option is to have the objects/variables in one form reference the object/variable of the other form in somewhere that gets called when the user goes back and forth between forms (or if they don't use something like mouse-over (there are better choices than this one, I just don't remember all of the various event options for a form)).
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    175

    The magic word is Scope

    Both DTech and nebulus200 are correct either one would work...If you can't find it in your book then here ya go:

    Note: I didn't look at your code, so these are just examples

    In A Module:

    Public gstrGlobal as String

    In Form1:

    'Set the String to something
    gstrGlobal = "Whatever String you want to pass between forms"

    In Form2:

    'Retrieve the String (We'll make it pop-up in a message box)
    msgbox gstrGlobal

    Or the other way:

    In Form1:

    Public strGlobal as string 'Make the string a property of Form1

    'Then set the string to something
    strGlobal = "Some String"

    In Form2:

    'Get the strGlobal Property from Form1
    msgbox Form1.strGlobal


    Enjoy

    Simon Templer
    Simon Templer

    \"Your work is to discover your world and then with all your heart give yourself to it. \"
    -The Buddha

  5. #5
    Senior Member
    Join Date
    Jun 2002
    Posts
    102
    Thanks Simon that was a big help. I got it to show the same info on the 2nd form. I'm gonna put it in a string and that way everytime there is a calculation on form1 it will added it to the string on form2. I'm not sure if it makes anysense but your example did help. I was at the microsoft site looking on diferent ways I could do it. My Visual Basic Compiler doesn't come with the help, which sucks so I usually have to go wondering around.
    Good Grief

  6. #6
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    I should have mentioned this earlier, but I would highly recommend that you avoid the use of global variables where possible. If you can access the data from exisiting objects and variables, it is 'cleaner' to do that rather than create a new and global variable...Object Oriented Programming is 'a good thing'...

    Neb
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  7. #7
    Junior Member
    Join Date
    Jun 2002
    Posts
    21
    yep, nebulous is 100% right,
    the less crap you carry.... the easier it is to remeber where everything goes.
    \"The fifth horseman of the apocalyse?\"
    \"Yeah, he left the group before they hit it big.\"
    T. Pratchett

  8. #8
    Senior Member
    Join Date
    Oct 2001
    Posts
    175

    VB Help

    July,

    I know how it is when don't have good documentation, Here are a few sites that might be helpful for you:

    http://www.vbworld.com/

    They have some good beginning tutorials on COM, and ActiveX, definately worth taking a look at.

    http://www.vbip.com

    Good place to go for learning about Winsock and creating Client/Server type applications.

    http://www.mvps.org/vbnet

    VBNet has a nice code library, and is a good place to go when you wanna play with API

    http://www.vbsquare.com/

    Another VB Site with beginning tutorials, they are very similiar to VBWorld, and have some beginning articles on Vb Objects, etc

    Simon Templer
    Simon Templer

    \"Your work is to discover your world and then with all your heart give yourself to it. \"
    -The Buddha

  9. #9
    Senior Member
    Join Date
    Jun 2002
    Posts
    102
    I liked this one the best http://www.mvps.org/vbnet/ Simon. Those are some good links. vbip.com had some cool stuff with tutorials. But I don't think I'm ready for that yet.
    Good Grief

  10. #10
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Nebulus and DTech are right in saying that you shoud avoid global variables.
    Refering to the other form's variables is one way to do it, but if you wanted to push it one step further, you would call a public sub or function on the other form to update its private variable(s) with the new one(s) you would pass as parameter(s). The public function on that second form would then do whatever logic should be down with the updated variables.

    The point of this is that all the logic related to the updating is contained in a single place. Lets say that later you want to have another form update the summary fields, and that you had already added logic that, for example, wrote the summary in a sentence and displayed it on the summary form. On that new form you would only need to call that public sub/function of the summary form where all the displaying logic is contained.

    Anyways, I hope I wasn't too confusing (it's late and I'm tired...)

    Ammo
    Credit travels up, blame travels down -- The Boss

Posting Permissions

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