Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    visual basic, help

    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.

    Code:
    '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
    
    Else
    
    Strtotalunleaded = strlitres * 90
    
    TxtOutput.Text = Strtotalunleaded
    
    
    End If
    
    
    End Sub

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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.
    Code:
    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
    
    Else
    
    Totalunleaded = litres * 90
    
    txtOutput.Text = Totalunleaded
    
    
    End If
    
    End Sub
    ...You get the idea
    Last edited by Brandoe85; 02-01-2005 at 08:29 AM.

  • #4
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    o.k i started again and this is what i have
    Code:
    '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?

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    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...
    Code:
    Dim DieselFuel As Single
    curDiesel * sngFuel = sngDieselFuel   'Wrong...
    DieselFuel = curDiesel * sngFuel        'Right...Notice the DieselFuel..not sngDieselFuel


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •