Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate in VB6

Status
Not open for further replies.

Dreamboat

Instructor
Nov 7, 1999
4,881
US
I need to have a popup that asks for width, height, and length, which I will then use to calculate its cubic measurement. The formula is (w x h x l) / 1728.

Not a difficult task, I'm sure, unless you're new to VB like me. I can create the form, and get a prompt on the first one, but after that I'm kinda lost. A little direction would probably be better than a direct answer. [sig]<p> <br><a href=mailto:techsupportgirl@home.com>techsupportgirl@home.com</a><br><a href= </a><br> [/sig]
 
Usually you just put 3 text boxes on your form and label one Height, one width and one Length.

then put a forth box and label it Answer or SqFt

Then add a button. In the button click event (just double click the button), have your formula.

If you really want it to prompt users then have 3 Inputboxes in a row.

Dim L, W, H, A As Variant
L = InputBox(&quot;Enter the Length&quot;, &quot;Key the length&quot;, 1)
W = InputBox(&quot;Enter the Width&quot;, &quot;Key the Width&quot;, 1)
H = InputBox(&quot;Enter the Height&quot;, &quot;Key the Height&quot;, 1)
A = L * W * H
MsgBox &quot;The Answer is &quot; & A, vbInformation, &quot;Answer&quot;
[sig]<p>DougP, MCP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br> Ask me how Bar-codes can help you be more productive.[/sig]
 
Thank you soooo much, Doug. I used to be here a lot, but I've dropped out for about six months. Answering questions at too many tech sites can be a bit overwhelming. But I'm back because of you. I very rarely am the asker, but VBA and VB6 are new to me.

I'll give the code a try and let you know how it goes. Thanks again. [sig]<p> <br><a href=mailto:techsupportgirl@home.com>techsupportgirl@home.com</a><br><a href= </a><br> [/sig]
 
Okay. Here's my code:

Dim L, W, H, A As Variant
Private Sub Text1_Change()
L = Text1.Name
End Sub

Private Sub Text2_Change()
W = Text2.Name
End Sub

Private Sub Text3_Change()
H = Text3.Name
End Sub
Private Sub Command1_Click()
A = L * W * H / 1728
A = Text4.Name
End Sub

But I must be doing something wrong...
[sig]<p> <br><a href=mailto:techsupportgirl@home.com>techsupportgirl@home.com</a><br><a href= </a><br> [/sig]
 
Dreamboat..
Declaring a variable as a Variant is rarely the best thing to do
Dim L, W, H, A As Variant
L,W,H as declared as Variant by default only A is declared as a Variant explicitly, this is a fairly common mistake if you wanted to declare them as Variants you would have to do this
Dim L as Variant, Dim W as Variant..etc
What you need to do is figure out what kind of numerical presicion you actualy need, probally Single or Double.


Private Sub Text1_Change()
L = Text1.Name
End Sub


This will not work because the Name property of the Text box is read only at runtime, what you should be useing is the Text property of the Textbox, Like this

Private Sub Text1_Change()
L = Text1.Text
End Sub


Private Sub Command1_Click()
A = L * W * H / 1728
A = Text4.Name
End Sub


This will not work because, Your useing the Name property again in the line A = Text4.Name and , you want to assign the Value of the variable &quot;A&quot; to the Property &quot;Text&quot; of the TextBox, Text4 so it should be Text4.Text = A

So..The whole thing would look like this

Dim L As Single, W as Single, H As Single, A As Single
Private Sub Text1_Change()
L = Text1.Text
End Sub

Private Sub Text2_Change()
W = Text2.Text
End Sub

Private Sub Text3_Change()
H = Text3.Text
End Sub
Private Sub Command1_Click()
A = (L * W * H )/ 1728
Text4.Text = A
End Sub


This,while not the best way will work, What you are lacking is error checking, for example there is no code to insure that only a valid numeric value is inputed into the TextBoxes. If you want some more help feel free to e-mail me at op1942@zianet.com

Collin [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top