Beruflich Dokumente
Kultur Dokumente
rating = Val(txtRating.Text)
Select Case rating
Case Is > 4
bonus = 0.150
Case 4, 3
bonus = 0.1
Case 2
bonus = 0.075
Case 1
bonus = 0.05
Case Else
bonus = 0
End Select
Enunciado Select Case (cont.)
Dim age As Integer
age = Val(txtAge.Text)
Select Case age
Case Is < 21
MsgBox("You are a minor.")
Case 21 To 64
MsgBox("You are an adult.")
Case Is > 64
MsgBox("You are a senior.")
End Select
Validacin de datos
Existen dos formas principales para
asegurarse de que un valor entrado es
adecuado.
Reaccin: recoger el valor y usar el enunciado
If..Then para revisarlo.
Prevencin: evitar que se pueda entrar un
valor inadecuado. Esto es lo preferible.
Validacin de datos (cont.)
Ejemplo de reaccin (usando un text box):
If txtSex.Text = "M" Then
MsgBox("You are a man.")
ElseIf txtSex.Text = "F" Then
MsgBox("You are a woman.")
Else
MsgBox("Input Error.", vbExclamation)
txtSex.Focus()
End If
Ejemplo de prevencin (usando dos radio buttons):
If radMasculine.Checked Then
MsgBox("You are a man.")
Else
MsgBox("You are a woman.")
End If
Validacin de datos (cont.)
Una funcin que ayuda a validar un valor
numrico en un text box es IsNumeric().
If IsNumeric(txtAge.Text) = False Then
MsgBox("Input Error.", vbExclamation)
txtAge.Focus()
Else
age = Val(txtAge.Text)
If age < 0 Or age > 120 Then
MsgBox("Input Error.", vbExclamation)
txtAge.Focus()
Else
...
End If
End If
Validacin de datos (cont.)
Otra forma de validar es previniendo que se entre un
caracter inadecuado en el text box mediante el
evento KeyPress.
Este evento ocurre despus de presionar una tecla
pero antes de mostrar el caracter en pantalla.
En este evento, el parmetro e contiene dos
propiedades importantes:
KeyChar contiene el cdigo Ascii del caracter presionado
Handled True para ignorar el caracter presionado
Validacin de datos (cont.)