[Solved] Confirm changes before saving record

Discuss the database features
Post Reply
MusicMan62
Posts: 9
Joined: Wed Mar 19, 2014 11:37 am

[Solved] Confirm changes before saving record

Post by MusicMan62 »

I have just moved a database across from Access to Base and it is all working fine. I can update, search and add records.

However I find it just too easy to accidentally modify records on my Form as Base autosaves a record if I move to another record (I am using a table subform so it's easy to move from one record to another). I want a feature that displays a confirmation YES/NO MsgBox and if you enter NO then it doesn't save the record and puts the old data back.

I did this in Access using a macro on the 'Before Update' event. I tried to do the same in Base on the 'Before record change' event but I found this event is triggered several times on opening up the Form and again several times when moving to another record and even closing the table. I just want it to ask once!

This is what I have been trying:

Code: Select all

Sub confirmInsert(event as object)
  confirmInsert=True
  If MsgBox("Update Record?", 4,"Confirm")=7 Then
   confirmInsert=False
   rem NEED AN UNDO ACTION HERE
  End If
End sub 
I have recorded a macro to undo an update which I have tried in the above code with moderate success. The big problem is the multiple calls to an event when I move around the subform table. Can I intercept the SAVE record routine somehow?

Isn't this sort of protection something quite basic in databases? I would have thought there would be an easy inbuilt solution to this.

Any help much appreciated.
Last edited by MusicMan62 on Fri Mar 21, 2014 7:48 am, edited 1 time in total.
OpenOffice 4.1.0 on Mac OSX 10.9.3
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: Confirm changes before saving record

Post by MTP »

Ah, hadn't seen this before I replied at the other forum: http://www.oooforum.org/forum/viewtopic ... 261352aff0
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
MusicMan62
Posts: 9
Joined: Wed Mar 19, 2014 11:37 am

Re: Confirm changes before saving record

Post by MusicMan62 »

OK I solved it.

For anyone else wanting to know how to do it. Run the following macro on the 'Before Record Action' event in the Form. It now correctly asks YES/NO to change a record and puts the old data back if I say NO

Code: Select all

sub BeforeRecordAction(oEvent as object)
   dim oForm
   select case  oEvent.Source.implementationname
      case "com.sun.star.form.FmXFormController" : oForm= oEvent.Source.model
      case "com.sun.star.comp.forms.ODatabaseForm"
			oForm= oEvent.Source
       		If MsgBox("Update record?", 4,"Confirm")=7 Then UndoRecord()
      case "org.openoffice.comp.svx.FormController" : oForm= oEvent.Source.model
      case else 
			rem print oEvent.Source.implementationname
   end select
End Sub

sub UndoRecord
	dim document   as object
	dim dispatcher as object
	rem ----------------------------------------------------------------------
	rem get access to the document
	document   = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

	rem ----------------------------------------------------------------------
	dispatcher.executeDispatch(document, ".uno:RecUndo", "", 0, Array())
end sub
:D
OpenOffice 4.1.0 on Mac OSX 10.9.3
Post Reply