En esta lección trabajamos la Ventana De Producto y su Validación.
En ese vídeo se validan los posibles errores que el usuario puede cometer al ingresar un producto. Ejemplo si deja el Campo Precio Costo vació esto genera que al calcular el precio de venta genere error o al al dejar el campo Porcentaje de Utilidad vació general igual un error al hacer el calculo.
En Este vídeo implementamos el usuario de On Error GoTo que es el manejador de errores de Visual Basic con ello evitamos que la aplicación se cierre al tener un error no controlado se implementa de la siguiente manera:
Se coloca On Error Goto miError el texto que dice miErro puede colocar la etiqueta que quiera es la palabra que usaremos para que visual se dirija a mensaje de error que tenemos. Luego al final del código colocamos ya sea un Exit sub o un Exit Function dependiento de en que estemos y luego la etiqueta que usamos:
Error:
msgbox Err.Descripcion
Un ejemplo completo seria así:
Sub Calcular()
On Error goto Error
Sum = Sum + 1
exit Sub
Error:
msgbox err.Description
End Sub
Veamos el Vídeo:
Se modificaron varios de los procedimientos que ya teníamos implementados:
Sub PorcentajeUtilidad(PrecioVenta As MaskEdBox, Porcentaje As TextBox, campUtilidad As TextBox)
On Error GoTo error
If PrecioVenta.Text = "" Then
MsgBox "El campo Precio de Venta no puede estar vacío", vbExclamation, "Error"
PrecioVenta.SetFocus
End If
If txtPrecioCImp.Text = "" Then
MsgBox "El campo Precio Costo no puede estar vacio", vbExclamation, "Error"
txtPrecioCImp.SetFocus
Exit Sub
End If
If Utilidad = "" Then
Utilidad = 0
End If
If PrecioVenta.Text > 0 Then
'calculamos la utilidad
CalPorcentaje = ((CCur(PrecioVenta) / CCur(txtPrecioCImp)) - 1) * 100
campUtilidad.Text = Round(CalPorcentaje, 2)
End If
Exit Sub
error:
MsgBox "PorcentajeUtilidad: " & Err.Description, vbCritical, "Error"
End Sub
Sub PrecioPublico(PrecioCosto As MaskEdBox, Porcentaje As TextBox, PrecioVenta As MaskEdBox, campUtilidad As TextBox)
If PrecioCosto = "" Then
MsgBox "El Precio Costo no puede estar vacío", vbExclamation, "Error"
Exit Sub
End If
If Porcentaje = "" Then
MsgBox "El Porcentaje de Utilidad no puede estar vacío", vbExclamation, "Error"
Exit Sub
End If
If CCur(PrecioCosto) = 0 Then
MsgBox "El Precio Costo debe ser mayor a cero", vbExclamation, "Error"
Exit Sub
End If
If Porcentaje = 0 Then
MsgBox "El Porcentaje de Utilidad debe ser mayor a cero", vbExclamation, "Error"
Exit Sub
End If
If PrecioCosto > 0 Then
PorcenGan = (Porcentaje) / 100
PrecioVenta = PrecioCosto + (PrecioCosto * PorcenGan)
Utilidad = PrecioVenta - PrecioCosto
campUtilidad = Format(Utilidad, "Currency")
End If
End Sub
Sub CalcularImpuesto()
If CCur(txtPrecioCosto.Text) = 0 Then
MsgBox "El Precio Costo debe ser mayor a Cero", vbExclamation, "Error"
Exit Sub
End If
If txtPrecioCosto.Text <> "" Then
PrecioCosto = CCur(txtPrecioCosto.Text)
Impuesto = txtImpuesto.Text
If PrecioCosto <> "" And Impuesto <> "" Then
PrecioImpuesto = PrecioCosto + ((PrecioCosto * Impuesto) / 100)
Else
PrecioImpuesto = 0
End If
txtPrecioCImp.Text = PrecioImpuesto
Else
txtPrecioCosto.Text = 0
End If
End Sub
El procedimiento de Guardar Producto quedo de la Siguiente Manera:
Dim CodigoProducto
Sub GuardarProducto()
If txtCodigoPro.Text = "" Then
MsgBox "Debe llenar el Campo Código", vbExclamation, "Error"
Exit Sub
End If
If txtNombrePro.Text = "" Then
MsgBox "Debe llenar el Campo Nombre del Producto", vbExclamation, "Error"
Exit Sub
End If
If txtNombreCort.Text = "" Then
MsgBox "Debe llenar el Campo Nombre Corto", vbExclamation, "Error"
Exit Sub
End If
If txtEstante.Text = "" Then
MsgBox "Debe llenar el Campo Estante", vbExclamation, "Error"
Exit Sub
End If
If txtPrecioCosto.Text = "" Then
MsgBox "Debe llenar el Campo Precio Costo", vbExclamation, "Error"
Exit Sub
End If
If txtPrecioV1.Text = "" Then
MsgBox "Debe llenar el Campo Precio Venta 1", vbExclamation, "Error"
Exit Sub
End If
If txtImpuesto.Text = "" Then
MsgBox "Debe llenar el Campo Impuesto", vbExclamation, "Error"
Exit Sub
End If
CodCategoria = cmbCategorias.ItemData(cmbCategorias.ListIndex)
CodProv = cmdProveedor.ItemData(cmdProveedor.ListIndex)
If CodigoProducto= 0 Then
IdProducto = UltimoIdTabla("tblProductos", "IdProducto")
Sql = "Insert Into tblProductos (IdProducto, CodigoPro, NombrePro, NombreCortoPro,EstantePro, ExistPro, ExistMinPro, PCostoPro,PVenta1Pro, PVenta2Pro, PVenta3Pro, PMinimoPro, IdCategoria, IdProveedor) Values (" & IdProducto & ",'" & txtCodigoPro & "','" & txtNombrePro & "','" & txtNombreCort & "','" & txtEstante & "','" & txtExistencia & "','" & txtExistMinima & "','" & txtPrecioCosto & "','" & txtPrecioV1 & "','" & txtPrecioV2 & "','" & txtPrecioV3 & "', '" & txtPrecioVMinim & "', " & CodCategoria & ", " & CodProv & ") "
Else
sql = "Update tblProductos SET CodigoPro = '" & txtCodigoPro & "', NombrePro = '" & txtNombrePro & "',NombreCortoPro = '" & txtNombreCort & "',EstantePro = '" & txtEstante & "',ExistPro = '" & txtExistencia & "',ExistMinPro = '" & txtExistMinima & "', PCostoPro = '" & txtPrecioCosto & "', PVenta1Pro = '" & txtPrecioV1 & "', PVenta2Pro = '" & txtPrecioV2 & "', PVenta3Pro = '" & txtPrecioV3 & "', PMinimoPro = '" & txtPrecioVMinim & "', IdCategoria = '" & CodCategoria & "', IdProveedor = '" & CodProv & "', Impuesto = " & txtImpuesto.Text & " Where IdProducto = " & CodigoProducto
End If
ConexionADO.Execute Sql
MsgBox "Producto Guardado", vbInformation, "Guardar"
End Sub
Ultima revisión 30/06/2021
Siguiente Lección Parte 19 – Ventana de Inventario

