ユーザーフォームでサブルーチンを実行すると、キャスト例外が発生し続けます。読み取り時に null 値を認識し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
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