Continuo recebendo uma exceção de conversão ao executar uma sub-rotina em um formulário de usuário. Ele reconhece o valor nulo quando lê txtMileage.text = ""
e gera um erro porque está tentando determinar se o valor é maior que 300
If btnYes.Checked = True And txtMileage.Text > 300 Then MsgBox("Distance Exceeds 300 Miles") txtMileage.Focus() Exit Sub
Eu preciso ignorar a verificação do valor quando btnNo.checked = true
etxtMileage.text = ""
Alguma ideia?
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
atualizado com mensagem de erro, ele ainda lanç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 Then
parece ser a linha do problema no últimoThen
Responder1
Que tal apenas verificar se .Text
contém um valor numérico e, caso não contenha, configurá-lo como 0
?
If Not IsNumeric(txtMileage.Text) Then txtMileage.Text = 0
Você pode ou não querer armazená-lo como uma variável para evitar alterar o controle.
Dim txt as Variant
txt = txtMileage.Text
If Not IsNumeric(txt) Then txt = 0
Responder2
Coloque-o como opção exclusiva através elseif
de txtMileage.Text = ""
. Não haverá chance de fazer a verificação if >300
quando estiver vazio.
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