View Full Version : visual basic, help

01-31-2005, 09:37 PM
o.k i have this peice of code, what i am trying to achieve is a program that you can enter a number into a text box select using radio buttons which one to times it by and then click a button and it happens. i am creating it for a petrol station. I have a box where i input a number then i have to radio buttons one that says unleaded and one that says diesel now, i can't seem to figure out how to get the code to know which radio button has been selected. help? i know that everything isn't perfect on there but i am working on it. Also i want to click a button and the total to appear in a text box i can get it to appear but 0 is shown and when i click a radio button and input a number nothing happens.

'Creating variables for working out the fuel cost
Dim litres As String

Dim TotalDiesel As String
'variables for unleaded
Dim Unleaded1 As String
Dim Totalunleaded As String

Private Sub cmdTotal_click()

'what ever is in the text box will be stored in the string
strliters = Txtlitres

'If diesel is selected then
If Diesel Then

strTotalDiesel = strlitres * 80

TxtOutput.Text = strTotalDiesel


Strtotalunleaded = strlitres * 90

TxtOutput.Text = Strtotalunleaded

End If

End Sub

02-01-2005, 08:44 AM
The reason it's not working is because your values are strings. You can't perform arithmetic operations on a string. Declare all you values as integers instead of strings.

When reading from the text box and assigning the values, use cint(txtlitres) ... to convert to integer before assigning to a value. When you want to display the value again, use cstr(value) to convert the value to a string.
That should fix the problem

02-01-2005, 09:27 AM
You really need to put option explicit in your general declarations. And as tboss said, you can't declare anything thats going to perform an arithmetic operation a string. Declare them a int, single, double, whichever you need. In your code, your not using the variable names as you declared them. They have to be exactly the same as in your Dim statement.

Private Sub Command1_Click()
Dim litres As Single

Dim TotalDiesel As Single
'variables for unleaded
Dim Unleaded1 As Single
Dim Totalunleaded As Single

litres = Val(Text1.Text)

'If diesel is selected then
If Diesel Then

TotalDiesel = litres * 80

txtOutput.Text = TotalDiesel


Totalunleaded = litres * 90

txtOutput.Text = Totalunleaded

End If

End Sub

...You get the idea

02-02-2005, 04:50 PM
o.k i started again and this is what i have

'Shows the cost of diesel
Private Const curDiesel As Single = 0.794
'Shows the cost of unleaded
Private Const curUnleaded As Currency = 0.8
'Shows the cost of unleaded 95
Private Const curunleaded95 As Currency = 0.9
'Holds the number that was entered into textLitres
Dim sngFuel As Single
'holds the answer if diesel is selected
Dim DieselFuel As Single
'Hold the answer if unleaded is selected
Dim UnleadedFuel As Single
'Hold the answer if unleaded95 is selected
Dim Unleaded95Fuel As Single

Private Sub cmdTotal_Click()

'If diesel is selected
If optDiesel = True Then

curDiesel * sngFuel = sngDieselFuel

txtTotal = sngDieselFuel

End If

' if unleaded is selected
If optUnleaded = True Then

curUnleaded * sngFuel = sngUnleadedFuel

txtTotal = sngUnleadedFuel

End If

' if Unleaded95 is selected
If optUnleaded95 = True Then

curUnleaded95 * sngFuel = sngUnleaded95Fuel

txtTotal = sngUnleaded95Fuel

End If

End Sub

how ever all the calculations are in red, so does anyone have any ideas?

02-03-2005, 04:40 AM
OK...did you read my post at all? Your variable names have to be the exact same as when you declare them. You assigment statements are backwards...

Dim DieselFuel As Single
curDiesel * sngFuel = sngDieselFuel 'Wrong...
DieselFuel = curDiesel * sngFuel 'Right...Notice the DieselFuel..not sngDieselFuel

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum