PDA

Click to See Complete Forum and Search --> : VB Multiple Form Passing ?


July
June 28th, 2002, 09:09 PM
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.

DTech
June 28th, 2002, 09:37 PM
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.

nebulus200
June 28th, 2002, 09:44 PM
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)).

Simon Templer
June 28th, 2002, 09:59 PM
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

July
June 28th, 2002, 11:05 PM
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.

nebulus200
June 28th, 2002, 11:06 PM
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

DTech
June 28th, 2002, 11:11 PM
yep, nebulous is 100% right,
the less crap you carry.... the easier it is to remeber where everything goes.

Simon Templer
June 29th, 2002, 03:49 AM
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 :)

July
June 29th, 2002, 03:55 AM
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. :)

ammo
June 29th, 2002, 07:05 AM
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