VB help.. subprocedures and functions

CCrunnernb

Golden Member
Sep 14, 2000
1,002
0
76
Im trying to finish a homework assignment, I've gotten 90% of it done.. but getting my function to take the data that my Input subprocedure reads from the text file and multiply's it by a fixed number.

This is just a portion of the code.. im just getting over the Flu so maybe im just overlooking something?

 

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
Private Function T(S As Single) as single
return (totalComputersSold * S)
End Function

edit: totalComputersSold is not a global variable, it needs to be (unless you just didnt post it)
 

CCrunnernb

Golden Member
Sep 14, 2000
1,002
0
76
Heres the whole program


Option Explicit

Private Sub frmTopFour_Load()
Dim totalComputersSold As Single
totalComputersSold = 12000000
End Sub

Private Sub Data(C As String, S As Single)
Dim T As Single
Open App.Path & "\computers.txt" For Input As #1
Input #1, C, S
Call Display(C, S, T)
Input #1, C, S
Call Display(C, S, T)
Input #1, C, S
Call Display(C, S, T)
Input #1, C, S
Call Display(C, S, T)
Close #1
End Sub

Private Sub Display(C As String, S As Single, T As Single)
Dim totalComputersSold As Single
picBox.Print C, FormatPercent(S, 2), T
End Sub

Private Function Total(S As Single, T As Single)
T = totalComputersSold * S
End Function

Private Sub cmdDisplay_Click()
Dim C As String, S As Single
picBox.Cls
picBox.Print "Company", "Market", "Number"
picBox.Print "", "Share", "Sold"
picBox.Print
Call Data(C, S)
End Sub

Private Sub cmdExit_Click()
End
End Sub
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
It helps if you give a more detailed explanation of the problem. Error message, exact behavior, etc.

But WannaFly is right. As soon as the Load procedure executes, totalComputersSold no longer exists.
You are recreating a new variable with the same name in Sub Display, but it will not have a value in it (well, I think it defaults to 0).
Why does Sub Data have parameters? It doesn't seem that you are passing anything to it.
 

CCrunnernb

Golden Member
Sep 14, 2000
1,002
0
76
Honestly im quite confused with all of this... My program runs but when it prints T the value is always 0 when it should be a percentage of the 12000000.

Here are the directions I was given:

The 12,000,000 you use for the total number of computers sold should be assigned to a variable in a Form_Load event procedure. This variable should be declared as a single (the value 12,000,000 is too large to put into an Integer).

The total number of computers sold variable mentioned above will be the only form variable in your program. All other variables will be local variables.


Am I missing something?
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
Originally posted by: CCrunnernb
Am I missing something?
You mean like the posts above your last one?

I'm more than happy to help, but I'm not sure what you are asking. It doesn't seem like you even listened to what we said.

totalComputersSold needs to be declared outside of any functions or subs. That's what it means by a form variable. As you have it, it only exists within the individual function where the Dim statement is. So as soon as that function finishes, it is gone. You are creating a variable in the form load procedure, but it DOES NOT EXIST as soon as the form finishes loading.

You also have parameters in the Sub Data and I can't figure out why. You don't seem to be passing anything to that sub.

The Total function is never executed because you never call it.