VB6 Temp Conversion Tool

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
I have written this conversion tool.I know it is wrong somewhere does anyone know where I am going wrong in this code.
Its supposed to convert a temperature given by the user either from Celsius to Fahrenheit or from Fahrenheit to Celsius.
This bit is especially annoying, the user is supposed to indicate which scale they wish their entry to be converted to.
This is what ive attempted.
Option Explicit

Private Sub cmdConvert_Click()
'Declare required storage space
Dim Fahrenheit As Integer
Dim Celsius As Integer
Dim F As Integer
Dim C As Integer


'Invite user to input temp
Fahrenheit = InputBox("Please input Temp in F for conversion to C ", "Temperature")
Celsius = InputBox("Please input Temp in C for conversion to F ", "Temperature")
F = Fahrenheit
C = Celsius

If Fahrenheit Then
picMessage.Print "The conversion in celsius is "; (5 / 9) * (F - 32)
If Celsius Then
picMessage.Print "The conversion in fahrenheit is "; 32 + (9 / 5) * C
End If
End If
End Sub

Thanks Charlie
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about sticking a couple of optionbuttons on your form, naming them optF and optC and using the following (change the MsgBox statements to your picMessage.Prints): -
<pre>
Private Sub cmdConvert_Click()
Dim F As Integer
Dim C As Integer

If optF Then
F = Application.InputBox("Please input Temp in F for conversion to C ", _
"Temperature", Type:=1)
If F Then MsgBox "The conversion in celsius is " & (5 / 9) * (F - 32)
Exit Sub
End If

If optC Then
C = Application.InputBox("Please input Temp in C for conversion to F ", _
"Temperature", Type:=1)
If C Then MsgBox "The conversion in fahrenheit is " & 32 + (9 / 5) * C
Exit Sub
End If

End Sub
</pre>
 
Upvote 0
Thanks its a good idea but I only wanna use 2 buttons one called convert and an exit.
I think I might leave it as it is for the moment until I can think of a neat way of giving a choice.
So you think my other code is OK then.
I was sure it was wrong somehow.

Charlie
 
Upvote 0
So you think my other code is OK then.
I was sure it was wrong somehow.

Only in that you don't give the user an option to choose what conversion they require. Also, try typing in a letter or pressing Cancel on one of the inputboxes and see what happens...

Have a look at the InputBox method (as opposed to function)- you can validate the box to only accept numbers. (Come to think of it I should have declared both F and C as Doubles...)
 
Upvote 0
My APPS always seem to have that problem with the cancel button.
I was wondering what that was last week.
It gives an error right?
End Debug etc.
Charlie
 
Upvote 0
Pressing Cancel on an inputbox returns an empty string (or False with the InputBox method), so you can test for it and either ignore it (eg If F<> "" Then...) or loop until someone puts something in: -

eg<pre>
Dim Answer As String

Do
Answer = InputBox("Enter a word")
Loop Until Answer<> ""</pre>
This message was edited by Mudface on 2002-10-18 09:20
 
Upvote 0

Forum statistics

Threads
1,202,987
Messages
6,052,939
Members
444,616
Latest member
novit19089

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top