Ignorar un campo de texto vacío

Ignorar un campo de texto vacío

ingrese la descripción de la imagen aquí

Sigo recibiendo una excepción de conversión cuando ejecuto una subrutina en un formulario de usuario. Reconoce el valor nulo cuando lo lee txtMileage.text = ""y arroja un error porque intenta determinar si el valor es mayor que 300.

If btnYes.Checked = True And txtMileage.Text > 300 Then MsgBox("Distance Exceeds 300 Miles") txtMileage.Focus() Exit Sub

Necesito que ignore la verificación del valor cuando btnNo.checked = trueytxtMileage.text = ""

¿Alguna idea?

Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdCalculate.Click
    If btnNo.Checked = False And btnYes.Checked = False Then
        MsgBox("Please select yes or no")
        Exit Sub
    End If
    If btnYes.Checked = True And txtMileage.Text = "" Then
        MsgBox("Please instert Mileage")
        txtMileage.Focus()
        Exit Sub
    End If
    If btnNo.Checked = True And txtMileage.Text = "" Then

    End If
    If btnYes.Checked = True And txtMileage.Text > 300 Then
        MsgBox("Distance Exceeds 300 Miles")
        txtMileage.Focus()
        Exit Sub
    End If
End Sub

actualizado con mensaje de error, todavía lo arroja a

If btnYes.Checked = True And txtMileage.Text = "" Then MsgBox("Please insert Mileage") txtMileage.Focus() Exit Sub ElseIf btnYes.Checked = True And txtMileage.Text > 300 Then MsgBox("Distance Exceeds 300 Miles") txtMileage.Focus() Exit Sub End If

ElseIf btnYes.Checked = True And txtMileage.Text > 300 Thenparece ser la línea problemática al finalThen

Respuesta1

¿Qué tal si simplemente verificamos si .Textcontiene un valor numérico y, si no es así, lo configuramos como 0?

If Not IsNumeric(txtMileage.Text) Then txtMileage.Text = 0

Es posible que desee o no almacenarlo como una variable para evitar cambiar el control.

Dim txt as Variant
txt = txtMileage.Text
If Not IsNumeric(txt) Then txt  = 0

Respuesta2

Ponlo como opción excluyente a través elseifde txtMileage.Text = "". No habrá posibilidad de que haga la verificación if >300cuando esté vacío.

Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdCalculate.Click
    If btnNo.Checked = False And btnYes.Checked = False Then
        MsgBox("Please select yes or no")
        Exit Sub
    End If
    If btnYes.Checked = True And txtMileage.Text = "" Then
        MsgBox("Please instert Mileage")
        txtMileage.Focus()
        Exit Sub
    Elseif  btnYes.Checked = True And txtMileage.Text > 300 Then
        MsgBox("Distance Exceeds 300 Miles")
        txtMileage.Focus()
        Exit Sub
    End If
    If btnNo.Checked = True And txtMileage.Text = "" Then

    End If
End Sub

información relacionada