View Single Post
  #7  
Old August 22nd, 2009, 12:26 AM posted to microsoft.public.access.gettingstarted
KenSheridan via AccessMonster.com
external usenet poster
 
Posts: 1,610
Default Adding new records via recordset

Entering data in a bound form begins the process of inserting a new row
(record) into the underlying table, but the record is not actually saved
until the user navigates off the current record, closes the form or
explicitly saves the record in some other way. In the meantime the form's
Dirty property is True. The new record can be aborted with the Esc key
before it is saved.

If you are concerned about a new record being inadvertently saved then one
thing you can do is force the record to be saved only by the user clicking a
'Save Record' button on the form. The following is the code from the module
of a simple form which demonstrates this:

' updates can only be saved via command button
Option Compare Database
Option Explicit

Dim blnSaved As Boolean

Private Sub cmdSave_Click()

Const MESSAGETEXT = "Save record?"

If Me.Dirty Then
' if user confirms set variable to True and attempt to save record
If MsgBox(MESSAGETEXT, vbQuestion + vbYesNo, "Confirm") = vbYes Then
blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
' if record cannot be saved set variable to False
If Err 0 Then
blnSaved = False
End If
Else
blnSaved = False
End If
End If

End Sub


Private Sub cmdUndo_Click()

' undo edits
Me.Undo

End Sub

Private Sub Form_AfterUpdate()

' reset variable to False
blnSaved = False

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

' cancel update if variable is False,
' i.e. save button has not been clicked
If Not blnSaved Then
Cancel = True
End If

End Sub

Private Sub Form_Current()

' reset variable to False
blnSaved = False

End Sub


Private Sub Form_Error(DataErr As Integer, Response As Integer)

Const IS_DIRTY = 2169

' suppress system error message if form
' is closed while record is unsaved,
' NB: changes to current record will be lost
If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If

End Sub

As well as the controls bound to the columns (fields) in the underlying table
the form includes two command buttons; cmdSave to save a new or edited
record; cmdUndo to abort a new record after data has begun to be entered, or
to discard any changes made to am existing record.

One thing to note is that with a form in which the above code is used, if a
user closes the form without first clicking the Save Record button any
changes to the current record will be lost without warning. If on the other
hand they try to navigate to another record without having first saved
changes to the current record via the button, they will be unable to do so
until they click either the save button or the undo button.

Ken Sheridan
Stafford, England

ssutton503 wrote:
Thanks guys. I will rethink how I am approaching this situation. I'm sure you
are right when you say that "just opening a form" won't create a new record.
It seems that what happens is that as soon as the user starts entering data
on the form a record is created. I will use your suggestions and see what I
can come up with. Thanks again.

am I just going at this the hard way?

[quoted text clipped - 33 lines]
Any
suggestions would be greatly appreciated.


--
Message posted via http://www.accessmonster.com