CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    1

    vb6 conversion program problem

    hi first of all i would like to state i am currently at college, ive been trying to get the isNumeric function to work with my Euro to pounds conversion. i can get it to work with my pounds to euro conversion, but even when i try to input numbers into the field when i have the euro to pounds option selected it says "please enter a valid number" (the message i have inputed when a user trys to input anything other than a number). heres my code, any help is much appreciated.

    Code:
     
    Option Explicit
    Dim euroToSterlingRate As Currency 'declares the variable "euroToSterlingRate" as currency
    Dim sterlingToEuroRate As Currency 'declares the variable "sterlingToEuroRate" as currency
    Private Sub cmdChangeEuroToPoundRate_Click()
    Dim EuroToPoundanswer As String 'declares the variable "EuroToPOundanswer as a string
    'when the user presses the button a input box apears.
    'when the user inputs a number into this input box and presses "ok",
    'it changes the euro to pound conversion rate to the entered amount.
    EuroToPoundanswer = InputBox("Enter the current Euro to pound coversion rate:")
    If EuroToPoundanswer <> "" Then
     euroToSterlingRate = EuroToPoundanswer
     End If
    End Sub
    Private Sub cmdChangePoundToEuroRate_Click()
    Dim answer As String 'declares the variable "answer" as a string
    'when the user presses the button a input box apears.
    'when the user inputs a number into this input box and presses "ok",
    'it changes the pound to euro conversion rate to the entered amou  nt.
    answer = InputBox("Enter the current Pound to Euro conversion Rate: ")
    If answer <> "" Then
        sterlingToEuroRate = answer
    End If
    End Sub
    
    Private Sub cmdClear_Click()
    'when the user clicks this button the "txtConversionAmount" field is cleared
    txtConversionAmount = ""
    End Sub
    
    Private Sub cmdConvert_Click()
    'this button starts the conversion and displays the answer in a message box
    
    If optPoundsToEuro.Value = True Then 'begins the if stament
     Dim ConvertAmount As Single 'declares the variable "ConvertAmount" as single
     Dim PoundtoEuroAnswer 'this declares the variable "PoundToEuroAnswer"
     Dim userInput As String 'defines the variable "userInput" as string
     userInput = txtConversionAmount.Text
     If IsNumeric(userInput) Then
     ConvertAmount = txtConversionAmount 'declares that the variable ConvertAmount=txtConversionAmount
      'declares that the variable "PoundToEuroAnswer",
     '="ConvertAmount" Multiplied by "sterlingToEuroRate".
     PoundtoEuroAnswer = ConvertAmount * sterlingToEuroRate
     'displays the answer of the amount converted into euros,
     'followed by the word "euros".
      MsgBox (PoundtoEuroAnswer & " euros")
    Else
    MsgBox ("please enter a valid number")
    End If 'ends the if statemant
    End If
    
    
    If optEurosToPounds.Value = True Then
     Dim EuroToPoundanswer 'this delcares the variable "EuroToPoundAnswer"
     '="ConvertAmount" Multiplied by "eurotoSterlingRate"
     If IsNumeric(userInput) Then
     EuroToPoundanswer = ConvertAmount * euroToSterlingRate
     MsgBox (EuroToPoundanswer & " pounds")
    Else
    MsgBox ("please enter a valid number")
    End If
    End If
    End Sub
    
    Private Sub cmdEuroToPoundCurrentRate_Click()
    'this button displays the current Euro to pound conversion rate in a message box
    MsgBox ("The Current Euro to Pound Conversion rate is: " & euroToSterlingRate)
    End Sub
    
    Private Sub cmdExit_Click()
    'this button ends the program
    End
    End Sub
    
    Private Sub cmdPoundToEuroCurrentRate_Click()
    'this button displays the current pound to euro conversion rate in a message box
    MsgBox ("The Current Pound to Euro Conversion Rate is " & sterlingToEuroRate)
    End Sub
    Private Sub Form_Load()
    euroToSterlingRate = 2.7 'default value
    sterlingToEuroRate = 4.9 'default value
    End Sub

  2. #2
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Re: vb6 conversion program problem

    Try stepping through the code 'F8'. I think you will see where the problem is!

  3. #3
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: vb6 conversion program problem

    I believe your problem is that you define USERINPUT on a condition - Variables should be defined at athe top od the procedure (1st thing that happens)

    Code:
    Private Sub cmdConvert_Click()
    'this button starts the conversion and displays the answer in a message box
    
    If optPoundsToEuro.Value = True Then 'begins the if stament
     Dim ConvertAmount As Single 'declares the variable "ConvertAmount" as single
     Dim PoundtoEuroAnswer 'this declares the variable "PoundToEuroAnswer"
     Dim userInput As String 'defines the variable "userInput" as string
     userInput = txtConversionAmount.Text
     If IsNumeric(userInput) Then
     ConvertAmount = txtConversionAmount 'declares that the variable ConvertAmount=txtConversionAmount
      'declares that the variable "PoundToEuroAnswer",
     '="ConvertAmount" Multiplied by "sterlingToEuroRate".
     PoundtoEuroAnswer = ConvertAmount * sterlingToEuroRate
     'displays the answer of the amount converted into euros,
     'followed by the word "euros".
      MsgBox (PoundtoEuroAnswer & " euros")
    Else
    MsgBox ("please enter a valid number")
    End If 'ends the if statemant
    End If

    This means that if the condition is not met then USERINPUT is NOT DEFINED !

    Consequently things wont work as you expect

    In addition, a definition like

    Code:
    Dim PoundtoEuroAnswer
    Is leading to problems- you defined a variant - which means it can be anything

    Why dont you either -

    Take all the DIM Statements out of the body and place them at the top of each Procedure (SUB)

    or

    Please them all at the top of your program as

    Option Explicit '(At the very top to make sure you define all your variables)

    Private USERINPUT as STRING
    Private ConvertAmount As Single
    etc

    I am sure you have seen other people's programs - you have just messed up on the placement of your Variable definitions

    If this doesnt make sense then download any number of programs from www.PlanetSourceCode.com and have a look

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured