Llamar sub (de un command button) a una acción de un textbox
Estoy tratando de llamar una public sub de un command button cuando se presiona la tecla "Enter", pero en vez de hacer los procesos de la "public sub", directamente se posiciona en el siguiente textbox.
Les dejo la imagen del userform.

La idea es la siguiente: quiero poder hacer que al momento de presionar la tecla "enter", y si el textbox "número de cliente" esta vacío, que llame la public sub asociado al command button "cargar datos automaticamente".
Dicha public sub, hace que, si no hay datos en el textbox, aparezca en el label oculto "numero de cliente invalido".
Pero como comente anteriormente, al presionar "enter", directamente se posiciona en el siguiente textbox (en este caso "nombre:").
Les dejo los codigos:
Private Sub num_client_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If (KeyAscii >= 48 And KeyAscii <= 57) And KeyAscii = 13 Then KeyAscii = KeyAscii Else KeyAscii = 0 End If If KeyAscii = 13 Then Call Cargar_num_client_Click End If End Sub
Public Sub Cargar_num_client_Click()
Dim num_cli_buscado As Variant
Dim nombre As String
Dim direccion As String
Dim telefono As String
Dim rango As Range
Application.ScreenUpdating = False
Sheets("Clientes").Unprotect "lukithas"
On Error GoTo ErrorHandler
Set rango = Sheets("Clientes").Range("A1").CurrentRegion
num_cli_buscado = Me.num_client.Value
If IsNumeric(num_cli_buscado) Then
num_cli_buscado = CDbl(num_cli_buscado)
End If
nombre = Application.WorksheetFunction.VLookup(num_cli_buscado, rango, 3, 0)
direccion = Application.WorksheetFunction.VLookup(num_cli_buscado, rango, 6, 0)
telefono = Application.WorksheetFunction.VLookup(num_cli_buscado, rango, 4, 0)
If nombre = "" Then
num_client.Text = ""
client_txtbox1.Text = ""
client_txtbox2.Text = ""
client_txtbox3.Text = ""
form_envios_numcli_mensaje.Caption = "Este número de cliente no tiene datos asignados."
form_envios_numcli_mensaje.Visible = True
num_client.SetFocus
Else
With Me
.client_txtbox1.Value = nombre
.client_txtbox2.Value = direccion
.client_txtbox3.Value = telefono
.form_envios_numcli_mensaje.Visible = False
End With
End If
Sheets("Clientes").Protect "lukithas"
Application.ScreenUpdating = True
Exit Sub
'
'De haberse encontrado un error mostramos mensajes.
ErrorHandler:
If Err.Number = 1004 Then
With Me
.form_envios_numcli_mensaje.Caption = "Número de cliente inválido."
.form_envios_numcli_mensaje.Visible = True
.num_client.Text = ""
.client_txtbox1.Text = ""
.client_txtbox2.Text = ""
.client_txtbox3.Text = ""
.num_client.SetFocus
End With
Else
MsgBox "Ha ocurrido un error: " & Err.Description, vbExclamation, Titulo
End If
End Sub
1 Respuesta
Respuesta de lucas niconini
1