빈 텍스트 필드 무시

빈 텍스트 필드 무시

여기에 이미지 설명을 입력하세요

사용자 양식에서 서브루틴을 실행할 때 계속 캐스트 예외가 발생합니다. txtMileage.text = ""값이 300보다 큰지 확인하려고 하기 때문에 읽고 오류가 발생하면 null 값을 인식합니다.

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

언제 값 확인을 무시해야합니까 btnNo.checked = true?txtMileage.text = ""

이견있는 사람?

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

오류 메시지로 업데이트되었지만 여전히 발생합니다.

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마지막에 문제가 있는 것 같습니다Then

답변1

.Text숫자 값이 포함되어 있는지 확인하고, 포함되어 있지 않으면 로 설정하는 것은 어떻습니까 0?

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

컨트롤 변경을 방지하기 위해 변수로 저장할 수도 있고 저장하지 않을 수도 있습니다.

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

답변2

elseif를 통해 독점 옵션으로 설정하십시오 txtMileage.Text = "". if >300비어 있을 때 검사를 수행할 가능성은 없습니다 .

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

관련 정보