ayuda con validacion
a ver si alguine sabe donde tengo mal la validacion, necesito que el textbox solo acepte numeros y no letras no se si tenga error al poner mal la condicion, les paso el padazo de codigo
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
Dim cvalor As Integer
If Not IsNumeric(Text1(Index).Text) Then
Beep
KeyAscii = 0
Else
If KeyAscii = vbKeyReturn Then
Dim size As Variant
size = Len(Text1(Index))
Dim rango As Long
rango = Val(Text1(Index))
If (rango >= 0 And rango <= 255) Then
If size = 0 Then
Text1(Index) = "000"
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
If size = 1 Then
Text1(Index) = "00" & Text1(Index)
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
If size = 2 Then
Text1(Index) = "0" & Text1(Index)
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
Text1(Index).Appearance = 0
Text1(Index).BackColor = &H8000000F
Text1(Index).BorderStyle = 0
If Text2(Index).Enabled = True Then
Text2(Index).SetFocus
Text2_Click (Index)
End If
Else 'RANGO
MsgBox "EL VALOR DEBE DE ESTAR ENTRE 000 Y 255", vbCritical, "ERROR"
Text1_Click (Index)
End If ' RANGO
End If 'KEYASCCI
End If
End Sub
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
Dim cvalor As Integer
If Not IsNumeric(Text1(Index).Text) Then
Beep
KeyAscii = 0
Else
If KeyAscii = vbKeyReturn Then
Dim size As Variant
size = Len(Text1(Index))
Dim rango As Long
rango = Val(Text1(Index))
If (rango >= 0 And rango <= 255) Then
If size = 0 Then
Text1(Index) = "000"
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
If size = 1 Then
Text1(Index) = "00" & Text1(Index)
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
If size = 2 Then
Text1(Index) = "0" & Text1(Index)
cvalor = Text1(Index)
'prueba(Index - 1) = cvalor
End If
Text1(Index).Appearance = 0
Text1(Index).BackColor = &H8000000F
Text1(Index).BorderStyle = 0
If Text2(Index).Enabled = True Then
Text2(Index).SetFocus
Text2_Click (Index)
End If
Else 'RANGO
MsgBox "EL VALOR DEBE DE ESTAR ENTRE 000 Y 255", vbCritical, "ERROR"
Text1_Click (Index)
End If ' RANGO
End If 'KEYASCCI
End If
End Sub
Mete esto en un modulo:
Public Function SoloNumeros(ByRef nChar As Integer) As Integer
'Solo Enteros
If (nChar >= 48 And nChar <= 57) Or nChar = 8 Or nChar = 13 Then
Solonumeros= nChar
Else
solonumeros= 0
End If
end function
Llamalo asi:
Private Sub text1_KeyPress(KeyAscii As Integer)
KeyAscii = SoloNumeros(KeyAscii)
End Sub
Public Function SoloNumeros(ByRef nChar As Integer) As Integer
'Solo Enteros
If (nChar >= 48 And nChar <= 57) Or nChar = 8 Or nChar = 13 Then
Solonumeros= nChar
Else
solonumeros= 0
End If
end function
Llamalo asi:
Private Sub text1_KeyPress(KeyAscii As Integer)
KeyAscii = SoloNumeros(KeyAscii)
End Sub
