ich brauche dringend ein Makro zum Kopieren von Datensatze in Base, da ich häufiger nahezu identische Datensätze habe, die sich nur wenig unterscheiden.
Auf http://codesnippets.services.openoffice ... ToNew.snip fand ich den unten aufgeführten Code. Die Ausführung wird jedoch abgebrochen bei
Code: Alles auswählen
oForm = oDoc.Drawpage.Forms(0)
Woran könnte es liegen:
Grüße, Stefan
Code: Alles auswählen
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, "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("com.sun.star.awt.UnoControlDateFieldModel") then
aVal(i) = aControl.Date
elseif aControl.supportsService("com.sun.star.awt.UnoControlTimeFieldModel") then
aVal(i) = aControl.Time
elseif aControl.supportsService("com.sun.star.awt.UnoControlListBoxModel") then
aVal(i) = oDoc.currentController.getControl(aControl).SelectedItemPos
elseif aControl.supportsService("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, "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("com.sun.star.awt.UnoControlDateFieldModel") then
aControl.Date = aVal(i)
elseif aControl.supportsService("com.sun.star.awt.UnoControlTimeFieldModel") then
aControl.Time = aVal(i)
elseif aControl.supportsService("com.sun.star.awt.UnoControlListBoxModel") then
oDoc.currentController.getControl(aControl).SelectedItemPos(aVal(i))
elseif aControl.supportsService("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