Datensatz kopieren in Base

Datenbanklösungen mit AOO/LO

Moderator: Moderatoren

shorn
Beiträge: 4
Registriert: Do, 08.05.2008 10:13

Datensatz kopieren in Base

Beitrag von shorn »

Hallo,

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)
Meldung: BASIC Laufzeitfehler: Eigenschaft oder Methode nicht gefunden.

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

Benutzeravatar
komma4
********
Beiträge: 5332
Registriert: Mi, 03.05.2006 23:29
Wohnort: Chon Buri Thailand Asia
Kontaktdaten:

Re: Datensatz kopieren in Base

Beitrag von komma4 »

Lieber Stefan,

sei mir nicht böse...
shorn hat geschrieben:Woran könnte es liegen
An Deinem Verständnis!!?


Du hast ein snippet gefunden - keine gebrauchsfertige Anwendung. Dir fehlt das Formular (verbunden mit Deinen Daten), mit dem Du eine angepasste Version des snippets nutzen kannst.


Ich nutze Base praktisch nicht, habe nur wenig Erfahrung mit seiner Programmierung (a la OOoWiki Base API), arbeite eher mit Dialogen und direkt ausgeführtem SQL.
Wenn Du OOoBase programmieren möchtest empfehle ich Andrews Base-Dokument.


Viel Erfolg!
Cheers
Winfried
aktuell: LO 5.3.5.2 30m0(Build:2) SUSE rpm, unter Linux openSuSE Leap 42.3 x86_64/KDE5
DateTime2 Einfügen von Datum/Zeit/Zeitstempel (als OOo Extension)
shorn
Beiträge: 4
Registriert: Do, 08.05.2008 10:13

Re: Datensatz kopieren in Base

Beitrag von shorn »

Hallo,
Du hast ein snippet gefunden - keine gebrauchsfertige Anwendung. Dir fehlt das Formular (verbunden mit Deinen Daten), mit dem Du eine angepasste Version des snippets nutzen kannst.
Ist klar, ein Formular mit Schaltfläche ist natürlich vorhanden.
Wenn Du OOoBase programmieren möchtest empfehle ich Andrews Base-Dokument.
Vielleicht finde ich dort einen Hinweis, Danke für den Link.

Grüße, Stefan
Benutzeravatar
komma4
********
Beiträge: 5332
Registriert: Mi, 03.05.2006 23:29
Wohnort: Chon Buri Thailand Asia
Kontaktdaten:

Re: Datensatz kopieren in Base

Beitrag von komma4 »

...dann hast Du oDoc nicht (richtig) gesetzt.

XRay hast Du schon entdeckt?
Cheers
Winfried
aktuell: LO 5.3.5.2 30m0(Build:2) SUSE rpm, unter Linux openSuSE Leap 42.3 x86_64/KDE5
DateTime2 Einfügen von Datum/Zeit/Zeitstempel (als OOo Extension)
Antworten