VbMarcar error si no esta el registro en la base vba

Tengo un checkbox, un comboBox que al momento de pucharle al checkbox puedo buscar en el comboBox el id en la base de excel, y me trae los datos en los textBox.

Me gustaría que cuando ingrese un id que no exista en la base me mande mensaje de que el id no existe, y no que me detenga la aplicación.

Les dejo mi código abajo

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ComboBox1.Enabled = True
cargarfolio
Else
ComboBox1.Enabled = False
ComboBox1.Clear
End If
End Sub

Private Sub ComboBox1_Change()
Dim var2 As String
If ComboBox1 = "" Then
Else
Editar.Locked = False
Sheets("Formulario").Activate
If ComboBox1 = Empty Then
MsgBox "Para modificar primero id", vbInformation, "Almacen"
ComboBox1.ListIndex = 0
ComboBox1.SetFocus
End If
var2 = ComboBox1.Column(0)
Cells.Find(what:=ComboBox1.Value, After:=ActiveCell, LookIn:=xlFormulas, lookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate

If var2 = ActiveCell Then
TextBox1 = ActiveCell.Value
TextBox2 = ActiveCell.Offset(0, 1)
TextBox3 = ActiveCell.Offset(0, 2)
ComboBox2 = ActiveCell.Offset(0, 3)
TextBox5 = ActiveCell.Offset(0, 4)
TextBox6 = ActiveCell.Offset(0, 5)
TextBox7 = ActiveCell.Offset(0, 6)
ComboBox3 = ActiveCell.Offset(0, 7)
TextBox8 = ActiveCell.Offset(0, 8)
ComboBox4 = ActiveCell.Offset(0, 9)

TextBox1.Locked = False
TextBox2.Locked = False
TextBox3.Locked = False
ComboBox2.Locked = False
TextBox5.Locked = False
TextBox6.Locked = False
TextBox7.Locked = False
ComboBox3.Locked = False
TextBox8.Locked = False
ComboBox4.Locked = False

End If
End If
End Sub

Private Sub CommandButton1_Click()
'En esta parte verificamos si el id ya existe, si es asi, no introduce nada, en caso contrario introduce valores
Registro = TextBox1.Value
contarregistros = Application.WorksheetFunction.CountIf(Columns(1), Registro) 'contar si en su version vba
If contarregistros > 0 Then
MsgBox "Ya existe un registro con ese FOLIO KUE"
Else
Sheets("Formulario").Activate
Range("A" & Cells.Rows.Count).End(xlUp).Offset(1).Select
ActiveCell = TextBox1.Value
ActiveCell.Offset(0, 1) = TextBox2.Value
ActiveCell.Offset(0, 2) = TextBox3.Value
ActiveCell.Offset(0, 3) = ComboBox2.Value
ActiveCell.Offset(0, 4) = TextBox5.Value
ActiveCell.Offset(0, 5) = TextBox6.Value
ActiveCell.Offset(0, 6) = TextBox7.Value
ActiveCell.Offset(0, 7) = ComboBox3.Value
ActiveCell.Offset(0, 8) = TextBox8.Value & "" & ComboBox5.Value
ActiveCell.Offset(0, 9) = ComboBox4.Value

MsgBox "Datos ingresados exitosamente", vbInformation, "Servitap"
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
ComboBox2 = ""
TextBox5 = ""
TextBox6 = ""
TextBox7 = ""
ComboBox3 = ""
TextBox8 = ""
ComboBox5 = ""
ComboBox4 = ""
End If

Me.ListBox1.Clear
Call LlenarListBox

TextBox1.SetFocus

End Sub

Respuesta

Amigo wayanluis garcia muñoz revisa el siguiente enlace en donde haces casi la misma pregunta, revísalo y lo adaptas a tu código.

Mensaje de error en una consulta utilizando Checkbox y ComboBox

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas