form records

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Good morning,

I have a "class" form that has a record source based on a
query. The form is used to edit classes. Upon opening up
the form... the query asks the user to enter the "Class
ID", which is a auto number. Then the form is opened and
all of the fields are filled with the class's
information. On the form, I have a command button which
when clicked it saves the information for that class.

But when I tested the form out it does not work
perfectly. When I open the form. Any fields that I edit
are updated right then and there. So, it doesn't matter
if I click on the save command button or just close out
the form (in essence, the save command button is not doing
what I would like it to do). Either way, the record gets
edited/updated. How can I get the form to only
save/update the records only when the save command button
is clicked?

Thanks for the help!
 
Good morning,

I have a "class" form that has a record source based on a
query. The form is used to edit classes. Upon opening up
the form... the query asks the user to enter the "Class
ID", which is a auto number. Then the form is opened and
all of the fields are filled with the class's
information. On the form, I have a command button which
when clicked it saves the information for that class.

Rather than having the user have to memorize and type in a meaningless
autonumber, you might want to consider concealing the autonumber from
view and using a Combo Box or Listbox to let the user pick the class
from a list. This can be set up using the Combo Box wizard.
But when I tested the form out it does not work
perfectly. When I open the form. Any fields that I edit
are updated right then and there. So, it doesn't matter
if I click on the save command button or just close out
the form (in essence, the save command button is not doing
what I would like it to do). Either way, the record gets
edited/updated. How can I get the form to only
save/update the records only when the save command button
is clicked?

This is exactly how Access forms are designed to work: edit the data,
move to the next record, save the data. If you want to defeat this
normal behavior and require an extra mouseclick you need some VBA code
in three places:

1. In the Form's Current event, invoke the Code Builder (doubleclick
the property so it shows [Event Procedure] and click the ... icon).
Edit the Sub and End Sub lines to read:

Public bOKToClose As Boolean
Private Sub Form_Current()
bOKToClose = False ' By default you can't close the form
End Sub

Then similarly edit the form's BeforeUpdate event:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If Not bOKToClose Then
iAns = MsgBox("Please use the SAVE button," & vbCrLf & _
"or click Cancel to erase your input", vbOKCancel)
If iAns = vbCancel Then
Me.Undo ' erase the form
End If
Cancel = True ' stop the update in either case
End If
' If bOKToClose is True, do nothing, let the form update
End Sub

and finally in the Click event of the Save button:

Private Sub cmdSave_Click()
bOKToClose = True
If Me.Dirty = True Then Me.Dirty = False ' force a save of the record
' you might want to close the form or move to the new record at this
' point
End Sub
 
Back
Top