在使用者窗體中執行子程式時,我不斷收到強制轉換異常。它在讀取時識別空值txtMileage.text = ""
並拋出錯誤,因為它試圖確定該值是否大於 300
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
把它當作exluluty選項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