Transformar en Función para varios TextBox

Esta

Sub Sleccionar_TextBox()
   On Error Resume Next
     nombretextbox = MPAG.txtmoc6(MPAG.txtmoc6.Text)
     nombretextbox = MPAG.txtmoc8(MPAG.txtmoc8.Text)
     'etc
valor = nombretextbox
If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
KeyAscii = 0 '<-- Esta linea borra la tecla presionada equivocadamente
MsgBox "Ingrese SOLO números en " & valor, vbOKOnly + vbInformation, Title:="CARACTER NULO"
End If
If Len(valor) = 12 Then KeyAscii = 0: MsgBox "MAX permitido en Telf 1 y 2, 12 dígitos": Exit Sub
'Para Guion
Select Case Len(valor)
Case 4
valor.Text = valor & "-"
End Select
End Sub
Para esto y mas si la necesidad lo ameita
'Solo números, guion y cantidad caracteres
Private Sub txtmoc6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Call Sleccionar_TextBox 'VAlor
        Call número
End Sub
Private Sub txtmoc8_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Call Sleccionar_TextBox 'VAlor
        Call número
End Sub

Feliz noche y mejor domingo.

1 respuesta

Respuesta
1

Me explicas con ejemplos.

Para no tener 2, 3 o 4 eventos en que la repetición de código es el mismo,

Private Sub txtmoc6_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
        Call número
    End If
'Cantidad caracteres
    If Len(Me.txtmoc6) = 12 Then KeyAscii = 0: MsgBox "MAX Permitido en Telf 1, 12 dígitos", vbOKOnly + vbInformation, Title:="MAX Cant.": Exit Sub
'Para Guion
    Select Case Len(txtmoc6)
    Case 4
        txtmoc6.Text = txtmoc6.Text & "-"
    End Select
End Sub
'-----------------------------------------------
Private Sub txtmoc8_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
        Call número
    End If
'Cantidad caracteres
    If Len(Me.txtmoc8) = 12 Then KeyAscii = 0: MsgBox "MAX permitido en Telf 2, 12 dígitos", vbOKOnly + vbInformation, Title:="MAX Cant.": Exit Sub
'Para Guion
    Select Case Len(txtmoc8)
    Case 4
        txtmoc8.Text = txtmoc8.Text & "-"
    End Select
End Sub
'--------------
este, ademas de los nombres, tambien el punto de colocacion del guion es diferente
Private Sub txtFFact_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
KeyAscii = 0 '<-- Esta linea borrar la tecla presionada equivocadamente
MsgBox "Ingrese SOLO números en Fecha Factura", vbOKOnly + vbInformation, Title:="CARACTER NULO"
End If
'Para Guion
Select Case Len(txtFFact)
Case 2, 5
txtFFact.Text = txtFFact.Text & "-"
End Select
If Len(txtFFact) = 10 Then KeyAscii = 0: MsgBox "MAX permitido en Fecha Factura, 10 dígitos": Exit Sub
End Sub

Si se puede, tener UN SOLO evento, llamese funcion o otro en que desde el evento KeyPress se llame a ese funcion y actue segun el TextBox en que se este introduciendo datos (numeros?.

En la pregunta te puse

Sub Sleccionar_TextBox()

pero no me sale bien

En los últimos eventos pusiste la llamada a la macro número:

Call número

Pero no pusiste el código, qué hace esa macro?, mejor pon el código.

Discúlpame, esa llamada no va, fue mi equivocación, elimina

La que si va es la llamada a la función o lo que decidas

OK es solamente este en virtud de que varios Cuiadros de texto tienen evento KeyPress para números

Sub número()
MsgBox "SOLO Permitido números, el guion entra automatico", vbOKOnly + vbInformation, Title:="CARACTER NULO"
End Sub

H o l a:

Te anexo la función para validar la longitud. Tienes que pasarle a la función 5 parámetros:

  1. El nombre del textbox
  2. El mensaje
  3. La cantidad de dígitos
  4. El keyascci (es la tecla que se presionó)
  5. El número de guiones: 1 ó 2 guiones
Function valida(wtext As MSForms. Control, mensaje, cantidad, keyascii, numguion)
'Por.Dante Amor 'Valida contenido de un textbox
    valida = True
    If Not (keyascii >= 48 And keyascii <= 57) Then valida = False
    'Cantidad caracteres
    If Len(wtext) = cantidad Then valida = False: MsgBox mensaje, vbInformation, "MAX Cant."
    'Para Guion
    Select Case numguion
        Case 1: If Len(wtext) = 4 Then wtext = wtext & "-"
        Case 2: If Len(wtext) = 2 Or Len(wtext) = 5 Then wtext = wtext & "-"
    End Select
End Function
'
Private Sub txtmoc6_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
    If valida(txtmoc6, "MAX Permitido en Telf 12 dígitos", 12, keyascii, 1) = False Then keyascii = 0
End Sub
Private Sub txtmoc8_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
    If valida(txtmoc8, "MAX Permitido en Telf 12 dígitos", 12, keyascii, 1) = False Then keyascii = 0
End Sub
Private Sub txtFFact_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
    If valida(txtFFact, "MAX permitido en Fecha Factura, 10 dígitos", 10, keyascii, 2) = False Then keyascii = 0
End Sub

Sal u dos

Buen día Dante

Perdona mi ignorancia; ¿Donde coloco la llamada a Call número, es decir, este mensaje aviso

MsgBox "SOLO Permitido números, el guion entra automatico", vbOKOnly + vbInformation, Title:="CARACTER NULO"

Dentro de la funcion?

Metí Call numero no en la función, si en código del control pero, cada numero o dígito que introduzco, me avisa y no debe hacerlo así

LA deje así por l ode los mensajes

Function valcaracter(wtext As MSForms.Control, mensaje, cantidad, keyascii, numguion)
'Por.Dante Amor 'Valida contenido de un textbox
    valcaracter = True
    If Not (keyascii >= 48 And keyascii <= 57) Then valcaracter = False: MsgBox "Solo permitdo números ", 64, "Tipo de Caracteres"
    'Cantidad caracteres
    If Len(wtext) = cantidad Then valcaracter = False: MsgBox mensaje, 64, "MAX Cantidad."
'MsgBox "SOLO Permitido números, el guion entra automatico", vbOKOnly + vbInformation, Title:="CARACTER NULO"
        'MsgBox "El " & num & " no contiene los dígitos completos", 64, ""
    'Para Guion
    Select Case numguion
    'Colocar el # de Case según el orden de cada control en el formulario
        Case 1: If Len(wtext) = 4 Then wtext = wtext & "-"
        Case 2: If Len(wtext) = 2 Or Len(wtext) = 5 Then wtext = wtext & "-"
    End Select
End Function

Pregunto:

Cada punto para el guion, una línea de Case más el # del

keyascii, 3

o el que correspopnda al Case ¿Cierto? Aun este el control en otro formulario,

Ejemplo:

        Case 1: If Len(wtext) = 4 Then wtext = wtext & "-"
        Case 2: If Len(wtext) = 2 Or Len(wtext) = 5 Then wtext = wtext & "-"
        Case 3: If Len(wtext) = 4 Then wtext = wtext & "-"
y en el control
    If valcaracter(TextBox1, " xxxxx, 8 dígitos", 8, keyascii, 3) = False Then keyascii = 0

¿Estoy en lo correcto?

Mencionaste que la llamada a número no era necesaria.

La función solamente funciona para 1 o para 2 guiones.

No modifiques la función, ya funciona para lo que pediste.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas