Am Mittwoch, den 16.05.2007, 20:04 +0200 schrieb Alex Thurgood:
> > I used this approach for copying a record in a form as template for e
> > new one (excliding the primary key field).
> 
> Now I have exactly this problem, you wouldn't have some macro code lying 
> around somewhere that you'd be willing to share by any chance ?

It's "Code-giving day" today. ;)

Please post any additions you make, not all possible cases are catched
by the code!

Have fun, too.

Marc

<snip>
' 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

' --- EOF
</snip>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to