record template form copy Marc Santhoff How can I copy an existing record to a new one as template?

When typing in data into a database form sometimes many records only differ in a small number of fields.

In this case it is desirable to copy one record to a new, empty one and only change the minor differences by hand.

' Copies the value currently shown in the form ' as a template to a new record. This new record only ' is displayed for editing, not stored yet. ' Only some control types are checked here, please ' add missing types needing special treatment. ' The programmer or user has to take care of adding a ' new primary key into the corresponding field control. ' event binding sub sub copyToNewEvent(evt as object) copyRecordToNewRecord(thisComponent) end sub sub copyRecordToNewRecord(oDoc as object, optional sKeyfieldname as string) dim aVal as Variant dim ccount as integer dim i as integer if IsMissing(sKeyfieldname) then sKeyfieldname = "ID" oForm = oDoc.Drawpage.Forms(0) ccount = oForm.count redim aVal(ccount) ' Step 1 --> get the current fields content (excluding key field for auto values) ' loop over all controls for i=0 to ccount-1 aControl = oForm.getByIndex(i) ' only get controls storing database values if HasUNOInterfaces(aControl, "{@see com.sun.star.form.XBoundComponent}") then ' read currently shown value n = aControl.name ' exclude primary key field if (InStr(sKeyfieldname, n)=0) then if aControl.supportsService("{@see com.sun.star.awt.UnoControlDateFieldModel}") then aVal(i) = aControl.Date elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlTimeFieldModel}") then aVal(i) = aControl.Time elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlListBoxModel}") then aVal(i) = oDoc.currentController.getControl(aControl).SelectedItemPos elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlFormattedFieldModel}") then aVal(i) = aControl.EffectiveValue else aVal(i) = aControl.Text end if end if end if next i ' Step 2 --> make a new record in the form (only) oForm.moveToInsertRow() ' Step 3 --> copy in saved values ' loop over controls again for i=0 to ccount-1 aControl = oForm.getByIndex(i) if HasUNOInterfaces(aControl, "{@see com.sun.star.form.XBoundComponent}") then ' get the value to set n = aControl.name ' exclude primary key field if (InStr(sKeyfieldname, n)=0) then if aControl.supportsService("{@see com.sun.star.awt.UnoControlDateFieldModel}") then aControl.Date = aVal(i) elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlTimeFieldModel}") then aControl.Time = aVal(i) elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlListBoxModel}") then oDoc.currentController.getControl(aControl).SelectedItemPos(aVal(i)) elseif aControl.supportsService("{@see com.sun.star.awt.UnoControlFormattedFieldModel}") then oDoc.currentController.getControl(aControl).setText(aVal(i)) else ' an EditField aControl.Text = aVal(i) end if ' let the control store it's value into the bound field model aControl.commit() end if end if next i end sub Initial version