My environment: Windows 10, LibreOffice Version: 6.4.2.2 (x64) HSQLDB Version 2.3.2 Split Database
A Main Table has a sub Table in a one-to-many relationship.
My objective is to force add a default record in the sub Table when a new record is added to the Main Table.
I would like to do this with a macro triggered by the update event in the main table.
I’m stuck on finding the appropriate code and event trigger to get the ID (a text ID primary key) of the newly created record in a variable, I can handle the rest of the code from there.
Thank you for any help.
EDIT: More Info
I tried Sub AddNewRecord
on Before Updating
of the Control and Sub AfterAddRecord
on After Updating
of Control, nothing happens (No MsgBox).
I tried Sub AddNewRecord
on Before Record Change
of the Form and Sub AfterAddRecord
of the Form, this triggers the MsgBox with Null on opening, a value on clicking on any record, and a Null MsgBox after adding a new record.
Maybe fatigue has reduced me to not seeing the obvious.
EDIT 2020-04-13: Solved
For those interested here is the final working code (the ID to add as new record is called “TextRef”):
One could consider adding a lookup routine on the When Loading
event of the form to check if there are any default records (ones with content “unspecified”) in the subtable and if so provide a MessageBox alert naming the record that needs attention.
Global sGlobalAftRecChange As String
Sub MacroAfterRecordChange
REM this is attached to the forms --After Record Change-- event
Dim oForm As Object
Dim oField As Object
sGlobalAftRecChange = ""
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
oField = oForm.getByName("TextRef")
sFieldData = oField.Text
If sFieldData = "" Then
sGlobalAftRecChange = "NewRecord"
End If
End Sub
Global sGlobalHold As String
Sub MacroBeforeRecordAction
REM this is attached to the forms --Before Record Action-- event
Dim oForm As Object
Dim oField As Object
sGlobalHold = ""
oForm = ThisComponent.Drawpage.Forms.getByName("MainForm")
oField = oForm.getByName("TextRef")
sGlobalHold = oField.Text
End Sub
Sub MacroAfterRecordAction
REM this is attached to the forms --After Record Action-- event
REM Add default language records for a new TextRef
Dim oForm As Object
Dim sTextRef As String
Dim sNewText As String
Dim sSelect As String
If sGlobalHold = "" Then
Exit Sub
End If
If sGlobalAftRecChange = "NewRecord" Then
sTextRef = sGlobalHold
sNewText = "Unspecified"
iCounter = 1
Do While (iCounter < 7)
sSQL = "INSERT INTO ""Text Fields Basic Details"" (""TextRef"",""Lingo"",""Text"") VALUES ('" & sTextRef & "','" & iCounter & "','" & sNewText & "')"
RunSomeSQL (sSQL)
iCounter = iCounter + 1
Loop
End If
End Sub
Sub RunSomeSQL (sSqlTask As String)
Dim oStatement As Object
If IsNull(ThisDatabaseDocument.CurrentController.ActiveConnection) then
ThisDatabaseDocument.CurrentController.connect
End If
oStatement = ThisDatabaseDocument.CurrentController.ActiveConnection.createStatement()
On Error GoTo ErrorSituation
oStatement.execute(sSqlTask)
Exit Sub
ErrorSituation:
MsgBox("Invalid Sql= " & Chr$(13) & Chr$(13) & sSqlTask)
End Sub