Hello,
Im new to VB and therefore i require some guidance in this tricky situation that im stuck in.
Basically, what my program does is calculate the mach number of an aircraft when the speed of sound and value of temperature are added into the text box. The problem arises in 2 places. One, the program calculates the value of speed wrongly. And secondly (The major part) is that i want the program to display an error that i should enter both the values (that is, if only one value is entered and calculate button is pressed.)and if i enter any alphabets, it should give an error to please input numbers.
Ill be really greatful if anyone can help me out on this. Thanks.
Here's the code.

Public Class mach

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim t As Double
        Dim v As Double
        t = CDbl(TextBox1.Text)
        v = CDbl(TextBox2.Text)
        Dim R As Integer = 287
        Dim Gamma As Integer = 1.4

        Dim x As Double = 287 * 1.4 * t
        Dim speed As Double = Math.Sqrt(x)
        Dim mach As Double = v / speed

        If TextBox1.Text <= 270 Then

            MsgBox("Flight in these conditions is impossible. Please enter the value of Temperature Greater than 270K")

        ElseIf mach <= 0.3 Then

            TextBox3.Text = speed
            TextBox4.Text = mach
            TextBox5.Text = "The Flight is Incompressible and Subsonic"

        ElseIf mach > 0.3 And mach <= 1 Then

            TextBox3.Text = speed
            TextBox4.Text = mach
            TextBox5.Text = "The Flight is Compressible and Subsonic"

        ElseIf mach > 1 And mach <= 5 Then

            TextBox3.Text = speed
            TextBox4.Text = mach
            TextBox5.Text = "The Flight is Supersonic"

        ElseIf mach > 5 Then

            TextBox3.Text = speed
            TextBox4.Text = mach
            TextBox5.Text = "The Flight is Hypersonic"
            Exit Sub
        End If

    End Sub
End Class

Recommended Answers

All 5 Replies

when u click on the calculate button make sure that both the text boxes are not empty...if so prompt the error....

if textbox1.Text="" or Textbox2.Text="" then
    Msgbox("Mandatory Info")
else
    'ur calculation code
End If

below code u can do for entering only numbers....it has to be written in ur textbox key press event

If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
      e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
      e.Handled = False
End If

What does ur textbox3, textbox4 and textbox5 hold???

You should check if the entry fields are numeric. Also, you can simplify the if-elseif block as follows

If Not IsNumeric(TextBox1.Text) Then
    MsgBox("please enter a numeric value in textbox1")
    Exit Sub
End If

If Not IsNumeric(TextBox2.Text) Then
    MsgBox("please enter a numeric value in textbox2")
    Exit Sub
End If

.
.
.

If t <= 270.0 Then
    MsgBox("Flight in these conditions is impossible. Please enter the value of Temperature Greater than 270K")
ElseIf mach <= 0.3 Then
    .
    .
    .
ElseIf mach <= 1.0 Then
    .
    .
    .
ElseIf mach <= 5.0 Then
    .
    .
    .
Else
    .
    .
    .
End If

Note that in the first "If" I compare t to 270.0 rather than TextBox1.Text. The way you are doing it compares a numeric value to a string value which will not give you the results you want.

Thank You! That helped

How to get data from access into textboxes in vb6?

@rakesh - please start a new thread instead of hijacking this thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.