Anda di halaman 1dari 2

Práctica: Partimos de un formulario de sólo lectura (AllowEdits=False).

Añadimos tres botones Guardar y Borrar con el asistente y otro


Modificar.
El procedimiento asociado al botón Modificar deberá realizar las siguientes
operaciones:
Dejar el formulario con posibilidad de modificaciones.
Situar el cursor en el segundo campo del formulario NombreProducto.

Para evitar alteraciones no deseadas en los datos del formulario lo


dejaremos de manera que solo se permita introducir datos al agregar un
nuevo registro o modificar otro existente.

Puesto que podemos guardar los datos con el botón Guardar pero también con
las flechas de desplazamiento entre registros del formulario, pondremos el
formulario en modo de solo lectura (Me.AllowEdits = False) en los eventos
Form_AfterUpdate( ) y Form_Current( ).
De esta forma únicamente al agregar un nuevo registro o pulsando el botón
Modificar (Me.AllowEdits = True) el formulario será editable.
Option Compare Database

Private Sub btnAgregarProducto_Click()


On Error GoTo Err_btnAgregarProducto_Click

DoCmd.GoToRecord , , acNewRec
NombreProducto.SetFocus

Exit_btnAgregarProducto_Click:
Exit Sub

Err_btnAgregarProducto_Click:
MsgBox Err.Description
Resume Exit_btnAgregarProducto_Click

End Sub

Private Sub btnGuardarProducto_Click()


On Error GoTo Err_btnGuardarProducto_Click

DoCmd.RunCommand acCmdSaveRecord

Exit_btnGuardarProducto_Click:
Exit Sub

Err_btnGuardarProducto_Click:
MsgBox Err.Description
Resume Exit_btnGuardarProducto_Click

End Sub

Private Sub btnModificarProducto_Click()


Me.AllowEdits = True
NombreProducto.SetFocus
End Sub

Private Sub btnBorrarProducto_Click()


On Error GoTo Err_btnBorrarProducto_Click

DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord

Exit_btnBorrarProducto_Click:
Exit Sub

Err_btnBorrarProducto_Click:
MsgBox Err.Description
Resume Exit_btnBorrarProducto_Click

End Sub

Private Sub Form_AfterUpdate()


Me.AllowEdits = False
MsgBox "Producto Guardado", vbInformation, "Añadir o modificar datos"
End Sub

Private Sub Form_Current()


Me.AllowEdits = False
End Sub

Anda mungkin juga menyukai