Saving changes rather than live edits question?

C

Craig Armitage

Hi,

Ive made a customers browser which is basically a listbox with all my
customers names in. I double click on a name to bring up an edit form.
Currently whenever i make a change to the form and close it, the change is
automatically made to the record but i would rather have a "close and save"
button along with being able to cancel accidental changes. How do i do
this? also, on the same note, in the customer browser i would like to have
an add button that brings up the same form but for a new record. again
how!?

thanks
 
S

Svetlana

On before update event of your form you could put the code
If MsgBox("Do you want to save changes?", vbYesNo, "Save?")=vbNo Then
DoCmd.RunCommand Undo
End If

On add button after opening your form set the property DataEntry to yes

DoCmd.OpenForm "NameOfForm"
Forms!NameOfForm.DataEntry=Yes
 
G

Guest

Code along these lines in the form's module should do it, where cmdSave is
the name of the button to save and close the current record. The blnSaved
variable keeps track of whether the record has been saved with the button and
the form's Error event procedure suppresses the system generated error
message if the form is closed while unsaved:

Option Compare Database
Option Explicit

Dim blnSaved As Boolean

Private Sub cmdSave_Click()

blnSaved = True
On Error Resume Next
RunCommand acCmdSaveRecord
DoCmd.Close acForm, Me.Name
If Err <> 0 Then
blnSaved = False
End If

End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)

If Not blnSaved Then
Cancel = True
Me.Undo
End If

End Sub

Private Sub Form_Current()

blnSaved = False

End Sub


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

Const IS_DIRTY = 2169

If DataErr = IS_DIRTY Then
Response = acDataErrContinue
End If

End Sub

To open the form at a new record use code along these lines in the button's
Click event procedure:

DoCmd.OpenForm "YourForm", DataMode:=acFormAdd

Ken Sheridan
Stafford, England
 
C

Craig Armitage

Hi Ken and Svetlana,

Thank you both for your great answers. Ken, your explanation of Queries was
great, gonna sit down and read it thoroughly later..

Just on the subject of editing records from a listbox...

I was wondering how i would delete a record that has been selected in the
listbox? and also, after adding a new record, how can i force the listbox
to refresh to show the new record? strangely, it does show updates if i
edit a record, just not new records unless i close and re-open the browser
form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top