When does form write to db?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form that a user inputs an employee number, a shift start, and shift
end date/time that is captured in a table called hours. I have a button
included in the form that the user selects to save the record. There is also
another button to close the form. My question is when a user enters their
employee number, and the shift start time and then selects the "close form"
button the record is still captured. I only want to capture the record if it
is complete (shift start and shift end) and only write when the user selects
save. Can someone tell me what I need to do to accomplish this? I am using
the "close form" that is part of the standard buttons included in access.

Thanks,

Jeff
 
I have a form that a user inputs an employee number, a shift start, and shift
end date/time that is captured in a table called hours. I have a button
included in the form that the user selects to save the record. There is also
another button to close the form. My question is when a user enters their
employee number, and the shift start time and then selects the "close form"
button the record is still captured. I only want to capture the record if it
is complete (shift start and shift end) and only write when the user selects
save. Can someone tell me what I need to do to accomplish this? I am using
the "close form" that is part of the standard buttons included in access.

Thanks,

Jeff

By default, a record is written to disk when the user closes the Form,
closes the database, selects any field in any subform on the Form,
moves to a new record, or moves to a previous record.

If you want to restrict saving the record you must put VBA code into
the form's BeforeUpdate event to prevent this automatic save. Define a
Boolean variable *at the top* of the Form's Module, before any of the
Sub lines:

Option Explicit
Option Compare Database

Dim bOKToClose As Boolean

Private Sub... <first routine>

In your Save button code set bOKToClose to True; set it to False in
the form's Current event (so new and newly selected records won't be
saved); and put code like this in the form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Not bOKToClose Then
MsgBox "Please use the Save button first, or cancel the record" _
& " by hitting the <Esc> key twice", vbOKOnly
Cancel = True
End If
End Sub


John W. Vinson[MVP]
 
Back
Top